Easier to maintain, and no longer an arbitrary line length limit. * tools/virsh.c (vshOutputLogFile): Replace snprintf with virBuffer. --- > However, I'd prefer if we instead rewrote this function to > use virBuffer instead of snprintf. Much less error-prone to maintain. Like this. tools/virsh.c | 56 ++++++++++++++++++++++++++++++++------------------------ 1 files changed, 32 insertions(+), 24 deletions(-) diff --git a/tools/virsh.c b/tools/virsh.c index b249829..256baec 100644 --- a/tools/virsh.c +++ b/tools/virsh.c @@ -12230,7 +12230,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; @@ -12238,8 +12240,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 * @@ -12247,16 +12247,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; @@ -12277,21 +12275,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); } /** -- 1.7.4.4 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list