]> git.0d.be Git - empathy.git/blob - libempathy/empathy-debug.c
Merge remote-tracking branch 'origin/gnome-3-8'
[empathy.git] / libempathy / empathy-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 "empathy-debug.h"
23
24 #ifdef ENABLE_DEBUG
25
26 static EmpathyDebugFlags flags = 0;
27
28 static GDebugKey keys[] = {
29   { "Tp", EMPATHY_DEBUG_TP },
30   { "Chat", EMPATHY_DEBUG_CHAT },
31   { "Contact", EMPATHY_DEBUG_CONTACT },
32   { "Account", EMPATHY_DEBUG_ACCOUNT },
33   { "Irc", EMPATHY_DEBUG_IRC },
34   { "Dispatcher", EMPATHY_DEBUG_DISPATCHER },
35   { "Ft", EMPATHY_DEBUG_FT },
36   { "Location", EMPATHY_DEBUG_LOCATION },
37   { "Other", EMPATHY_DEBUG_OTHER },
38   { "Connectivity", EMPATHY_DEBUG_CONNECTIVITY },
39   { "ImportMc4Accounts", EMPATHY_DEBUG_IMPORT_MC4_ACCOUNTS },
40   { "Tests", EMPATHY_DEBUG_TESTS },
41   { "Voip", EMPATHY_DEBUG_VOIP },
42   { "Tls", EMPATHY_DEBUG_TLS },
43   { "Sasl", EMPATHY_DEBUG_SASL },
44   { "Camera", EMPATHY_DEBUG_CAMERA },
45   { 0, }
46 };
47
48 static void
49 debug_set_flags (EmpathyDebugFlags new_flags)
50 {
51   flags |= new_flags;
52 }
53
54 void
55 empathy_debug_set_flags (const gchar *flags_string)
56 {
57   guint nkeys;
58
59   for (nkeys = 0; keys[nkeys].value; nkeys++);
60
61   tp_debug_set_flags (flags_string);
62
63   if (flags_string)
64       debug_set_flags (g_parse_debug_string (flags_string, keys, nkeys));
65 }
66
67 gboolean
68 empathy_debug_flag_is_set (EmpathyDebugFlags flag)
69 {
70   return (flag & flags) != 0;
71 }
72
73 GHashTable *flag_to_keys = NULL;
74
75 static const gchar *
76 debug_flag_to_key (EmpathyDebugFlags flag)
77 {
78   if (flag_to_keys == NULL)
79     {
80       guint i;
81
82       flag_to_keys = g_hash_table_new_full (g_direct_hash, g_direct_equal,
83           NULL, g_free);
84
85       for (i = 0; keys[i].value; i++)
86         {
87           GDebugKey key = (GDebugKey) keys[i];
88           g_hash_table_insert (flag_to_keys, GUINT_TO_POINTER (key.value),
89               g_strdup (key.key));
90         }
91     }
92
93   return g_hash_table_lookup (flag_to_keys, GUINT_TO_POINTER (flag));
94 }
95
96 void
97 empathy_debug_free (void)
98 {
99   if (flag_to_keys == NULL)
100     return;
101
102   g_hash_table_unref (flag_to_keys);
103   flag_to_keys = NULL;
104 }
105
106 static void
107 log_to_debug_sender (EmpathyDebugFlags flag,
108     const gchar *message)
109 {
110   TpDebugSender *sender;
111   gchar *domain;
112   GTimeVal now;
113
114   sender = tp_debug_sender_dup ();
115
116   g_get_current_time (&now);
117
118   domain = g_strdup_printf ("%s/%s", G_LOG_DOMAIN, debug_flag_to_key (flag));
119
120   tp_debug_sender_add_message (sender, &now, domain, G_LOG_LEVEL_DEBUG, message);
121
122   g_free (domain);
123   g_object_unref (sender);
124 }
125
126 void
127 empathy_debug (EmpathyDebugFlags flag,
128     const gchar *format,
129     ...)
130 {
131   gchar *message;
132   va_list args;
133
134   va_start (args, format);
135   message = g_strdup_vprintf (format, args);
136   va_end (args);
137
138   log_to_debug_sender (flag, message);
139
140   if (flag & flags)
141     g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "%s", message);
142
143   g_free (message);
144 }
145
146 #else
147
148 gboolean
149 empathy_debug_flag_is_set (EmpathyDebugFlags flag)
150 {
151   return FALSE;
152 }
153
154 void
155 empathy_debug (EmpathyDebugFlags flag, const gchar *format, ...)
156 {
157 }
158
159 void
160 empathy_debug_set_flags (const gchar *flags_string)
161 {
162 }
163
164 #endif /* ENABLE_DEBUG */
165