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