[tracker/libtracker-sparql: 42/44] libtracker-common: Support TRACKER_VERBOSITY environment variable



commit 4b12a214396e345ad6198c8d30de1f12aa922e6a
Author: Jürg Billeter <j bitron ch>
Date:   Tue Aug 10 11:35:13 2010 +0200

    libtracker-common: Support TRACKER_VERBOSITY environment variable

 src/libtracker-common/tracker-log.c |  103 ++++++++++++++++-------------------
 src/libtracker-common/tracker-log.h |    2 -
 src/tracker-extract/tracker-main.c  |   15 -----
 3 files changed, 47 insertions(+), 73 deletions(-)
---
diff --git a/src/libtracker-common/tracker-log.c b/src/libtracker-common/tracker-log.c
index 7a6f7cf..ed96727 100644
--- a/src/libtracker-common/tracker-log.c
+++ b/src/libtracker-common/tracker-log.c
@@ -122,27 +122,39 @@ tracker_log_handler (const gchar    *domain,
                      const gchar    *message,
                      gpointer        user_data)
 {
-	if (!tracker_log_should_handle (log_level, verbosity)) {
-		return;
-	}
-
 	log_output (domain, log_level, message);
 
 	/* Now show the message through stdout/stderr as usual */
 	g_log_default_handler (domain, log_level, message, user_data);
 }
 
