Help: Use stable 'if' namespace instead of experimental
[empathy-mirror.git] / libempathy / empathy-debug.c
blobd919743eb76ad022664708c7db6b8bb2f39bce95
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
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.
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.
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
21 #include "config.h"
22 #include "empathy-debug.h"
24 #include <tp-account-widgets/tpaw-debug.h>
26 #ifdef ENABLE_DEBUG
28 static EmpathyDebugFlags flags = 0;
30 static GDebugKey keys[] = {
31 { "Tp", EMPATHY_DEBUG_TP },
32 { "Chat", EMPATHY_DEBUG_CHAT },
33 { "Contact", EMPATHY_DEBUG_CONTACT },
34 { "Account", EMPATHY_DEBUG_ACCOUNT },
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, }
49 static void
50 debug_set_flags (EmpathyDebugFlags new_flags)
52 flags |= new_flags;
55 void
56 empathy_debug_set_flags (const gchar *flags_string)
58 guint nkeys;
60 for (nkeys = 0; keys[nkeys].value; nkeys++);
62 tp_debug_set_flags (flags_string);
63 tpaw_debug_set_flags (flags_string);
65 if (flags_string)
66 debug_set_flags (g_parse_debug_string (flags_string, keys, nkeys));
69 gboolean
70 empathy_debug_flag_is_set (EmpathyDebugFlags flag)
72 return (flag & flags) != 0;
75 GHashTable *flag_to_keys = NULL;
77 static const gchar *
78 debug_flag_to_key (EmpathyDebugFlags flag)
80 if (flag_to_keys == NULL)
82 guint i;
84 flag_to_keys = g_hash_table_new_full (g_direct_hash, g_direct_equal,
85 NULL, g_free);
87 for (i = 0; keys[i].value; i++)
89 GDebugKey key = (GDebugKey) keys[i];
90 g_hash_table_insert (flag_to_keys, GUINT_TO_POINTER (key.value),
91 g_strdup (key.key));
95 return g_hash_table_lookup (flag_to_keys, GUINT_TO_POINTER (flag));
98 void
99 empathy_debug_free (void)
101 if (flag_to_keys == NULL)
102 return;
104 g_hash_table_unref (flag_to_keys);
105 flag_to_keys = NULL;
108 static void
109 log_to_debug_sender (EmpathyDebugFlags flag,
110 const gchar *message)
112 TpDebugSender *sender;
113 gchar *domain;
114 GTimeVal now;
116 sender = tp_debug_sender_dup ();
118 g_get_current_time (&now);
120 domain = g_strdup_printf ("%s/%s", G_LOG_DOMAIN, debug_flag_to_key (flag));
122 tp_debug_sender_add_message (sender, &now, domain, G_LOG_LEVEL_DEBUG, message);
124 g_free (domain);
125 g_object_unref (sender);
128 void
129 empathy_debug (EmpathyDebugFlags flag,
130 const gchar *format,
131 ...)
133 gchar *message;
134 va_list args;
136 va_start (args, format);
137 message = g_strdup_vprintf (format, args);
138 va_end (args);
140 log_to_debug_sender (flag, message);
142 if (flag & flags)
143 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "%s", message);
145 g_free (message);
148 #else
150 gboolean
151 empathy_debug_flag_is_set (EmpathyDebugFlags flag)
153 return FALSE;
156 void
157 empathy_debug (EmpathyDebugFlags flag, const gchar *format, ...)
161 void
162 empathy_debug_set_flags (const gchar *flags_string)
166 #endif /* ENABLE_DEBUG */