Migrate certificates, icons, logs to XDG dirs
[pidgin-git.git] / libpurple / debug.c
blob6c96c942d09446de4ee1d111e659b3b57c877cc7
1 /* purple
3 * Purple is the legal property of its developers, whose names are too numerous
4 * to list here. Please refer to the COPYRIGHT file distributed with this
5 * source distribution.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
21 #include "internal.h"
22 #include "debug.h"
23 #include "prefs.h"
24 #include "util.h"
26 static PurpleDebugUiOps *debug_ui_ops = NULL;
29 * This determines whether debug info should be written to the
30 * console or not.
32 * It doesn't make sense to make this a normal Purple preference
33 * because it's a command line option. This will always be FALSE,
34 * unless the user explicitly started the UI with the -d flag.
35 * It doesn't matter what this value was the last time Purple was
36 * started, so it doesn't make sense to save it in prefs.
38 static gboolean debug_enabled = FALSE;
41 * These determine whether verbose or unsafe debugging are desired. I
42 * don't want to make these purple preferences because their values should
43 * not be remembered across instances of the UI.
45 static gboolean debug_verbose = FALSE;
46 static gboolean debug_unsafe = FALSE;
48 static gboolean debug_colored = FALSE;
50 static void
51 purple_debug_vargs(PurpleDebugLevel level, const char *category,
52 const char *format, va_list args)
54 PurpleDebugUiOps *ops;
55 char *arg_s = NULL;
57 g_return_if_fail(level != PURPLE_DEBUG_ALL);
58 g_return_if_fail(format != NULL);
60 ops = purple_debug_get_ui_ops();
62 if (!debug_enabled && ((ops == NULL) || (ops->print == NULL) ||
63 (ops->is_enabled && !ops->is_enabled(level, category))))
64 return;
66 arg_s = g_strdup_vprintf(format, args);
67 g_strchomp(arg_s); /* strip trailing linefeeds */
69 if (debug_enabled) {
70 gchar *ts_s;
71 const char *mdate;
72 time_t mtime = time(NULL);
73 const gchar *format_pre, *format_post;
75 format_pre = "";
76 format_post = "";
78 if (!debug_colored)
79 format_pre = "";
80 else if (level == PURPLE_DEBUG_MISC)
81 format_pre = "\033[0;37m";
82 else if (level == PURPLE_DEBUG_INFO)
83 format_pre = "";
84 else if (level == PURPLE_DEBUG_WARNING)
85 format_pre = "\033[0;33m";
86 else if (level == PURPLE_DEBUG_ERROR)
87 format_pre = "\033[1;31m";
88 else if (level == PURPLE_DEBUG_FATAL)
89 format_pre = "\033[1;33;41m";
91 if (format_pre[0] != '\0')
92 format_post = "\033[0m";
94 mdate = purple_utf8_strftime("%H:%M:%S", localtime(&mtime));
95 ts_s = g_strdup_printf("(%s) ", mdate);
97 if (category == NULL)
98 g_print("%s%s%s%s\n", format_pre, ts_s, arg_s, format_post);
99 else
100 g_print("%s%s%s: %s%s\n", format_pre, ts_s, category, arg_s, format_post);
102 g_free(ts_s);
105 if (ops != NULL && ops->print != NULL)
106 ops->print(level, category, arg_s);
108 g_free(arg_s);
111 void
112 purple_debug(PurpleDebugLevel level, const char *category,
113 const char *format, ...)
115 va_list args;
117 g_return_if_fail(level != PURPLE_DEBUG_ALL);
118 g_return_if_fail(format != NULL);
120 va_start(args, format);
121 purple_debug_vargs(level, category, format, args);
122 va_end(args);
125 void
126 purple_debug_misc(const char *category, const char *format, ...)
128 va_list args;
130 g_return_if_fail(format != NULL);
132 va_start(args, format);
133 purple_debug_vargs(PURPLE_DEBUG_MISC, category, format, args);
134 va_end(args);
137 void
138 purple_debug_info(const char *category, const char *format, ...)
140 va_list args;
142 g_return_if_fail(format != NULL);
144 va_start(args, format);
145 purple_debug_vargs(PURPLE_DEBUG_INFO, category, format, args);
146 va_end(args);
149 void
150 purple_debug_warning(const char *category, const char *format, ...)
152 va_list args;
154 g_return_if_fail(format != NULL);
156 va_start(args, format);
157 purple_debug_vargs(PURPLE_DEBUG_WARNING, category, format, args);
158 va_end(args);
161 void
162 purple_debug_error(const char *category, const char *format, ...)
164 va_list args;
166 g_return_if_fail(format != NULL);
168 va_start(args, format);
169 purple_debug_vargs(PURPLE_DEBUG_ERROR, category, format, args);
170 va_end(args);
173 void
174 purple_debug_fatal(const char *category, const char *format, ...)
176 va_list args;
178 g_return_if_fail(format != NULL);
180 va_start(args, format);
181 purple_debug_vargs(PURPLE_DEBUG_FATAL, category, format, args);
182 va_end(args);
185 void
186 purple_debug_set_enabled(gboolean enabled)
188 debug_enabled = enabled;
191 gboolean
192 purple_debug_is_enabled()
194 return debug_enabled;
197 static PurpleDebugUiOps *
198 purple_debug_ui_ops_copy(PurpleDebugUiOps *ops)
200 PurpleDebugUiOps *ops_new;
202 g_return_val_if_fail(ops != NULL, NULL);
204 ops_new = g_new(PurpleDebugUiOps, 1);
205 *ops_new = *ops;
207 return ops_new;
210 GType
211 purple_debug_ui_ops_get_type(void)
213 static GType type = 0;
215 if (type == 0) {
216 type = g_boxed_type_register_static("PurpleDebugUiOps",
217 (GBoxedCopyFunc)purple_debug_ui_ops_copy,
218 (GBoxedFreeFunc)g_free);
221 return type;
224 void
225 purple_debug_set_ui_ops(PurpleDebugUiOps *ops)
227 debug_ui_ops = ops;
230 gboolean
231 purple_debug_is_verbose()
233 return debug_verbose;
236 void
237 purple_debug_set_verbose(gboolean verbose)
239 debug_verbose = verbose;
242 gboolean
243 purple_debug_is_unsafe()
245 return debug_unsafe;
248 void
249 purple_debug_set_unsafe(gboolean unsafe)
251 debug_unsafe = unsafe;
254 void
255 purple_debug_set_colored(gboolean colored)
257 debug_colored = colored;
260 PurpleDebugUiOps *
261 purple_debug_get_ui_ops(void)
263 return debug_ui_ops;
266 void
267 purple_debug_init(void)
269 /* Read environment variables once per init */
270 if(g_getenv("PURPLE_UNSAFE_DEBUG"))
271 purple_debug_set_unsafe(TRUE);
273 if(g_getenv("PURPLE_VERBOSE_DEBUG"))
274 purple_debug_set_verbose(TRUE);
276 purple_prefs_add_none("/purple/debug");