]> xenbits.xensource.com Git - libvirt.git/commitdiff
virsh: Use Env variables for debug level and logfile
authorSupriya Kannery <supriyak@in.ibm.com>
Mon, 9 May 2011 07:08:06 +0000 (12:38 +0530)
committerEric Blake <eblake@redhat.com>
Thu, 19 May 2011 23:53:46 +0000 (17:53 -0600)
Use variables VIRSH_DEBUG and VIRSH_LOG_FILE for controlling virsh logging.

Signed-off-by: Supriya Kannery <supriyak@in.ibm.com>
tools/virsh.c

index c2f4de66b6ef14f50cf117768e2b735b1df3a059..de4948932f99be8d99b19ef0b76be631c86eaa03 100644 (file)
@@ -12406,9 +12406,32 @@ vshError(vshControl *ctl, const char *format, ...)
 static bool
 vshInit(vshControl *ctl)
 {
+    char *debugEnv;
+
     if (ctl->conn)
         return false;
 
+    if (ctl->debug == -1) {
+        /* log level not set from commandline, check env variable */
+        debugEnv = getenv("VIRSH_DEBUG");
+        if (debugEnv) {
+            if (virStrToLong_i(debugEnv, NULL, 10, &ctl->debug) < 0 ||
+                ctl->debug < VSH_ERR_DEBUG || ctl->debug > VSH_ERR_ERROR) {
+                vshError(ctl, "%s",
+                         _("VIRSH_DEBUG not set with a valid numeric value"));
+                ctl->debug = VSH_ERR_DEBUG;
+            }
+        }
+    }
+
+    if (ctl->logfile == NULL) {
+        /* log file not set from cmdline */
+        debugEnv = getenv("VIRSH_LOG_FILE");
+        if (debugEnv && *debugEnv) {
+            ctl->logfile = vshStrdup(ctl, debugEnv);
+        }
+    }
+
     vshOpenLogFile(ctl);
 
     /* set up the library error handler */
@@ -12507,14 +12530,15 @@ vshOutputLogFile(vshControl *ctl, int log_level, const char *msg_format,
     */
     gettimeofday(&stTimeval, NULL);
     stTm = localtime(&stTimeval.tv_sec);
-    virBufferAsprintf(&buf, "[%d.%02d.%02d %02d:%02d:%02d %s] ",
+    virBufferAsprintf(&buf, "[%d.%02d.%02d %02d:%02d:%02d %s %d] ",
                       (1900 + stTm->tm_year),
                       (1 + stTm->tm_mon),
                       stTm->tm_mday,
                       stTm->tm_hour,
                       stTm->tm_min,
                       stTm->tm_sec,
-                      SIGN_NAME);
+                      SIGN_NAME,
+                      (int) getpid());
     switch (log_level) {
         case VSH_ERR_DEBUG:
             lvl = LVL_DEBUG;
@@ -12835,7 +12859,7 @@ vshUsage(void)
                       "  options:\n"
                       "    -c | --connect <uri>    hypervisor connection URI\n"
                       "    -r | --readonly         connect readonly\n"
-                      "    -d | --debug <num>      debug level [0-5]\n"
+                      "    -d | --debug <num>      debug level [0-4]\n"
                       "    -h | --help             this help\n"
                       "    -q | --quiet            quiet mode\n"
                       "    -t | --timing           print timing information\n"
@@ -13083,6 +13107,7 @@ main(int argc, char **argv)
     memset(ctl, 0, sizeof(vshControl));
     ctl->imode = true;          /* default is interactive mode */
     ctl->log_fd = -1;           /* Initialize log file descriptor */
+    ctl->debug = -1;            /* Initialize log level */
 
     if (!setlocale(LC_ALL, "")) {
         perror("setlocale");