From 575b3ea925f4d4f61072fe1e12b26ee304dc5944 Mon Sep 17 00:00:00 2001 From: Jiri Denemark Date: Thu, 20 Feb 2025 12:49:43 +0100 Subject: [PATCH] virsh: Do not format messages twice 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 Reviewed-by: Michal Privoznik --- tools/vsh.c | 27 +++++++++++---------------- tools/vsh.h | 4 +--- 2 files changed, 12 insertions(+), 19 deletions(-) diff --git a/tools/vsh.c b/tools/vsh.c index 5f5e2f281d..1b650bdd9b 100644 --- a/tools/vsh.c +++ b/tools/vsh.c @@ -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'); diff --git a/tools/vsh.h b/tools/vsh.h index 7de9fa2f09..1c7370dd4f 100644 --- a/tools/vsh.h +++ b/tools/vsh.h @@ -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, -- 2.47.3