[libvirt] [PATCH] build: fix VIR_DEBUG on mingw

We don't use the gnulib vsnprintf replacement, which means that on mingw, vsnprintf doesn't support %sn. But as it turns out, VIR_GET_VAR_STR was a rather inefficient reimplementation of virVasprintf. * src/util/logging.c (VIR_GET_VAR_STR): Drop. (virLogMessage): Inline a simpler version here. * src/util/virterror.c (VIR_GET_VAR_STR, virRaiseErrorFull): Likewise. Reported by Matthias Bolte. --- src/util/logging.c | 45 ++++++--------------------------------------- src/util/virterror.c | 44 +++++--------------------------------------- 2 files changed, 11 insertions(+), 78 deletions(-) diff --git a/src/util/logging.c b/src/util/logging.c index 823e506..c86fcda 100644 --- a/src/util/logging.c +++ b/src/util/logging.c @@ -47,43 +47,6 @@ #define VIR_FROM_THIS VIR_FROM_NONE /* - * Macro used to format the message as a string in virLogMessage - * and borrowed from libxml2 (also used in virRaiseError) - */ -#define VIR_GET_VAR_STR(msg, str) { \ - int size, prev_size = -1; \ - int chars; \ - char *larger; \ - va_list ap; \ - \ - str = (char *) malloc(150); \ - if (str != NULL) { \ - \ - size = 150; \ - \ - while (1) { \ - va_start(ap, msg); \ - chars = vsnprintf(str, size, msg, ap); \ - va_end(ap); \ - if ((chars > -1) && (chars < size)) { \ - if (prev_size == chars) { \ - break; \ - } else { \ - prev_size = chars; \ - } \ - } \ - if (chars > -1) \ - size += chars + 1; \ - else \ - size += 100; \ - if ((larger = (char *) realloc(str, size)) == NULL) { \ - break; \ - } \ - str = larger; \ - }} \ -} - -/* * A logging buffer to keep some history over logs */ @@ -729,6 +692,7 @@ void virLogMessage(const char *category, int priority, const char *funcname, int len, fprio, i, ret; int saved_errno = errno; int emit = 1; + va_list ap; if (!virLogInitialized) virLogStartup(); @@ -753,9 +717,12 @@ void virLogMessage(const char *category, int priority, const char *funcname, /* * serialize the error message, add level and timestamp */ - VIR_GET_VAR_STR(fmt, str); - if (str == NULL) + va_start(ap, fmt); + if (virVasprintf(&str, fmt, ap) < 0) { + va_end(ap); goto cleanup; + } + va_end(ap); gettimeofday(&cur_time, NULL); localtime_r(&cur_time.tv_sec, &time_info); diff --git a/src/util/virterror.c b/src/util/virterror.c index 2d7309a..852ff9a 100644 --- a/src/util/virterror.c +++ b/src/util/virterror.c @@ -28,43 +28,6 @@ virErrorFunc virErrorHandler = NULL; /* global error handler */ void *virUserData = NULL; /* associated data */ virErrorLogPriorityFunc virErrorLogPriorityFilter = NULL; -/* - * Macro used to format the message as a string in virRaiseError - * and borrowed from libxml2. - */ -#define VIR_GET_VAR_STR(msg, str) { \ - int size, prev_size = -1; \ - int chars; \ - char *larger; \ - va_list ap; \ - \ - str = (char *) malloc(150); \ - if (str != NULL) { \ - \ - size = 150; \ - \ - while (1) { \ - va_start(ap, msg); \ - chars = vsnprintf(str, size, msg, ap); \ - va_end(ap); \ - if ((chars > -1) && (chars < size)) { \ - if (prev_size == chars) { \ - break; \ - } else { \ - prev_size = chars; \ - } \ - } \ - if (chars > -1) \ - size += chars + 1; \ - else \ - size += 100; \ - if ((larger = (char *) realloc(str, size)) == NULL) { \ - break; \ - } \ - str = larger; \ - }} \ -} - static virLogPriority virErrorLevelPriority(virErrorLevel level) { switch (level) { case VIR_ERR_NONE: @@ -718,12 +681,15 @@ virRaiseErrorFull(const char *filename ATTRIBUTE_UNUSED, } /* - * formats the message + * formats the message; drop message on OOM situations */ if (fmt == NULL) { str = strdup(_("No error message provided")); } else { - VIR_GET_VAR_STR(fmt, str); + va_list ap; + va_start(ap, fmt); + virVasprintf(&str, fmt, ap); + va_end(ap); } /* -- 1.7.4.4

2011/5/24 Eric Blake <eblake@redhat.com>:
We don't use the gnulib vsnprintf replacement, which means that on mingw, vsnprintf doesn't support %sn.
But as it turns out, VIR_GET_VAR_STR was a rather inefficient reimplementation of virVasprintf.
* src/util/logging.c (VIR_GET_VAR_STR): Drop. (virLogMessage): Inline a simpler version here. * src/util/virterror.c (VIR_GET_VAR_STR, virRaiseErrorFull): Likewise. Reported by Matthias Bolte. --- src/util/logging.c | 45 ++++++--------------------------------------- src/util/virterror.c | 44 +++++--------------------------------------- 2 files changed, 11 insertions(+), 78 deletions(-)
ACK. This actually fixed a debug output that confused me. Before this patch the output claimed 19:14:15.365: 2140: debug : virEventPollCalculateTimeout:345 : Timeout at 0 due in 0 ms But this 0 0 case is impossible from the logic in the code. After the patch it is reporting correctly 20:15:25.157: 2140: debug : virEventPollCalculateTimeout:345 : Timeout at 0 due in -1 ms Matthias

On 05/24/2011 12:33 PM, Matthias Bolte wrote:
2011/5/24 Eric Blake <eblake@redhat.com>:
We don't use the gnulib vsnprintf replacement, which means that on mingw, vsnprintf doesn't support %sn.
I meant %zn, not %sn; and I'll also mention %llu which is another one mingw doesn't natively know.
But as it turns out, VIR_GET_VAR_STR was a rather inefficient reimplementation of virVasprintf.
* src/util/logging.c (VIR_GET_VAR_STR): Drop. (virLogMessage): Inline a simpler version here. * src/util/virterror.c (VIR_GET_VAR_STR, virRaiseErrorFull): Likewise. Reported by Matthias Bolte. --- src/util/logging.c | 45 ++++++--------------------------------------- src/util/virterror.c | 44 +++++--------------------------------------- 2 files changed, 11 insertions(+), 78 deletions(-)
ACK.
Thanks; pushed.
This actually fixed a debug output that confused me. Before this patch the output claimed
19:14:15.365: 2140: debug : virEventPollCalculateTimeout:345 : Timeout at 0 due in 0 ms
But this 0 0 case is impossible from the logic in the code. After the patch it is reporting correctly
20:15:25.157: 2140: debug : virEventPollCalculateTimeout:345 : Timeout at 0 due in -1 ms
Cool. That probably means that mingw treated %llu as %lu and only parsed 32-bits of a 64-bit stack entity, leaving the %d for the remaining 32 bits; whereas post-patch you get the right argument mapping. -- Eric Blake eblake@redhat.com +1-801-349-2682 Libvirt virtualization library http://libvirt.org
participants (2)
-
Eric Blake
-
Matthias Bolte