]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
virsh: Do not format messages twice
authorJiri Denemark <jdenemar@redhat.com>
Thu, 20 Feb 2025 11:49:43 +0000 (12:49 +0100)
committerJiri Denemark <jdenemar@redhat.com>
Mon, 24 Feb 2025 10:14:11 +0000 (11:14 +0100)
The same message was formatted both in vshOutputLogFile and in vshDebug
and vshError functions. This patch refactor vshOutputLogFile and its
callers to only format each message once.

Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
tools/vsh.c
tools/vsh.h

index 5f5e2f281d25dab62338d949e4dab2b9eb099d6c..1b650bdd9baf7b8e549b413df1fa0f91d537027d 100644 (file)
@@ -1967,13 +1967,12 @@ vshDebug(vshControl *ctl, int level, const char *format, ...)
     if (level < ctl->debug)
         return;
 
-    va_start(ap, format);
-    vshOutputLogFile(ctl, level, format, ap);
-    va_end(ap);
-
     va_start(ap, format);
     str = g_strdup_vprintf(format, ap);
     va_end(ap);
+
+    vshOutputLogFile(ctl, level, str);
+
     fputs(str, stdout);
     fflush(stdout);
 }
@@ -2118,11 +2117,12 @@ vshError(vshControl *ctl, const char *format, ...)
     va_list ap;
     g_autofree char *str = NULL;
 
-    if (ctl != NULL) {
-        va_start(ap, format);
-        vshOutputLogFile(ctl, VSH_ERR_ERROR, format, ap);
-        va_end(ap);
-    }
+    va_start(ap, format);
+    str = g_strdup_vprintf(format, ap);
+    va_end(ap);
+
+    if (ctl)
+        vshOutputLogFile(ctl, VSH_ERR_ERROR, str);
 
     /* Most output is to stdout, but if someone ran virsh 2>&1, then
      * printing to stderr will not interleave correctly with stdout
@@ -2130,10 +2130,6 @@ vshError(vshControl *ctl, const char *format, ...)
     fflush(stdout);
     fputs(_("error: "), stderr);
 
-    va_start(ap, format);
-    str = g_strdup_vprintf(format, ap);
-    va_end(ap);
-
     fprintf(stderr, "%s\n", NULLSTR(str));
     fflush(stderr);
 }
@@ -2337,8 +2333,7 @@ vshOpenLogFile(vshControl *ctl)
  * Outputting an error to log file.
  */
 void
-vshOutputLogFile(vshControl *ctl, int log_level, const char *msg_format,
-                 va_list ap)
+vshOutputLogFile(vshControl *ctl, int log_level, const char *msg)
 {
     g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
     g_autofree char *str = NULL;
@@ -2381,7 +2376,7 @@ vshOutputLogFile(vshControl *ctl, int log_level, const char *msg_format,
             break;
     }
     virBufferAsprintf(&buf, "%s ", lvl);
-    virBufferVasprintf(&buf, msg_format, ap);
+    virBufferAddStr(&buf, msg);
     virBufferTrim(&buf, "\n");
     virBufferAddChar(&buf, '\n');
 
index 7de9fa2f099859b2ae0f80d279a8b46ab3b7ca24..1c7370dd4fbdd564b27bf949207360f9f492a864 100644 (file)
@@ -241,9 +241,7 @@ struct _vshCmdGrp {
 void vshError(vshControl *ctl, const char *format, ...)
     G_GNUC_PRINTF(2, 3);
 void vshOpenLogFile(vshControl *ctl);
-void vshOutputLogFile(vshControl *ctl, int log_level, const char *format,
-                      va_list ap)
-    G_GNUC_PRINTF(3, 0);
+void vshOutputLogFile(vshControl *ctl, int log_level, const char *msg);
 void vshCloseLogFile(vshControl *ctl);
 
 int vshCommandOptInt(vshControl *ctl, const vshCmd *cmd,