Replace g_list_remove_link+g_list_free_1 with g_list_delete_link
[pidgin-git.git] / libpurple / debug.c
blobbb9ebd185a4248997e7d56e899fffb4ba8cc6ae9
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 PurpleDebugUi *debug_ui = 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 PurpleDebugUi *ops;
55 PurpleDebugUiInterface *iface;
56 char *arg_s = NULL;
58 g_return_if_fail(level != PURPLE_DEBUG_ALL);
59 g_return_if_fail(format != NULL);
61 ops = purple_debug_get_ui();
62 if (!ops)
63 return;
64 iface = PURPLE_DEBUG_UI_GET_IFACE(ops);
65 if (!iface)
66 return;
68 if (!debug_enabled &&
69 ((iface->print == NULL) ||
70 (iface->is_enabled && !iface->is_enabled(ops, level, category)))) {
71 return;
74 arg_s = g_strdup_vprintf(format, args);
75 g_strchomp(arg_s); /* strip trailing linefeeds */
77 if (debug_enabled) {
78 gchar *ts_s;
79 const char *mdate;
80 time_t mtime = time(NULL);
81 const gchar *format_pre, *format_post;
83 format_pre = "";
84 format_post = "";
86 if (!debug_colored)
87 format_pre = "";
88 else if (level == PURPLE_DEBUG_MISC)
89 format_pre = "\033[0;37m";
90 else if (level == PURPLE_DEBUG_INFO)
91 format_pre = "";
92 else if (level == PURPLE_DEBUG_WARNING)
93 format_pre = "\033[0;33m";
94 else if (level == PURPLE_DEBUG_ERROR)
95 format_pre = "\033[1;31m";
96 else if (level == PURPLE_DEBUG_FATAL)
97 format_pre = "\033[1;33;41m";
99 if (format_pre[0] != '\0')
100 format_post = "\033[0m";
102 mdate = purple_utf8_strftime("%H:%M:%S", localtime(&mtime));
103 ts_s = g_strdup_printf("(%s) ", mdate);
105 if (category == NULL)
106 g_print("%s%s%s%s\n", format_pre, ts_s, arg_s, format_post);
107 else
108 g_print("%s%s%s: %s%s\n", format_pre, ts_s, category, arg_s, format_post);
110 g_free(ts_s);
113 if (iface->print != NULL) {
114 iface->print(ops, level, category, arg_s);
117 g_free(arg_s);
120 void
121 purple_debug(PurpleDebugLevel level, const char *category,
122 const char *format, ...)
124 va_list args;
126 g_return_if_fail(level != PURPLE_DEBUG_ALL);
127 g_return_if_fail(format != NULL);
129 va_start(args, format);
130 purple_debug_vargs(level, category, format, args);
131 va_end(args);
134 void
135 purple_debug_misc(const char *category, const char *format, ...)
137 va_list args;
139 g_return_if_fail(format != NULL);
141 va_start(args, format);
142 purple_debug_vargs(PURPLE_DEBUG_MISC, category, format, args);
143 va_end(args);
146 void
147 purple_debug_info(const char *category, const char *format, ...)
149 va_list args;
151 g_return_if_fail(format != NULL);
153 va_start(args, format);
154 purple_debug_vargs(PURPLE_DEBUG_INFO, category, format, args);
155 va_end(args);
158 void
159 purple_debug_warning(const char *category, const char *format, ...)
161 va_list args;
163 g_return_if_fail(format != NULL);
165 va_start(args, format);
166 purple_debug_vargs(PURPLE_DEBUG_WARNING, category, format, args);
167 va_end(args);
170 void
171 purple_debug_error(const char *category, const char *format, ...)
173 va_list args;
175 g_return_if_fail(format != NULL);
177 va_start(args, format);
178 purple_debug_vargs(PURPLE_DEBUG_ERROR, category, format, args);
179 va_end(args);
182 void
183 purple_debug_fatal(const char *category, const char *format, ...)
185 va_list args;
187 g_return_if_fail(format != NULL);
189 va_start(args, format);
190 purple_debug_vargs(PURPLE_DEBUG_FATAL, category, format, args);
191 va_end(args);
194 void
195 purple_debug_set_enabled(gboolean enabled)
197 debug_enabled = enabled;
200 gboolean
201 purple_debug_is_enabled()
203 return debug_enabled;
206 void
207 purple_debug_set_ui(PurpleDebugUi *ops)
209 g_set_object(&debug_ui, ops);
212 gboolean
213 purple_debug_is_verbose()
215 return debug_verbose;
218 void
219 purple_debug_set_verbose(gboolean verbose)
221 debug_verbose = verbose;
224 gboolean
225 purple_debug_is_unsafe()
227 return debug_unsafe;
230 void
231 purple_debug_set_unsafe(gboolean unsafe)
233 debug_unsafe = unsafe;
236 void
237 purple_debug_set_colored(gboolean colored)
239 debug_colored = colored;
242 PurpleDebugUi *
243 purple_debug_get_ui(void)
245 return debug_ui;
248 G_DEFINE_INTERFACE(PurpleDebugUi, purple_debug_ui, G_TYPE_OBJECT);
250 static void
251 purple_debug_ui_default_init(PurpleDebugUiInterface *iface)
253 /* add properties and signals to the interface here */
256 void
257 purple_debug_init(void)
259 /* Read environment variables once per init */
260 if(g_getenv("PURPLE_UNSAFE_DEBUG"))
261 purple_debug_set_unsafe(TRUE);
263 if(g_getenv("PURPLE_VERBOSE_DEBUG"))
264 purple_debug_set_verbose(TRUE);
266 purple_prefs_add_none("/purple/debug");