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