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