]> xenbits.xensource.com Git - libvirt.git/commitdiff
virsh: use new method for easier log to file
authorEric Blake <eblake@redhat.com>
Sat, 30 Apr 2011 17:05:43 +0000 (11:05 -0600)
committerEric Blake <eblake@redhat.com>
Thu, 5 May 2011 19:48:19 +0000 (13:48 -0600)
Easier to maintain, and no longer an arbitrary line length limit.

* tools/virsh.c (vshOutputLogFile): Replace snprintf with
virBuffer.

tools/virsh.c

index 4aa17e6bdb216fa1524114b3144aad32b0d4dd8a..e32680c196ec920769d09312b712b8de88e3e73f 100644 (file)
@@ -12231,7 +12231,9 @@ static void
 vshOutputLogFile(vshControl *ctl, int log_level, const char *msg_format,
                  va_list ap)
 {
-    char *msg_buf;
+    virBuffer buf = VIR_BUFFER_INITIALIZER;
+    char *str;
+    size_t len;
     const char *lvl = "";
     struct timeval stTimeval;
     struct tm *stTm;
@@ -12239,8 +12241,6 @@ vshOutputLogFile(vshControl *ctl, int log_level, const char *msg_format,
     if (ctl->log_fd == -1)
         return;
 
-    msg_buf = vshMalloc(ctl, MSG_BUFFER);
-
     /**
      * create log format
      *
@@ -12248,16 +12248,14 @@ vshOutputLogFile(vshControl *ctl, int log_level, const char *msg_format,
     */
     gettimeofday(&stTimeval, NULL);
     stTm = localtime(&stTimeval.tv_sec);
-    snprintf(msg_buf, MSG_BUFFER,
-             "[%d.%02d.%02d %02d:%02d:%02d ",
-             (1900 + stTm->tm_year),
-             (1 + stTm->tm_mon),
-             (stTm->tm_mday),
-             (stTm->tm_hour),
-             (stTm->tm_min),
-             (stTm->tm_sec));
-    snprintf(msg_buf + strlen(msg_buf), MSG_BUFFER - strlen(msg_buf),
-             "%s] ", SIGN_NAME);
+    virBufferAsprintf(&buf, "[%d.%02d.%02d %02d:%02d:%02d %s] ",
+                      (1900 + stTm->tm_year),
+                      (1 + stTm->tm_mon),
+                      stTm->tm_mday,
+                      stTm->tm_hour,
+                      stTm->tm_min,
+                      stTm->tm_sec,
+                      SIGN_NAME);
     switch (log_level) {
         case VSH_ERR_DEBUG:
             lvl = LVL_DEBUG;
@@ -12278,21 +12276,31 @@ vshOutputLogFile(vshControl *ctl, int log_level, const char *msg_format,
             lvl = LVL_DEBUG;
             break;
     }
-    snprintf(msg_buf + strlen(msg_buf), MSG_BUFFER - strlen(msg_buf),
-             "%s ", lvl);
-    vsnprintf(msg_buf + strlen(msg_buf), MSG_BUFFER - strlen(msg_buf),
-              msg_format, ap);
+    virBufferAsprintf(&buf, "%s ", lvl);
+    virBufferVasprintf(&buf, msg_format, ap);
+    virBufferAddChar(&buf, '\n');
 
-    if (msg_buf[strlen(msg_buf) - 1] != '\n')
-        snprintf(msg_buf + strlen(msg_buf), MSG_BUFFER - strlen(msg_buf), "\n");
+    if (virBufferError(&buf))
+        goto error;
 
-    /* write log */
-    if (safewrite(ctl->log_fd, msg_buf, strlen(msg_buf)) < 0) {
-        vshCloseLogFile(ctl);
-        vshError(ctl, "%s", _("failed to write the log file"));
+    str = virBufferContentAndReset(&buf);
+    len = strlen(str);
+    if (len > 1 && str[len - 2] == '\n') {
+        str[len - 1] = '\0';
+        len--;
     }
 
-    VIR_FREE(msg_buf);
+    /* write log */
+    if (safewrite(ctl->log_fd, str, len) < 0)
+        goto error;
+
+    return;
+
+error:
+    vshCloseLogFile(ctl);
+    vshError(ctl, "%s", _("failed to write the log file"));
+    virBufferFreeAndReset(&buf);
+    VIR_FREE(str);
 }
 
 /**