[tracker/rss-enclosures] libtracker-sparql: Use TRACKER_SPARQL_VERBOSITY to control logging



commit 7604844b09d6e6c72444a5311f762c5917987708
Author: Martyn Russell <martyn lanedo com>
Date:   Wed Jul 21 11:32:46 2010 +0100

    libtracker-sparql: Use TRACKER_SPARQL_VERBOSITY to control logging
    
    By default it is 0 unless set to another value to avoid spamming for tracker-utils

 src/libtracker-sparql/tracker-connection.vala |   67 +++++++++++++++++++++++++
 1 files changed, 67 insertions(+), 0 deletions(-)
---
diff --git a/src/libtracker-sparql/tracker-connection.vala b/src/libtracker-sparql/tracker-connection.vala
index 4f97126..8ea94d7 100644
--- a/src/libtracker-sparql/tracker-connection.vala
+++ b/src/libtracker-sparql/tracker-connection.vala
@@ -29,12 +29,15 @@ public const string TRACKER_DBUS_OBJECT_STEROIDS = "/org/freedesktop/Tracker1/St
 public abstract class Tracker.Sparql.Connection : Object {
 	static bool direct_only;
 	static weak Connection? singleton;
+	static int verbosity = 0;
 
 	public static Connection get () throws GLib.Error {
 		if (singleton != null) {
 			assert (!direct_only);
 			return singleton;
 		} else {
+			log_init ();
+			
 			var result = new PluginLoader ();
 			singleton = result;
 			result.add_weak_pointer ((void**) (&singleton));
@@ -47,6 +50,8 @@ public abstract class Tracker.Sparql.Connection : Object {
 			assert (direct_only);
 			return singleton;
 		} else {
+			log_init ();
+			
 			var result = new PluginLoader (true /* direct_only */);
 			direct_only = true;
 			singleton = result;
@@ -55,6 +60,68 @@ public abstract class Tracker.Sparql.Connection : Object {
 		}
 	}
 
+	private static void log_init () {
+		// Avoid debug messages
+		string env_verbosity = Environment.get_variable ("TRACKER_SPARQL_VERBOSITY");
+		if (env_verbosity != null)
+			verbosity = env_verbosity.to_int ();
+	
+		GLib.Log.set_handler (null, LogLevelFlags.LEVEL_MASK | LogLevelFlags.FLAG_FATAL, log_handler);
+		GLib.Log.set_default_handler (log_handler);
+	}
+
+	private static bool log_should_handle (LogLevelFlags log_level) {
+		switch (verbosity) {
+		// Log level 3: EVERYTHING
+		case 3:
+			break;
+
+		// Log level 2: CRITICAL/ERROR/WARNING/INFO/MESSAGE only
+		case 2:
+			if (!(LogLevelFlags.LEVEL_MESSAGE in log_level) &&
+				!(LogLevelFlags.LEVEL_INFO in log_level) &&
+				!(LogLevelFlags.LEVEL_WARNING in log_level) &&
+				!(LogLevelFlags.LEVEL_ERROR in log_level) &&
+				!(LogLevelFlags.LEVEL_CRITICAL in log_level)) {
+				return false;
+			}
+
+			break;
+
+		// Log level 1: CRITICAL/ERROR/WARNING/INFO only
+		case 1:
+			if (!(LogLevelFlags.LEVEL_INFO in log_level) &&
+				!(LogLevelFlags.LEVEL_WARNING in log_level) &&
+				!(LogLevelFlags.LEVEL_ERROR in log_level) &&
+				!(LogLevelFlags.LEVEL_CRITICAL in log_level)) {
+				return false;
+			}
+
+			break;
+
+		// Log level 0: CRITICAL/ERROR/WARNING only (default)
+		default:
+		case 0:
+			if (!(LogLevelFlags.LEVEL_WARNING in log_level) &&
+				!(LogLevelFlags.LEVEL_ERROR in log_level) &&
+				!(LogLevelFlags.LEVEL_CRITICAL in log_level)) {
+				return false;
+			}
+
+			break;
+		}
+
+		return true;
+	}
+
+	private static void log_handler (string? log_domain, LogLevelFlags log_level, string message) {
+		if (!log_should_handle (log_level)) {
+			return;
+		}
+
+		GLib.Log.default_handler (log_domain, log_level, message);
+	}
+
 	// Query
 	public abstract Cursor query (string sparql, Cancellable? cancellable = null) throws GLib.Error;
 	public async abstract Cursor query_async (string sparql, Cancellable? cancellable = null) throws GLib.Error;



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