[telepathy-salut/master] Add support for TpDebugSender.

Jonny Lamb jonny.lamb at collabora.co.uk
Mon Aug 24 04:37:26 PDT 2009


Signed-off-by: Jonny Lamb <jonny.lamb at collabora.co.uk>
---
 configure.ac                   |    2 +-
 src/Makefile.am                |    3 +-
 src/debug.c                    |   72 ++++++++++++++++++++++++++++++++++++---
 src/debug.h                    |    3 ++
 src/salut-connection-manager.c |   22 ++++++++++++
 5 files changed, 94 insertions(+), 8 deletions(-)

diff --git a/configure.ac b/configure.ac
index 66d4466..ac880c5 100644
--- a/configure.ac
+++ b/configure.ac
@@ -175,7 +175,7 @@ AC_SUBST(LIBXML2_CFLAGS)
 AC_SUBST(LIBXML2_LIBS)
 
 dnl Check for telepathy-glib
-PKG_CHECK_MODULES(TELEPATHY_GLIB, [telepathy-glib >= 0.7.31])
+PKG_CHECK_MODULES(TELEPATHY_GLIB, [telepathy-glib >= 0.7.36])
 
 AC_SUBST(TELEPATHY_GLIB_CFLAGS)
 AC_SUBST(TELEPATHY_GLIB_LIBS)
diff --git a/src/Makefile.am b/src/Makefile.am
index 4e58b4f..8302180 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -162,7 +162,8 @@ AM_CFLAGS = $(ERROR_CFLAGS) $(GCOV_CFLAGS) @DBUS_CFLAGS@ \
   @TELEPATHY_GLIB_CFLAGS@ \
   @AVAHI_CFLAGS@ \
   -I $(top_srcdir) -I $(top_builddir) \
-  -I $(top_srcdir)/lib -I $(top_builddir)/lib
+  -I $(top_srcdir)/lib -I $(top_builddir)/lib \
+  -DG_LOG_DOMAIN=\"salut\"
 
 AM_LDFLAGS = $(GCOV_LIBS) @DBUS_LIBS@ \
              @TELEPATHY_GLIB_LIBS@ \
diff --git a/src/debug.c b/src/debug.c
index b43eb34..9227dc4 100644
--- a/src/debug.c
+++ b/src/debug.c
@@ -10,6 +10,7 @@
 #include <glib/gstdio.h>
 
 #include <telepathy-glib/debug.h>
+#include <telepathy-glib/debug-sender.h>
 
 #include "debug.h"
 
@@ -67,17 +68,76 @@ gboolean debug_flag_is_set (DebugFlags flag)
   return flag & flags;
 }
 
+static GHashTable *flag_to_keys = NULL;
+
+static const gchar *
+debug_flag_to_domain (DebugFlags flag)
+{
+  if (G_UNLIKELY (flag_to_keys == NULL))
+    {
+      guint i;
+
+      flag_to_keys = g_hash_table_new_full (g_direct_hash, g_direct_equal,
+          NULL, g_free);
+
+      for (i = 0; keys[i].value; i++)
+        {
+          GDebugKey key = (GDebugKey) keys[i];
+          gchar *val;
+
+          val = g_strdup_printf ("%s/%s", G_LOG_DOMAIN, key.key);
+          g_hash_table_insert (flag_to_keys,
+              GUINT_TO_POINTER (key.value), val);
+        }
+    }
+
+  return g_hash_table_lookup (flag_to_keys, GUINT_TO_POINTER (flag));
+}
+
+void
+debug_free (void)
+{
+  if (flag_to_keys == NULL)
+    return;
+
+  g_hash_table_destroy (flag_to_keys);
+  flag_to_keys = NULL;
+}
+
+static void
+log_to_debug_sender (DebugFlags flag,
+    const gchar *message)
+{
+  TpDebugSender *dbg;
+  GTimeVal now;
+
+  dbg = tp_debug_sender_dup ();
+
+  g_get_current_time (&now);
+
+  tp_debug_sender_add_message (dbg, &now, debug_flag_to_domain (flag),
+      G_LOG_LEVEL_DEBUG, message);
+
+  g_object_unref (dbg);
+}
+
 void debug (DebugFlags flag,
                    const gchar *format,
                    ...)
 {
+  gchar *message;
+  va_list args;
+
+  va_start (args, format);
+  message = g_strdup_vprintf (format, args);
+  va_end (args);
+
+  log_to_debug_sender (flag, message);
+
   if (flag & flags)
-    {
-      va_list args;
-      va_start (args, format);
-      g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format, args);
-      va_end (args);
-    }
+    g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "%s", message);
+
+  g_free (message);
 }
 
 #endif /* ENABLE_DEBUG */