+static void
+hide_log_handler (const gchar    *domain,
+                  GLogLevelFlags  log_level,
+                  const gchar    *message,
+                  gpointer        user_data)
+{
+	/* do nothing */
+}
+
 gboolean
 tracker_log_init (gint    this_verbosity,
                   gchar **used_filename)
 {
 	gchar *filename;
 	gchar *basename;
+	const gchar *env_verbosity;
+	GLogLevelFlags hide_levels = 0;
 
 	if (initialized) {
 		return TRUE;
 	}
 
+	env_verbosity = g_getenv ("TRACKER_VERBOSITY");
+	if (env_verbosity != NULL) {
+		this_verbosity = atoi (env_verbosity);
+	}
+
 	basename = g_strdup_printf ("%s.log", g_get_application_name ());
 	filename = g_build_filename (g_get_user_data_dir (),
 	                             "tracker",
@@ -150,7 +162,7 @@ tracker_log_init (gint    this_verbosity,
 	                             NULL);
 	g_free (basename);
 
-	/* Remove previous log */
+	/* hide previous log */
 	g_unlink (filename);
 
 	/* Open file */
@@ -170,12 +182,38 @@ tracker_log_init (gint    this_verbosity,
 	verbosity = CLAMP (this_verbosity, 0, 3);
 	mutex = g_mutex_new ();
 
-	/* Add log handler function */
-	log_handler_id = g_log_set_handler (NULL,
-	                                    G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL,
-	                                    tracker_log_handler,
+	switch (this_verbosity) {
+		/* Log level 3: EVERYTHING */
+	case 3:
+		break;
+
+		/* Log level 2: CRITICAL/ERROR/WARNING/INFO/MESSAGE only */
+	case 2:
+		hide_levels = G_LOG_LEVEL_DEBUG;
+		break;
+
+		/* Log level 1: CRITICAL/ERROR/WARNING/INFO only */
+	case 1:
+		hide_levels = G_LOG_LEVEL_DEBUG |
+		              G_LOG_LEVEL_MESSAGE;
+		break;
+
+		/* Log level 0: CRITICAL/ERROR/WARNING only (default) */
+	default:
+	case 0:
+		hide_levels = G_LOG_LEVEL_DEBUG |
+		              G_LOG_LEVEL_MESSAGE |
+		              G_LOG_LEVEL_INFO;
+		break;
+	}
+
+	/* Hide log levels according to configuration */
+	log_handler_id = g_log_set_handler (G_LOG_DOMAIN,
+	                                    hide_levels,
+	                                    hide_log_handler,
 	                                    NULL);
 
+	/* Set log handler function for the rest */
 	g_log_set_default_handler (tracker_log_handler, NULL);
 
 	if (used_filename) {
@@ -210,50 +248,3 @@ tracker_log_shutdown (void)
 
 	initialized = FALSE;
 }
-
-gboolean
-tracker_log_should_handle (GLogLevelFlags log_level,
-                           gint           this_verbosity)
-{
-	switch (this_verbosity) {
-		/* Log level 3: EVERYTHING */
-	case 3:
-		break;
-
-		/* Log level 2: CRITICAL/ERROR/WARNING/INFO/MESSAGE only */
-	case 2:
-		if (!(log_level & G_LOG_LEVEL_MESSAGE) &&
-		    !(log_level & G_LOG_LEVEL_INFO) &&
-		    !(log_level & G_LOG_LEVEL_WARNING) &&
-		    !(log_level & G_LOG_LEVEL_ERROR) &&
-		    !(log_level & G_LOG_LEVEL_CRITICAL)) {
-			return FALSE;
-		}
-
-		break;
-
-		/* Log level 1: CRITICAL/ERROR/WARNING/INFO only */
-	case 1:
-		if (!(log_level & G_LOG_LEVEL_INFO) &&
-		    !(log_level & G_LOG_LEVEL_WARNING) &&
-		    !(log_level & G_LOG_LEVEL_ERROR) &&
-		    !(log_level & G_LOG_LEVEL_CRITICAL)) {
-			return FALSE;
-		}
-
-		break;
-
-		/* Log level 0: CRITICAL/ERROR/WARNING only (default) */
-	default:
-	case 0:
-		if (!(log_level & G_LOG_LEVEL_WARNING) &&
-		    !(log_level & G_LOG_LEVEL_ERROR) &&
-		    !(log_level & G_LOG_LEVEL_CRITICAL)) {
-			return FALSE;
-		}
-
-		break;
-	}
-
-	return TRUE;
-}
diff --git a/src/libtracker-common/tracker-log.h b/src/libtracker-common/tracker-log.h
index fb4c1b9..d7a4bc7 100644
--- a/src/libtracker-common/tracker-log.h
+++ b/src/libtracker-common/tracker-log.h
@@ -54,8 +54,6 @@ tracker_info (const gchar *format,
 gboolean tracker_log_init          (gint             verbosity,
                                     gchar          **used_filename);
 void     tracker_log_shutdown      (void);
-gboolean tracker_log_should_handle (GLogLevelFlags   log_level,
-                                    gint             verbosity);
 
 G_END_DECLS
 
diff --git a/src/tracker-extract/tracker-main.c b/src/tracker-extract/tracker-main.c
index 558af19..a99a249 100644
--- a/src/tracker-extract/tracker-main.c
+++ b/src/tracker-extract/tracker-main.c
@@ -241,10 +241,6 @@ log_handler (const gchar    *domain,
              const gchar    *message,
              gpointer        user_data)
 {
-	if (!tracker_log_should_handle (log_level, verbosity)) {
-		return;
-	}
-
 	switch (log_level) {
 	case G_LOG_LEVEL_WARNING:
 	case G_LOG_LEVEL_CRITICAL:
@@ -278,14 +274,8 @@ run_standalone (void)
 	TrackerExtract *object;
 	GFile *file;
 	gchar *uri;
-	guint log_handler_id;
 
 	/* Set log handler for library messages */
-	log_handler_id = g_log_set_handler (NULL,
-	                                    G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL,
-	                                    log_handler,
-	                                    NULL);
-
 	g_log_set_default_handler (log_handler, NULL);
 
 	/* Set the default verbosity if unset */
@@ -318,11 +308,6 @@ run_standalone (void)
 	g_object_unref (file);
 	g_free (uri);
 
-	if (log_handler_id != 0) {
-		/* Unset log handler */
-		g_log_remove_handler (NULL, log_handler_id);
-	}
-
 	tracker_albumart_shutdown ();
 
 	return EXIT_SUCCESS;



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