[glib] g_logv: only expand the message once



commit 78a8aecbb339f620e87711389f56308c219d443f
Author: Dan Winship <danw gnome org>
Date:   Mon Jul 30 15:32:31 2012 -0400

    g_logv: only expand the message once
    
    Hoist the g_strdup_printf()'ing out of the loop, since the message is
    the same for every handler that gets called.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=679556

 glib/gmessages.c |   60 +++++++++++++++++++++--------------------------------
 1 files changed, 24 insertions(+), 36 deletions(-)
---
diff --git a/glib/gmessages.c b/glib/gmessages.c
index f32d51c..b1697d6 100644
--- a/glib/gmessages.c
+++ b/glib/gmessages.c
@@ -657,16 +657,30 @@ void
 g_logv (const gchar   *log_domain,
 	GLogLevelFlags log_level,
 	const gchar   *format,
-	va_list	       args1)
+	va_list	       args)
 {
   gboolean was_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
   gboolean was_recursion = (log_level & G_LOG_FLAG_RECURSION) != 0;
+  gchar buffer[1025], *msg, *msg_alloc = NULL;
   gint i;
 
   log_level &= G_LOG_LEVEL_MASK;
   if (!log_level)
     return;
 
+  if (log_level & G_LOG_FLAG_RECURSION)
+    {
+      /* we use a stack buffer of fixed size, since we're likely
+       * in an out-of-memory situation
+       */
+      gsize size G_GNUC_UNUSED;
+
+      size = _g_vsnprintf (buffer, 1024, format, args);
+      msg = buffer;
+    }
+  else
+    msg = msg_alloc = g_strdup_vprintf (format, args);
+
   for (i = g_bit_nth_msf (log_level, -1); i >= 0; i = g_bit_nth_msf (log_level, i))
     {
       register GLogLevelFlags test_level;
@@ -705,42 +719,14 @@ g_logv (const gchar   *log_domain,
 
 	  g_private_set (&g_log_depth, GUINT_TO_POINTER (depth));
 
+          log_func (log_domain, test_level, msg, data);
 
-	  if (test_level & G_LOG_FLAG_RECURSION)
-	    {
-	      /* we use a stack buffer of fixed size, since we're likely
-	       * in an out-of-memory situation
-	       */
-	      gchar buffer[1025];
-              gsize size G_GNUC_UNUSED;
-              va_list args2;
-
-              G_VA_COPY (args2, args1);
-	      size = _g_vsnprintf (buffer, 1024, format, args2);
-              va_end (args2);
-
-	      log_func (log_domain, test_level, buffer, data);
-	    }
-	  else
-	    {
-	      gchar *msg;
-              va_list args2;
-
-              G_VA_COPY (args2, args1);
-              msg = g_strdup_vprintf (format, args2);
-              va_end (args2);
-
-	      log_func (log_domain, test_level, msg, data);
-
-              if ((test_level & G_LOG_FLAG_FATAL)
-                && !(test_level & G_LOG_LEVEL_ERROR))
-                {
-                  masquerade_fatal = fatal_log_func
-                    && !fatal_log_func (log_domain, test_level, msg, fatal_log_data);
-                }
-
-	      g_free (msg);
-	    }
+          if ((test_level & G_LOG_FLAG_FATAL)
+              && !(test_level & G_LOG_LEVEL_ERROR))
+            {
+              masquerade_fatal = fatal_log_func
+                && !fatal_log_func (log_domain, test_level, msg, fatal_log_data);
+            }
 
 	  if ((test_level & G_LOG_FLAG_FATAL) && !masquerade_fatal)
             {
@@ -765,6 +751,8 @@ g_logv (const gchar   *log_domain,
 	  g_private_set (&g_log_depth, GUINT_TO_POINTER (depth));
 	}
     }
+
+  g_free (msg_alloc);
 }
 
 /**



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]