[telepathy-haze/master] Add support for TpDebugSender

Jonny Lamb jonny.lamb at collabora.co.uk
Mon Nov 9 13:12:03 PST 2009


Signed-off-by: Jonny Lamb <jonny.lamb at collabora.co.uk>
---
 configure.ac             |    2 +-
 src/connection-manager.c |   34 ++++++++++++++++++++++++++++
 src/connection-manager.h |    2 +
 src/debug.c              |   56 +++++++++++++++++++++++++++++++++++++++++-----
 src/debug.h              |    2 +
 5 files changed, 89 insertions(+), 7 deletions(-)

diff --git a/configure.ac b/configure.ac
index 5e0a00c..f598918 100644
--- a/configure.ac
+++ b/configure.ac
@@ -57,7 +57,7 @@ PKG_CHECK_MODULES(PURPLE,[purple >= 2.1.1])
 AC_SUBST(PURPLE_CFLAGS)
 AC_SUBST(PURPLE_LIBS)
 
-PKG_CHECK_MODULES(TP_GLIB,[telepathy-glib >= 0.7.21])
+PKG_CHECK_MODULES(TP_GLIB,[telepathy-glib >= 0.7.36])
 AC_SUBST(TP_GLIB_CFLAGS)
 AC_SUBST(TP_GLIB_LIBS)
 
diff --git a/src/connection-manager.c b/src/connection-manager.c
index bccdfba..382b58b 100644
--- a/src/connection-manager.c
+++ b/src/connection-manager.c
@@ -27,6 +27,8 @@
 #include <libpurple/prpl.h>
 #include <libpurple/accountopt.h>
 
+#include <telepathy-glib/debug-sender.h>
+
 #include "connection-manager.h"
 #include "debug.h"
 
@@ -34,6 +36,12 @@ G_DEFINE_TYPE(HazeConnectionManager,
     haze_connection_manager,
     TP_TYPE_BASE_CONNECTION_MANAGER)
 
+typedef struct _HazeConnectionManagerPrivate HazeConnectionManagerPrivate;
+struct _HazeConnectionManagerPrivate
+{
+    TpDebugSender *debug_sender;
+};
+
 /* For some protocols, removing the "prpl-" prefix from its name in libpurple
  * doesn't give the right name for Telepathy.  Other protocols need some
  * parameters renaming to match well-known names in the spec, or to have
@@ -506,20 +514,46 @@ static void _init_protocol_table (HazeConnectionManagerClass *klass)
 }
 
 static void
+_haze_cm_finalize (GObject *object)
+{
+    HazeConnectionManager *self = HAZE_CONNECTION_MANAGER (object);
+    HazeConnectionManagerPrivate *priv = self->priv;
+
+    if (priv->debug_sender != NULL)
+    {
+        g_object_unref (priv->debug_sender);
+        priv->debug_sender = NULL;
+    }
+}
+
+static void
 haze_connection_manager_class_init (HazeConnectionManagerClass *klass)
 {
     TpBaseConnectionManagerClass *base_class =
         (TpBaseConnectionManagerClass *)klass;
+    GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
     _init_protocol_table (klass);
 
+    object_class->finalize = _haze_cm_finalize;
+
     base_class->new_connection = _haze_connection_manager_new_connection;
     base_class->cm_dbus_name = "haze";
     base_class->protocol_params = get_protocols (klass);
+
+    g_type_class_add_private (klass, sizeof (HazeConnectionManagerPrivate));
 }
 
 static void
 haze_connection_manager_init (HazeConnectionManager *self)
 {
+    HazeConnectionManagerPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
+        HAZE_TYPE_CONNECTION_MANAGER, HazeConnectionManagerPrivate);
+
+    self->priv = priv;
+
+    priv->debug_sender = tp_debug_sender_dup ();
+    g_log_set_default_handler (tp_debug_sender_log_handler, G_LOG_DOMAIN);
+
     DEBUG ("Initializing (HazeConnectionManager *)%p", self);
 }
diff --git a/src/connection-manager.h b/src/connection-manager.h
index 4ebf4c7..134cc95 100644
--- a/src/connection-manager.h
+++ b/src/connection-manager.h
@@ -40,6 +40,8 @@ struct _HazeConnectionManagerClass {
 
 struct _HazeConnectionManager {
     TpBaseConnectionManager parent;
+
+    gpointer priv;
 };
 
 typedef struct _HazeProtocolInfo HazeProtocolInfo;
diff --git a/src/debug.c b/src/debug.c
index 2b53123..3c5f829 100644
--- a/src/debug.c
+++ b/src/debug.c
@@ -25,6 +25,7 @@
 
 #include <libpurple/debug.h>
 #include <telepathy-glib/debug.h>
+#include <telepathy-glib/debug-sender.h>
 
 
 typedef enum
@@ -74,30 +75,66 @@ static char *debug_level_names[] =
 };
 
 static void
+log_to_debug_sender (const gchar *category,
+                     GLogLevelFlags level,
+                     const gchar *message)
+{
+    TpDebugSender *dbg;
+    GTimeVal now;
+    gchar *domain;
+
+    dbg = tp_debug_sender_dup ();
+
+    g_get_current_time (&now);
+
+    domain = g_strdup_printf ("%s/%s", G_LOG_DOMAIN, category);
+
+    tp_debug_sender_add_message (dbg, &now, domain,
+        G_LOG_LEVEL_DEBUG, message);
+
+    g_free (domain);
+    g_object_unref (dbg);
+}
+
+static void
 haze_debug_print (PurpleDebugLevel level,
                   const char *category,
                   const char *arg_s)
 {
     char *argh = g_strchomp (g_strdup (arg_s));
     const char *level_name = debug_level_names[level];
+    GLogLevelFlags log_level;
+
     switch (level)
     {
         case PURPLE_DEBUG_WARNING:
+            log_level = G_LOG_LEVEL_WARNING;
             g_warning ("%s: %s", category, argh);
             break;
         case PURPLE_DEBUG_FATAL:
             /* g_critical doesn't cause an abort() in haze, so libpurple will
              * still get to do the honours of blowing us out of the water.
              */