diff --git a/src/debug.h b/src/debug.h
index ede6484..cc28fcc 100644
--- a/src/debug.h
+++ b/src/debug.h
@@ -40,6 +40,7 @@ void debug_set_flags (DebugFlags flags);
 gboolean debug_flag_is_set (DebugFlags flag);
 void debug (DebugFlags flag, const gchar *format, ...)
     G_GNUC_PRINTF (2, 3);
+void debug_free (void);
 
 #ifdef DEBUG_FLAG
 
@@ -64,6 +65,8 @@ void debug (DebugFlags flag, const gchar *format, ...)
 
 #endif /* DEBUG_FLAG */
 
+#define debug_free() G_STMT_START { } G_STMT_END
+
 #endif /* ENABLE_DEBUG */
 
 G_END_DECLS
diff --git a/src/salut-connection-manager.c b/src/salut-connection-manager.c
index 9d912d3..666ffed 100644
--- a/src/salut-connection-manager.c
+++ b/src/salut-connection-manager.c
@@ -24,9 +24,11 @@
 
 #include <dbus/dbus-protocol.h>
 #include <telepathy-glib/util.h>
+#include <telepathy-glib/debug-sender.h>
 
 #include "salut-connection-manager.h"
 #include "salut-connection.h"
+#include "debug.h"
 
 /* properties */
 enum
@@ -39,6 +41,7 @@ typedef struct _SalutConnectionManagerPrivate SalutConnectionManagerPrivate;
 struct _SalutConnectionManagerPrivate
 {
   GType backend_type;
+  TpDebugSender *debug_sender;
 };
 
 #define SALUT_CONNECTION_MANAGER_GET_PRIVATE(obj) \
@@ -112,6 +115,9 @@ salut_connection_manager_init (SalutConnectionManager *self)
   SalutConnectionManagerPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
       SALUT_TYPE_CONNECTION_MANAGER, SalutConnectionManagerPrivate);
 
+  priv->debug_sender = tp_debug_sender_dup ();
+  g_log_set_default_handler (tp_debug_sender_log_handler, G_LOG_DOMAIN);
+
   self->priv = priv;
 }
 
@@ -158,6 +164,21 @@ salut_connection_manager_set_property (GObject *object,
 }
 
 static void
+salut_connection_manager_finalize (GObject *object)
+{
+  SalutConnectionManagerPrivate *priv = SALUT_CONNECTION_MANAGER_GET_PRIVATE (
+      object);
+
+  if (priv->debug_sender != NULL)
+    {
+      g_object_unref (priv->debug_sender);
+      priv->debug_sender = NULL;
+    }
+
+  debug_free ();
+}
+
+static void
 salut_connection_manager_class_init (
     SalutConnectionManagerClass *salut_connection_manager_class)
 {
@@ -171,6 +192,7 @@ salut_connection_manager_class_init (
 
   object_class->get_property = salut_connection_manager_get_property;
   object_class->set_property = salut_connection_manager_set_property;
+  object_class->finalize = salut_connection_manager_finalize;
 
   param_spec = g_param_spec_gtype (
       "backend-type",
-- 
1.5.6.5



More information about the telepathy-commits mailing list