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

Eric Blake eblake at redhat.com
Tue May 24 17:39:30 UTC 2011


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




More information about the libvir-list mailing list