[gnome-keyring] gkm: Fix GKM_DEBUG environment variable for G_MESSAGES_DEBUG



commit 425394b296ecb3fe67e917ce2c048e45b6fb80ab
Author: Stef Walter <stefw gnome org>
Date:   Tue Jun 19 11:07:07 2012 +0200

    gkm: Fix GKM_DEBUG environment variable for G_MESSAGES_DEBUG
    
    G_MESSAGES_DEBUG is a new environment variable that glib expects
    without which no debug messages are displayed. Since we also have
    GKM_DEBUG we reconcile the two environment variables

 pkcs11/gkm/Makefile.am |    1 +
 pkcs11/gkm/gkm-debug.c |   65 ++++++++++++++++++++++++++++++++++++++++-------
 2 files changed, 56 insertions(+), 10 deletions(-)
---
diff --git a/pkcs11/gkm/Makefile.am b/pkcs11/gkm/Makefile.am
index f0e902c..9928c6b 100644
--- a/pkcs11/gkm/Makefile.am
+++ b/pkcs11/gkm/Makefile.am
@@ -63,6 +63,7 @@ libgkm_la_SOURCES = \
 	$(BUILT_SOURCES)
 
 libgkm_la_CFLAGS = \
+	-DG_LOG_DOMAIN=\"Gkm\" \
 	$(GOBJECT_CFLAGS) \
 	$(LIBGCRYPT_CFLAGS) \
 	$(LIBTASN1_CFLAGS) \
diff --git a/pkcs11/gkm/gkm-debug.c b/pkcs11/gkm/gkm-debug.c
index 7c29bd8..0c45523 100644
--- a/pkcs11/gkm/gkm-debug.c
+++ b/pkcs11/gkm/gkm-debug.c
@@ -60,28 +60,73 @@ gkm_debug_flag_is_set (GkmDebugFlags flag)
 	return (flag & current_flags) != 0;
 }
 
+static void
+on_gkm_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
 gkm_debug_message (GkmDebugFlags 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)) {
-		gkm_debug_set_flags (g_getenv ("GKM_DEBUG"));
+		messages_env = g_getenv ("G_MESSAGES_DEBUG");
+		debug_env = g_getenv ("GKM_DEBUG");
+#ifdef GKM_DEBUG
+		if (debug_env == NULL)
+			debug_env = G_STRINGIFY (GKM_DEBUG);
+#endif
+
+		/*
+		 * If the caller is selectively asking for certain debug
+		 * messages with the GKM_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_gkm_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";
+
+		gkm_debug_set_flags (debug_env);
+
 		g_once_init_leave (&initialized_flags, 1);
 	}
 
-	va_start (args, format);
-	message = g_strdup_vprintf (format, args);
-	va_end (args);
-
-	if (flag & current_flags)
-		g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "%s", message);
-
-	g_free (message);
+	if (flag & current_flags) {
+		va_start (args, format);
+		g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format, args);
+		va_end (args);
+	}
 }
 
 #else /* !WITH_DEBUG */



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