[gnome-color-manager] Rework the EggDebug module to be more glibish



commit a7fa4769bd16da3d92a1fee215f8fc8b22f5bc1b
Author: Richard Hughes <richard hughsie com>
Date:   Sun Nov 22 21:52:16 2009 +0000

    Rework the EggDebug module to be more glibish

 src/Makefile.am   |    6 +-
 src/egg-debug.c   |  258 ++++++++++++++++++++++++++++++++++++-----------------
 src/egg-debug.h   |   17 ++--
 src/gcm-apply.c   |    6 +-
 src/gcm-import.c  |    6 +-
 src/gcm-inspect.c |    7 +--
 src/gcm-prefs.c   |    7 +-
 src/gcm-session.c |    5 +-
 8 files changed, 191 insertions(+), 121 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 1f4e0b8..68ab31a 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -14,11 +14,7 @@ INCLUDES =						\
 	-DSYSCONFDIR=\""$(sysconfdir)"\" 		\
 	-DVERSION="\"$(VERSION)\"" 			\
 	-DPACKAGE_LOCALE_DIR=\"$(localedir)\"		\
-	-DGCM_DATA=\"$(pkgdatadir)\"			\
-	-DEGG_LOG_FILE=\""$(GCM_LOG_DIR)/gcm"\"		\
-	-DEGG_VERBOSE="\"GCM_VERBOSE\""			\
-	-DEGG_LOGGING="\"GCM_LOGGING\""			\
-	-DEGG_CONSOLE="\"GCM_CONSOLE\""
+	-DGCM_DATA=\"$(pkgdatadir)\"
 
 noinst_LIBRARIES = libgcmshared.a
 libgcmshared_a_SOURCES =				\
diff --git a/src/egg-debug.c b/src/egg-debug.c
index dff185e..ed3fe65 100644
--- a/src/egg-debug.c
+++ b/src/egg-debug.c
@@ -1,6 +1,6 @@
 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
  *
- * Copyright (C) 2007-2008 Richard Hughes <richard hughsie com>
+ * Copyright (C) 2007-2009 Richard Hughes <richard hughsie com>
  *
  * Licensed under the GNU General Public License Version 2
  *
@@ -58,18 +58,73 @@
 #define CONSOLE_CYAN		36
 #define CONSOLE_WHITE		37
 
-static gint fd = -1;
+static gint _fd = -1;
+static gboolean _verbose = FALSE;
+static gboolean _console = FALSE;
+static gchar *_debug_filename = NULL;
+static gboolean _initialized = FALSE;
+static gchar **_modules = NULL;
+static gchar **_functions = NULL;
 
 /**
- * pk_set_console_mode:
+ * egg_debug_filter_module:
+ **/
+static gboolean
+egg_debug_filter_module (const gchar *filename)
+{
+	gchar *module;
+	guint i;
+	gboolean ret = FALSE;
+
+	/* nothing filtering */
+	if (_modules == NULL)
+		return FALSE;
+
+	/* are we in the filter list */
+	module = g_strdup (filename);
+	g_strdelimit (module, ".", '\0');
+	for (i=0; _modules[i] != NULL; i++) {
+		if (g_strcmp0 (_modules[i], module) == 0) {
+			ret = TRUE;
+			break;
+		}
+	}
+	return ret;
+}
+
+/**
+ * egg_debug_filter_function:
+ **/
+static gboolean
+egg_debug_filter_function (const gchar *function)
+{
+	guint i;
+	gboolean ret = FALSE;
+
+	/* nothing filtering */
+	if (_functions == NULL)
+		return FALSE;
+
+	/* are we in the filter list */
+	for (i=0; _functions[i] != NULL; i++) {
+		if (g_str_has_prefix (function, _functions[i])) {
+			ret = TRUE;
+			break;
+		}
+	}
+	return ret;
+}
+
+/**
+ * egg_debug_set_console_mode:
  **/
 static void
