[libgnome-keyring/gnome-3-4] Fixes for G_MESSAGES_DEBUG



commit bacf26db8098cd50059122ad0086b9257f8a5a34
Author: Stef Walter <stefw gnome org>
Date:   Sat Jun 23 16:08:58 2012 +0200

    Fixes for G_MESSAGES_DEBUG
    
     * Respect G_MESSAGES_DEBUG when set
     * Make GKR_DEBUG work again even with glib versions that use
       G_MESSAGES_DEBUG

 library/gkr-debug.c |   79 +++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 64 insertions(+), 15 deletions(-)
---
diff --git a/library/gkr-debug.c b/library/gkr-debug.c
index 0b36dbc..422865d 100644
--- a/library/gkr-debug.c
+++ b/library/gkr-debug.c
@@ -33,7 +33,7 @@
 
 #ifdef WITH_DEBUG
 
-#if FOR_WHEN_ALL_ELSE_FAILS
+#ifdef FOR_WHEN_ALL_ELSE_FAILS
 #include <syslog.h>
 #endif
 
@@ -68,34 +68,83 @@ gkr_debug_flag_is_set (GkrDebugFlags flag)
 	return (flag & current_flags) != 0;
 }
 
+static void
+on_gkr_log_debug (const gchar *log_domain,
+                  GLogLevelFlags log_level,
+                  const gchar *message,
+                  gpointer user_data)
+{
+	GString *gstring;
+	const gchar *progname;
+
+	gstring = g_string_new (NULL);
+
+	progname = g_get_prgname ();
+	g_string_append_printf (gstring, "(%s:%lu): %s-DEBUG: %s\n",
+	                        progname ? progname : "process",
+	                        (gulong)getpid (), log_domain,
+	                        message ? message : "(NULL) message");
+
+	write (1, gstring->str, gstring->len);
+	g_string_free (gstring, TRUE);
+}
+
 void
-gkr_debug_message (GkrDebugFlags flag, const gchar *format, ...)
+gkr_debug_message (GkrDebugFlags flag,
+                   const gchar *format,
+                   ...)
 {
 	static gsize initialized_flags = 0;
-	gchar *message;
+	const gchar *messages_env;
+	const gchar *debug_env;
 	va_list args;
 
 	if (g_once_init_enter (&initialized_flags)) {
-		gkr_debug_set_flags (g_getenv ("GKR_DEBUG"));
-		g_once_init_leave (&initialized_flags, 1);
-
-#if FOR_WHEN_ALL_ELSE_FAILS
+		messages_env = g_getenv ("G_MESSAGES_DEBUG");
+		debug_env = g_getenv ("GKR_DEBUG");
+#ifdef GKR_DEBUG
+		if (debug_env == NULL)
+			debug_env = G_STRINGIFY (GKR_DEBUG);
+#endif
+		/*
+		 * If the caller is selectively asking for certain debug
+		 * messages with the GKR_DEBUG environment variable, then
+		 * we install our own output handler and only print those
+		 * messages. This happens irrespective of G_MESSAGES_DEBUG
+		 */
+		if (messages_env == NULL && debug_env != NULL)
+			g_log_set_handler (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,
+			                   on_gkr_log_debug, NULL);
+
+		/*
+		 * If the caller is using G_MESSAGES_DEBUG then we enable
+		 * all our debug messages, and let Glib filter which ones
+		 * to display.
+		 */
+		if (messages_env != NULL && debug_env == NULL)
+			debug_env = "all";
+
+		gkr_debug_set_flags (debug_env);
+
+#ifdef FOR_WHEN_ALL_ELSE_FAILS
 		openlog ("libgnome-keyring", LOG_NDELAY | LOG_PID, LOG_AUTHPRIV);
 		gkr_debug_set_flags ("all");
 #endif
+
+		g_once_init_leave (&initialized_flags, 1);
 	}
 
+	if (flag & current_flags) {
+		va_start (args, format);
+		g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format, args);
+		va_end (args);
+	}
+
+#ifdef FOR_WHEN_ALL_ELSE_FAILS
 	va_start (args, format);
-	message = g_strdup_vprintf (format, args);
+	vsyslog (LOG_ERR, format, args);
 	va_end (args);
-
-	if (flag & current_flags)
-		g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "%s", message);
-
-#if FOR_WHEN_ALL_ELSE_FAILS
-	syslog (LOG_ERR, "%s", message);
 #endif
-	g_free (message);
 }
 
 #else /* !WITH_DEBUG */



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