Add debug flags support
authorPhilip Withnall <philip.withnall@collabora.co.uk>
Tue, 29 Jun 2010 14:18:54 +0000 (15:18 +0100)
committerTravis Reitter <travis.reitter@collabora.co.uk>
Wed, 28 Jul 2010 14:13:27 +0000 (16:13 +0200)
Debug flags for libfolks, controlling which debug domains spew messages, can
be specified using the FOLKS_DEBUG environment variable, with a GLib-style
debug flags string.

The two initial debug domains are:
 * Core: everything in the folks directory
 * TelepathyBackend: everything in the backends/telepathy directory

backends/telepathy/Makefile.am
folks/Makefile.am
folks/debug.vala [new file with mode: 0644]
folks/individual-aggregator.vala

index f5d7036..a7882fd 100644 (file)
@@ -2,6 +2,7 @@ AM_CPPFLAGS = \
        -I$(top_srcdir)/folks \
        -include $(CONFIG_HEADER) \
        -DPACKAGE_DATADIR=\"$(pkgdatadir)\" \
+       -DG_LOG_DOMAIN=\"TelepathyBackend\" \
        $(NULL)
 
 VALAFLAGS += $(TP_VALA_VALAFLAGS)
index c3d025e..f9d3b83 100644 (file)
@@ -6,6 +6,7 @@ AM_CPPFLAGS = \
        -DPACKAGE_DATADIR=\"$(pkgdatadir)\" \
        -DDATA_DIR=\"$(shareddir)\" \
        -DBACKEND_DIR=\"$(backenddir)\" \
+       -DG_LOG_DOMAIN=\"Core\" \
        $(NULL)
 
 lib_LTLIBRARIES = libfolks.la
@@ -23,6 +24,7 @@ folks_valasources = \
        persona.vala \
        persona-store.vala \
        presence.vala \
+       debug.vala \
        $(NULL)
 
 libfolks_la_SOURCES = $(folks_valasources:.vala=.c)
diff --git a/folks/debug.vala b/folks/debug.vala
new file mode 100644 (file)
index 0000000..f56de98
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ * Copyright (C) 2010 Collabora Ltd.
+ *
+ * This library is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors:
+ *       Philip Withnall <philip.withnall@collabora.co.uk>
+ */
+
+using GLib;
+
+namespace Folks.Debug
+{
+  private enum Domains {
+    /* Zero is used for "no debug spew" */
+    CORE = 1 << 0,
+    TELEPATHY_BACKEND = 1 << 1
+  }
+
+  internal static void set_flags (string? debug_flags)
+    {
+      GLib.DebugKey keys[2] =
+        {
+          DebugKey () { key = "Core", value = Domains.CORE },
+          DebugKey () { key = "TelepathyBackend",
+              value = Domains.TELEPATHY_BACKEND }
+        };
+
+      uint flags = GLib.parse_debug_string (debug_flags, keys);
+
+      foreach (unowned DebugKey key in keys)
+        {
+          if ((flags & key.value) == 0)
+            {
+              /* Install a log handler which will blackhole the log message.
+               * Other log messages will be printed out by the default log
+               * handler. */
+              Log.set_handler (key.key, LogLevelFlags.LEVEL_DEBUG,
+                  (domain, flags, message) => {});
+            }
+        }
+    }
+}
index 1e9ab6e..5d602a3 100644 (file)
@@ -104,6 +104,8 @@ public class Folks.IndividualAggregator : Object
 
       this.backends = new HashSet<Backend> ();
 
+      Debug.set_flags (Environment.get_variable ("FOLKS_DEBUG"));
+
       this.backend_store = new BackendStore ();
       this.backend_store.backend_available.connect (this.backend_available_cb);
     }