-pk_set_console_mode (guint console_code)
+egg_debug_set_console_mode (guint console_code)
 {
 	gchar command[13];
 
 	/* don't put extra commands into logs */
-	if (!egg_debug_is_console ())
+	if (!_console)
 		return;
 
 	/* Command is the control command to the terminal */
@@ -92,48 +147,50 @@ egg_debug_backtrace (void)
 	call_stack_size = backtrace (call_stack, G_N_ELEMENTS (call_stack));
 	symbols = backtrace_symbols (call_stack, call_stack_size);
 	if (symbols != NULL) {
-		pk_set_console_mode (CONSOLE_RED);
+		egg_debug_set_console_mode (CONSOLE_RED);
 		g_print ("Traceback:\n");
 		while (i < call_stack_size) {
 			g_print ("\t%s\n", symbols[i]);
 			i++;
 		}
-		pk_set_console_mode (CONSOLE_RESET);
+		egg_debug_set_console_mode (CONSOLE_RESET);
 		free (symbols);
 	}
 #endif
 }
 
 /**
- * pk_log_line:
+ * egg_debug_log_line:
  **/
 static void
-pk_log_line (const gchar *buffer)
+egg_debug_log_line (const gchar *buffer)
 {
 	ssize_t count;
+
 	/* open a file */
-	if (fd == -1) {
+	if (_fd == -1) {
 		/* ITS4: ignore, /var/log/foo is owned by root, and this is just debug text */
-		fd = open (EGG_LOG_FILE, O_WRONLY|O_APPEND|O_CREAT, 0777);
-		if (fd == -1)
-			g_error ("could not open log: '%s'", EGG_LOG_FILE);
+		_fd = open (_debug_filename, O_WRONLY|O_APPEND|O_CREAT, 0777);
+		if (_fd == -1)
+			g_error ("could not open log: '%s'", _debug_filename);
 	}
 
 	/* ITS4: ignore, debug text always NULL terminated */
-	count = write (fd, buffer, strlen (buffer));
+	count = write (_fd, buffer, strlen (buffer));
 	if (count == -1)
 		g_warning ("could not write %s", buffer);
+
 	/* newline */
-	count = write (fd, "\n", 1);
+	count = write (_fd, "\n", 1);
 	if (count == -1)
 		g_warning ("could not write newline");
 }
 
 /**
- * pk_print_line:
+ * egg_debug_print_line:
  **/
 static void
-pk_print_line (const gchar *func, const gchar *file, const int line, const gchar *buffer, guint color)
+egg_debug_print_line (const gchar *func, const gchar *file, const int line, const gchar *buffer, guint color)
 {
 	gchar *str_time;
 	gchar *header;
@@ -148,18 +205,18 @@ pk_print_line (const gchar *func, const gchar *file, const int line, const gchar
 	g_free (str_time);
 
 	/* always in light green */
-	pk_set_console_mode (CONSOLE_GREEN);
+	egg_debug_set_console_mode (CONSOLE_GREEN);
 	printf ("%s\n", header);
 
 	/* different colors according to the severity */
-	pk_set_console_mode (color);
+	egg_debug_set_console_mode (color);
 	printf (" - %s\n", buffer);
-	pk_set_console_mode (CONSOLE_RESET);
+	egg_debug_set_console_mode (CONSOLE_RESET);
 
 	/* log to a file */
-	if (egg_debug_is_logging ()) {
-		pk_log_line (header);
-		pk_log_line (buffer);
+	if (_debug_filename != NULL) {
+		egg_debug_log_line (header);
+		egg_debug_log_line (buffer);
 	}
 
 	/* flush this output, as we need to debug */
@@ -177,16 +234,16 @@ egg_debug_real (const gchar *func, const gchar *file, const int line, const gcha
 	va_list args;
 	gchar *buffer = NULL;
 
-	if (!egg_debug_enabled ())
+	if (!_verbose && !egg_debug_filter_module (file) && !egg_debug_filter_function (func))
 		return;
 
 	va_start (args, format);
 	g_vasprintf (&buffer, format, args);
 	va_end (args);
 
-	pk_print_line (func, file, line, buffer, CONSOLE_BLUE);
+	egg_debug_print_line (func, file, line, buffer, CONSOLE_BLUE);
 
-	g_free(buffer);
+	g_free (buffer);
 }
 
 /**
@@ -198,7 +255,7 @@ egg_warning_real (const gchar *func, const gchar *file, const int line, const gc
 	va_list args;
 	gchar *buffer = NULL;
 
-	if (!egg_debug_enabled ())
+	if (!_verbose && !egg_debug_filter_module (file) && !egg_debug_filter_function (func))
 		return;
 
 	va_start (args, format);
@@ -206,11 +263,11 @@ egg_warning_real (const gchar *func, const gchar *file, const int line, const gc
 	va_end (args);
 
 	/* do extra stuff for a warning */
-	if (!egg_debug_is_console ())
+	if (!_console)
 		printf ("*** WARNING ***\n");
-	pk_print_line (func, file, line, buffer, CONSOLE_RED);
+	egg_debug_print_line (func, file, line, buffer, CONSOLE_RED);
 
-	g_free(buffer);
+	g_free (buffer);
 }
 
 /**
@@ -227,10 +284,10 @@ egg_error_real (const gchar *func, const gchar *file, const int line, const gcha
 	va_end (args);
 
 	/* do extra stuff for a warning */
-	if (!egg_debug_is_console ())
+	if (!_console)
 		printf ("*** ERROR ***\n");
-	pk_print_line (func, file, line, buffer, CONSOLE_RED);
-	g_free(buffer);
+	egg_debug_print_line (func, file, line, buffer, CONSOLE_RED);
+	g_free (buffer);
 
 	/* we want to fix this! */
 	egg_debug_backtrace ();
@@ -239,75 +296,112 @@ egg_error_real (const gchar *func, const gchar *file, const int line, const gcha
 }
 
 /**
- * egg_debug_enabled:
+ * egg_debug_is_verbose:
  *
  * Returns: TRUE if we have debugging enabled
  **/
 gboolean
-egg_debug_enabled (void)
+egg_debug_is_verbose (void)
 {
-	const gchar *env;
-	env = g_getenv (EGG_VERBOSE);
-	return (g_strcmp0 (env, "1") == 0);
+	return _verbose;
 }
 
 /**
- * egg_debug_is_logging:
+ * egg_debug_init:
+ * @argc: a pointer to the number of command line arguments.
+ * @argv: a pointer to the array of command line arguments.
  *
- * Returns: TRUE if we have logging enabled
- **/
-gboolean
-egg_debug_is_logging (void)
-{
-	const gchar *env;
-	env = g_getenv (EGG_LOGGING);
-	return (g_strcmp0 (env, "1") == 0);
-}
-
-/**
- * egg_debug_is_console:
+ * Parses command line arguments.
  *
- * Returns: TRUE if we have debugging enabled
+ * Any arguments used are removed from the array and
+ * @argc and @argv are updated accordingly.
+ *
+ * Return value: %TRUE if initialization succeeded, otherwise %FALSE.
  **/
 gboolean
-egg_debug_is_console (void)
+egg_debug_init (gint *argc, gchar ***argv)
 {
-	const gchar *env;
-	env = g_getenv (EGG_CONSOLE);
-	return (g_strcmp0 (env, "1") == 0);
+	GOptionContext *option_context;
+	GOptionGroup *group;
+	GError *error = NULL;
+	gboolean verbose = FALSE;
+	gchar *debug_filename = NULL;
+	gchar **modules = NULL;
+	gchar **functions = NULL;
+	const GOptionEntry main_entries[] = {
+		{ "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
+		  /* TRANSLATORS: turn on all debugging */
+		  _("Show debugging information for all files"), NULL },
+		{ NULL}
+	};
+	const GOptionEntry debug_entries[] = {
+		{ "debug-modules", '\0', 0, G_OPTION_ARG_STRING_ARRAY, &modules,
+		  /* TRANSLATORS: a list of modules to debug */
+		  _("Debug these specific modules"), NULL },
+		{ "debug-functions", '\0', 0, G_OPTION_ARG_STRING_ARRAY, &functions,
+		  /* TRANSLATORS: a list of functions to debug */
+		  _("Debug these specific functions"), NULL },
+		{ "debug-filename", '\0', 0, G_OPTION_ARG_STRING, &debug_filename,
+		  /* TRANSLATORS: save to a log */
+		  _("Log debugging data to a file"), NULL },
+		{ NULL}
+	};
+
+	/* already initialized */
+	if (_initialized)
+		return TRUE;
+
+	option_context = g_option_context_new (NULL);
+	g_option_context_set_ignore_unknown_options (option_context, TRUE);
+	g_option_context_set_help_enabled (option_context, TRUE);
+
+	/* create a new group */
+	group = g_option_group_new ("debug", "Detailed debugging", "Show all debugging options", NULL, NULL);
+	g_option_group_add_entries (group, debug_entries);
+
+	/* only add one main entry */
+	g_option_context_add_main_entries (option_context, main_entries, NULL);
+	g_option_context_add_group (option_context, group);
+	if (!g_option_context_parse (option_context, argc, argv, &error)) {
+		g_warning ("%s", error->message);
+		g_error_free (error);
+	}
+
+	/* set options */
+	_debug_filename = g_strdup (debug_filename);
+	_verbose = verbose;
+	_initialized = TRUE;
+	_modules = g_strdupv (modules);
+	_functions = g_strdupv (functions);
+	_console = (isatty (fileno (stdout)) == 1);
+	egg_debug ("Verbose debugging %i (on console %i)", _verbose, _console);
+
+	g_option_context_free (option_context);
+	g_free (debug_filename);
+	g_strfreev (modules);
+	g_strfreev (functions);
+	return TRUE;
 }
 
 /**
- * egg_debug_set_logging:
+ * egg_debug_free:
  **/
 void
-egg_debug_set_logging (gboolean enabled)
+egg_debug_free (void)
 {
-	if (enabled)
-		g_setenv (EGG_LOGGING, "1", TRUE);
-	else
-		g_setenv (EGG_LOGGING, "0", TRUE);
+	if (!_initialized)
+		return;
 
-	if (egg_debug_is_logging ())
-		egg_debug ("logging to %s", EGG_LOG_FILE);
-}
+	/* close file */
+	if (_fd != -1)
+		close (_fd);
 
-/**
- * egg_debug_init:
- * @debug: If we should print out verbose logging
- **/
-void
-egg_debug_init (gboolean debug)
-{
-	/* check if we are on console */
-	if (isatty (fileno (stdout)) == 1)
-		g_setenv (EGG_CONSOLE, "1", FALSE);
-	else
-		g_setenv (EGG_CONSOLE, "0", FALSE);
-	if (debug)
-		g_setenv (EGG_VERBOSE, "1", FALSE);
-	else
-		g_setenv (EGG_VERBOSE, "0", FALSE);
-	egg_debug ("Verbose debugging %i (on console %i)%s", egg_debug_enabled (), egg_debug_is_console (), EGG_VERBOSE);
+	/* free memory */
+	g_free (_debug_filename);
+	g_strfreev (_modules);
+	g_strfreev (_functions);
+
+	/* can not re-init */
+	_initialized = FALSE;
 }
 
diff --git a/src/egg-debug.h b/src/egg-debug.h
index c935dcb..a6524f0 100644
--- a/src/egg-debug.h
+++ b/src/egg-debug.h
@@ -1,6 +1,6 @@
 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
  *
- * Copyright (C) 2007-2008 Richard Hughes <richard hughsie com>
+ * Copyright (C) 2007-2009 Richard Hughes <richard hughsie com>
  *
  * Licensed under the GNU General Public License Version 2
  *
@@ -59,23 +59,22 @@ G_BEGIN_DECLS
 #define egg_error(...)
 #endif
 
-void		egg_debug_init			(gboolean	 debug);
-void		egg_debug_set_logging		(gboolean	 enabled);
-gboolean	egg_debug_enabled		(void);
-gboolean	egg_debug_is_logging		(void);
-gboolean	egg_debug_is_console		(void);
+gboolean	egg_debug_init			(gint		*argc,
+						 gchar		***argv);
+void		egg_debug_free			(void);
+gboolean	egg_debug_is_verbose		(void);
 void		egg_debug_backtrace		(void);
 void		egg_debug_real			(const gchar	*func,
 						 const gchar	*file,
-						 int		 line,
+						 gint		 line,
 						 const gchar	*format, ...) __attribute__((format (printf,4,5)));
 void		egg_warning_real		(const gchar	*func,
 						 const gchar	*file,
-						 int		 line,
+						 gint		 line,
 						 const gchar	*format, ...) __attribute__((format (printf,4,5)));
 void		egg_error_real			(const gchar	*func,
 						 const gchar	*file,
-						 int		 line,
+						 gint		 line,
 						 const gchar	*format, ...) G_GNUC_NORETURN __attribute__((format (printf,4,5)));
 
 G_END_DECLS
diff --git a/src/gcm-apply.c b/src/gcm-apply.c
index a7de7f3..b2fc5cc 100644
--- a/src/gcm-apply.c
+++ b/src/gcm-apply.c
@@ -35,7 +35,6 @@ int
 main (int argc, char **argv)
 {
 	gboolean ret;
-	gboolean verbose = FALSE;
 	guint retval = 0;
 	GError *error = NULL;
 	GOptionContext *context;
@@ -46,20 +45,17 @@ main (int argc, char **argv)
 	GcmDeviceType type;
 
 	const GOptionEntry options[] = {
-		{ "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
-			"Show extra debugging information", NULL },
 		{ NULL}
 	};
 
 	gtk_init (&argc, &argv);
+	egg_debug_init (&argc, &argv);
 
 	context = g_option_context_new ("gnome-color-manager apply program");
 	g_option_context_add_main_entries (context, options, NULL);
 	g_option_context_parse (context, &argc, &argv, NULL);
 	g_option_context_free (context);
 
-	egg_debug_init (verbose);
-
 	/* get devices */
 	client = gcm_client_new ();
 	ret = gcm_client_add_connected (client, &error);
diff --git a/src/gcm-import.c b/src/gcm-import.c
index 8a798a9..8ecbe0a 100644
--- a/src/gcm-import.c
+++ b/src/gcm-import.c
@@ -35,7 +35,6 @@ int
 main (int argc, char **argv)
 {
 	gboolean ret;
-	gboolean verbose = FALSE;
 	gchar *copyright = NULL;
 	gchar *description = NULL;
 	gchar *destination = NULL;
@@ -49,8 +48,6 @@ main (int argc, char **argv)
 	GtkResponseType response;
 
 	const GOptionEntry options[] = {
-		{ "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
-			"Show extra debugging information", NULL },
 		{ G_OPTION_REMAINING, '\0', 0, G_OPTION_ARG_FILENAME_ARRAY, &files,
 		  /* TRANSLATORS: command line option: a list of catalogs to install */
 		  _("ICC profile to install"), NULL },
@@ -58,14 +55,13 @@ main (int argc, char **argv)
 	};
 
 	gtk_init (&argc, &argv);
+	egg_debug_init (&argc, &argv);
 
 	context = g_option_context_new ("gnome-color-manager import program");
 	g_option_context_add_main_entries (context, options, NULL);
 	g_option_context_parse (context, &argc, &argv, NULL);
 	g_option_context_free (context);
 
-	egg_debug_init (verbose);
-
 	/* nothing sent */
 	if (files == NULL) {
 		/* TRANSLATORS: nothing was specified on the command line */
diff --git a/src/gcm-inspect.c b/src/gcm-inspect.c
index 5482726..6dcb4a6 100644
--- a/src/gcm-inspect.c
+++ b/src/gcm-inspect.c
@@ -208,7 +208,6 @@ out:
 int
 main (int argc, char **argv)
 {
-	gboolean verbose = FALSE;
 	gboolean x11 = FALSE;
 	gboolean dump = FALSE;
 	gchar *sysfs_path = NULL;
@@ -216,9 +215,6 @@ main (int argc, char **argv)
 	GOptionContext *context;
 
 	const GOptionEntry options[] = {
-		{ "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
-			/* TRANSLATORS: command line option */
-			_("Show extra debugging information"), NULL },
 		{ "x11", 'x', 0, G_OPTION_ARG_NONE, &x11,
 			/* TRANSLATORS: command line option */
 			_("Show X11 properties"), NULL },
@@ -232,14 +228,13 @@ main (int argc, char **argv)
 	};
 
 	gtk_init (&argc, &argv);
+	egg_debug_init (&argc, &argv);
 
 	context = g_option_context_new ("gnome-color-manager inspect program");
 	g_option_context_add_main_entries (context, options, NULL);
 	g_option_context_parse (context, &argc, &argv, NULL);
 	g_option_context_free (context);
 
-	egg_debug_init (verbose);
-
 	if (x11 || dump)
 		gcm_inspect_show_x11_atoms ();
 	if (sysfs_path != NULL)
diff --git a/src/gcm-prefs.c b/src/gcm-prefs.c
index 440afa5..994f480 100644
--- a/src/gcm-prefs.c
+++ b/src/gcm-prefs.c
@@ -1519,7 +1519,6 @@ gcm_prefs_radio_cb (GtkWidget *widget, gpointer user_data)
 int
 main (int argc, char **argv)
 {
-	gboolean verbose = FALSE;
 	guint retval = 0;
 	GOptionContext *context;
 	GtkWidget *main_window;
@@ -1535,8 +1534,6 @@ main (int argc, char **argv)
 	GtkWidget *info_bar_label;
 
 	const GOptionEntry options[] = {
-		{ "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
-		  _("Show extra debugging information"), NULL },
 		{ "parent-window", 'p', 0, G_OPTION_ARG_INT, &xid,
 		  /* TRANSLATORS: we can make this modal (stay on top of) another window */
 		  _("Set the parent window to make this modal"), NULL },
@@ -1544,14 +1541,13 @@ main (int argc, char **argv)
 	};
 
 	gtk_init (&argc, &argv);
+	egg_debug_init (&argc, &argv);
 
 	context = g_option_context_new ("gnome-color-manager prefs program");
 	g_option_context_add_main_entries (context, options, NULL);
 	g_option_context_parse (context, &argc, &argv, NULL);
 	g_option_context_free (context);
 
-	egg_debug_init (verbose);
-
 	/* block in a loop */
 	loop = g_main_loop_new (NULL, FALSE);
 
@@ -1770,6 +1766,7 @@ out:
 		g_ptr_array_unref (profiles_array_in_combo);
 	if (gcm_client != NULL)
 		g_object_unref (gcm_client);
+	egg_debug_free ();
 	return retval;
 }
 
diff --git a/src/gcm-session.c b/src/gcm-session.c
index 060605e..0093123 100644
--- a/src/gcm-session.c
+++ b/src/gcm-session.c
@@ -110,7 +110,6 @@ gcm_session_check_idle_cb (GcmDbus *dbus)
 int
 main (int argc, char *argv[])
 {
-	gboolean verbose = FALSE;
 	gboolean no_timed_exit = FALSE;
 	GcmDbus *dbus = NULL;
 	GOptionContext *context;
@@ -120,8 +119,6 @@ main (int argc, char *argv[])
 	DBusGConnection *connection;
 
 	const GOptionEntry options[] = {
-		{ "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
-		  _("Show extra debugging information"), NULL },
 		{ "no-timed-exit", '\0', 0, G_OPTION_ARG_NONE, &no_timed_exit,
 		  _("Do not exit after the request has been processed"), NULL },
 		{ NULL}
@@ -140,7 +137,7 @@ main (int argc, char *argv[])
 	g_option_context_parse (context, &argc, &argv, NULL);
 	g_option_context_free (context);
 
-	egg_debug_init (verbose);
+	egg_debug_init (&argc, &argv);
 	gtk_init (&argc, &argv);
 
 	/* create new objects */



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