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