]> git.0d.be Git - empathy.git/blob - tp-account-widgets/tpaw-debug.c
ee30b0e0034698c623cb9b6cd5a8f2d575ef9540
[empathy.git] / tp-account-widgets / tpaw-debug.c
1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
2 /*
3  * Copyright (C) 2007 Collabora Ltd.
4  * Copyright (C) 2007 Nokia Corporation
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20
21 #include "config.h"
22 #include "tpaw-debug.h"
23
24 #ifdef ENABLE_DEBUG
25
26 static TpawDebugFlags flags = 0;
27
28 static GDebugKey keys[] = {
29   { "Tp", TPAW_DEBUG_TP },
30   { "Chat", TPAW_DEBUG_CHAT },
31   { "Contact", TPAW_DEBUG_CONTACT },
32   { "Account", TPAW_DEBUG_ACCOUNT },
33   { "Irc", TPAW_DEBUG_IRC },
34   { "Dispatcher", TPAW_DEBUG_DISPATCHER },
35   { "Ft", TPAW_DEBUG_FT },
36   { "Location", TPAW_DEBUG_LOCATION },
37   { "Other", TPAW_DEBUG_OTHER },
38   { "Connectivity", TPAW_DEBUG_CONNECTIVITY },
39   { "ImportMc4Accounts", TPAW_DEBUG_IMPORT_MC4_ACCOUNTS },
40   { "Tests", TPAW_DEBUG_TESTS },
41   { "Voip", TPAW_DEBUG_VOIP },
42   { "Tls", TPAW_DEBUG_TLS },
43   { "Sasl", TPAW_DEBUG_SASL },
44   { "Camera", TPAW_DEBUG_CAMERA },
45   { 0, }
46 };
47
48 static void
49 debug_set_flags (TpawDebugFlags new_flags)
50 {
51   flags |= new_flags;
52 }
53
54 void
55 tpaw_debug_set_flags (const gchar *flags_string)
56 {
57   guint nkeys;
58
59   for (nkeys = 0; keys[nkeys].value; nkeys++);
60
61   if (flags_string)
62       debug_set_flags (g_parse_debug_string (flags_string, keys, nkeys));
63 }
64
65 gboolean
66 tpaw_debug_flag_is_set (TpawDebugFlags flag)
67 {
68   return (flag & flags) != 0;
69 }
70
71 GHashTable *flag_to_keys = NULL;
72
73 static const gchar *
74 debug_flag_to_key (TpawDebugFlags flag)
75 {
76   if (flag_to_keys == NULL)
77     {
78       guint i;
79
80       flag_to_keys = g_hash_table_new_full (g_direct_hash, g_direct_equal,
81           NULL, g_free);
82
83       for (i = 0; keys[i].value; i++)
84         {
85           GDebugKey key = (GDebugKey) keys[i];
86           g_hash_table_insert (flag_to_keys, GUINT_TO_POINTER (key.value),
87               g_strdup (key.key));
88         }
89     }
90
91   return g_hash_table_lookup (flag_to_keys, GUINT_TO_POINTER (flag));
92 }
93
94 void
95 tpaw_debug_free (void)
96 {
97   if (flag_to_keys == NULL)
98     return;
99
100   g_hash_table_unref (flag_to_keys);
101   flag_to_keys = NULL;
102 }
103
104 static void
105 log_to_debug_sender (TpawDebugFlags flag,
106     const gchar *message)
107 {
108   TpDebugSender *sender;
109   gchar *domain;
110   GTimeVal now;
111
112   sender = tp_debug_sender_dup ();
113
114   g_get_current_time (&now);
115
116   domain = g_strdup_printf ("%s/%s", G_LOG_DOMAIN, debug_flag_to_key (flag));
117
118   tp_debug_sender_add_message (sender, &now, domain, G_LOG_LEVEL_DEBUG, message);
119
120   g_free (domain);
121   g_object_unref (sender);
122 }
123
124 void
125 tpaw_debug (TpawDebugFlags flag,
126     const gchar *format,
127     ...)
128 {
129   gchar *message;
130   va_list args;
131
132   va_start (args, format);
133   message = g_strdup_vprintf (format, args);
134   va_end (args);
135
136   log_to_debug_sender (flag, message);
137
138   if (flag & flags)
139     g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "%s", message);
140
141   g_free (message);
142 }
143
144 #else
145
146 gboolean
147 tpaw_debug_flag_is_set (TpawDebugFlags flag)
148 {
149   return FALSE;
150 }
151
152 void
153 tpaw_debug (TpawDebugFlags flag, const gchar *format, ...)
154 {
155 }
156
157 void
158 tpaw_debug_set_flags (const gchar *flags_string)
159 {
160 }
161
162 #endif /* ENABLE_DEBUG */