[telepathy-gabble/master] Use a hash table to contain debug flags.

Jonny Lamb jonny.lamb at collabora.co.uk
Wed Jun 3 05:28:27 PDT 2009


Signed-off-by: Jonny Lamb <jonny.lamb at collabora.co.uk>
---
 src/connection-manager.c |    9 +++++++++
 src/debug.c              |   45 ++++++++++++++++++++++++++++++++++++++-------
 src/debug.h              |    1 +
 3 files changed, 48 insertions(+), 7 deletions(-)

diff --git a/src/connection-manager.c b/src/connection-manager.c
index 21fc865..1c1ae58 100644
--- a/src/connection-manager.c
+++ b/src/connection-manager.c
@@ -29,6 +29,7 @@
 #include <telepathy-glib/errors.h>
 
 #include "connection.h"
+#include "debug.h"
 
 G_DEFINE_TYPE(GabbleConnectionManager,
     gabble_connection_manager,
@@ -46,14 +47,22 @@ static TpBaseConnection *_gabble_connection_manager_new_connection (
     TpIntSet *params_present, void *parsed_params, GError **error);
 
 static void
+gabble_connection_manager_finalize (GObject *object)
+{
+  gabble_debug_free ();
+}
+
+static void
 gabble_connection_manager_class_init (GabbleConnectionManagerClass *klass)
 {
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
   TpBaseConnectionManagerClass *base_class =
     (TpBaseConnectionManagerClass *) klass;
 
   base_class->new_connection = _gabble_connection_manager_new_connection;
   base_class->cm_dbus_name = "gabble";
   base_class->protocol_params = gabble_connection_manager_get_protocols ();
+  object_class->finalize = gabble_connection_manager_finalize;
 }
 
 /* private data */
diff --git a/src/debug.c b/src/debug.c
index 47e37c9..f115cbb 100644
--- a/src/debug.c
+++ b/src/debug.c
@@ -68,19 +68,50 @@ gboolean gabble_debug_flag_is_set (GabbleDebugFlags flag)
   return flag & flags;
 }
 
+GHashTable *flag_to_keys = NULL;
+
+static guint
+debug_flag_hash (gconstpointer key)
+{
+  return GPOINTER_TO_UINT (key);
+}
+
+static gboolean
+debug_flag_equal (gconstpointer a,
+    gconstpointer b)
+{
+  return GPOINTER_TO_UINT (a) == GPOINTER_TO_UINT (b);
+}
+
 static const gchar *
 debug_flag_to_key (GabbleDebugFlags flag)
 {
-  guint i;
-
-  for (i = 0; keys[i].value; i++)
+  if (flag_to_keys == NULL)
     {
-      GDebugKey key = (GDebugKey) keys[i];
-      if (key.value == flag)
-        return key.key;
+      guint i;
+
+      flag_to_keys = g_hash_table_new_full (debug_flag_hash, debug_flag_equal,
+          NULL, g_free);
+
+      for (i = 0; keys[i].value; i++)
+        {
+          GDebugKey key = (GDebugKey) keys[i];
+          g_hash_table_insert (flag_to_keys, GUINT_TO_POINTER (key.value),
+              g_strdup (key.key));
+        }
     }
 
-  return NULL;
+  return g_hash_table_lookup (flag_to_keys, GUINT_TO_POINTER (flag));
+}
+
+void
+gabble_debug_free (void)
+{
+  if (flag_to_keys == NULL)
+    return;
+
+  g_hash_table_destroy (flag_to_keys);
+  flag_to_keys = NULL;
 }
 
 static void
diff --git a/src/debug.h b/src/debug.h
index da4c3e5..67e3583 100644
--- a/src/debug.h
+++ b/src/debug.h
@@ -35,6 +35,7 @@ typedef enum
 void gabble_debug_set_flags_from_env (void);
 void gabble_debug_set_flags (GabbleDebugFlags flags);
 gboolean gabble_debug_flag_is_set (GabbleDebugFlags flag);
+void gabble_debug_free (void);
 void gabble_debug (GabbleDebugFlags flag, const gchar *format, ...)
     G_GNUC_PRINTF (2, 3);
 
-- 
1.5.6.5




More information about the telepathy-commits mailing list