+            log_level = G_LOG_LEVEL_CRITICAL;
             g_critical ("[%s] %s: %s", level_name, category, argh);
             break;
         case PURPLE_DEBUG_ERROR:
         case PURPLE_DEBUG_MISC:
         case PURPLE_DEBUG_INFO:
         default:
+          if (level == PURPLE_DEBUG_ERROR)
+              log_level = G_LOG_LEVEL_ERROR;
+          else if (level == PURPLE_DEBUG_INFO)
+              log_level = G_LOG_LEVEL_INFO;
+          else
+              log_level = G_LOG_LEVEL_DEBUG;
+
             g_message ("[%s] %s: %s", level_name, category, argh);
             break;
     }
+
+    log_to_debug_sender (category, log_level, argh);
+
     g_free(argh);
 }
 
@@ -160,13 +197,20 @@ void
 haze_debug (const gchar *format,
             ...)
 {
-    if (flags & HAZE_DEBUG_HAZE)
-    {
-        va_list args;
-        va_start (args, format);
+    gchar *message;
+    va_list args;
 
-        g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format, args);
+    va_start (args, format);
+    message = g_strdup_vprintf (format, args);
+    va_end (args);
 
-        va_end (args);
+    log_to_debug_sender ("haze", G_LOG_LEVEL_DEBUG, message);
+
+    if (flags & HAZE_DEBUG_HAZE)
+    {
+        g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "%s", message);
     }
+
+    g_free (message);
 }
+
diff --git a/src/debug.h b/src/debug.h
index 4d5e41c..b31faae 100644
--- a/src/debug.h
+++ b/src/debug.h
@@ -25,6 +25,8 @@ void haze_debug_init(void);
 void haze_debug (const gchar *format, ...)
     G_GNUC_PRINTF (1,2);
 
+void haze_debug_free (void);
+
 void haze_debug_set_flags_from_env (void);
 
 #define DEBUG(format, ...) \
-- 
1.5.6.5




More information about the telepathy-commits mailing list