zephyr: Remove unused defines and headers.
[pidgin-git.git] / libpurple / debug.c
blob588ff6edf53afdca6809d9a66d591a9e5ce80633
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 && ((iface == NULL) || (iface->print == NULL) ||
69 (iface->is_enabled && !iface->is_enabled(ops, level, category))))
70 return;
72 arg_s = g_strdup_vprintf(format, args);
73 g_strchomp(arg_s); /* strip trailing linefeeds */
75 if (debug_enabled) {
76 gchar *ts_s;
77 const char *mdate;
78 time_t mtime = time(NULL);
79 const gchar *format_pre, *format_post;
81 format_pre = "";
82 format_post = "";
84 if (!debug_colored)
85 format_pre = "";
86 else if (level == PURPLE_DEBUG_MISC)
87 format_pre = "\033[0;37m";
88 else if (level == PURPLE_DEBUG_INFO)
89 format_pre = "";
90 else if (level == PURPLE_DEBUG_WARNING)
91 format_pre = "\033[0;33m";
92 else if (level == PURPLE_DEBUG_ERROR)
93 format_pre = "\033[1;31m";
94 else if (level == PURPLE_DEBUG_FATAL)
95 format_pre = "\033[1;33;41m";
97 if (format_pre[0] != '\0')
98 format_post = "\033[0m";
100 mdate = purple_utf8_strftime("%H:%M:%S", localtime(&mtime));
101 ts_s = g_strdup_printf("(%s) ", mdate);
103 if (category == NULL)
104 g_print("%s%s%s%s\n", format_pre, ts_s, arg_s, format_post);
105 else
106 g_print("%s%s%s: %s%s\n", format_pre, ts_s, category, arg_s, format_post);
108 g_free(ts_s);
111 if (iface != NULL && iface->print != NULL)
112 iface->print(ops, level, category, arg_s);
114 g_free(arg_s);
117 void
118 purple_debug(PurpleDebugLevel level, const char *category,
119 const char *format, ...)
121 va_list args;
123 g_return_if_fail(level != PURPLE_DEBUG_ALL);
124 g_return_if_fail(format != NULL);
126 va_start(args, format);
127 purple_debug_vargs(level, category, format, args);
128 va_end(args);
131 void
132 purple_debug_misc(const char *category, const char *format, ...)
134 va_list args;
136 g_return_if_fail(format != NULL);
138 va_start(args, format);
139 purple_debug_vargs(PURPLE_DEBUG_MISC, category, format, args);
140 va_end(args);
143 void
144 purple_debug_info(const char *category, const char *format, ...)
146 va_list args;
148 g_return_if_fail(format != NULL);
150 va_start(args, format);
151 purple_debug_vargs(PURPLE_DEBUG_INFO, category, format, args);
152 va_end(args);
155 void
156 purple_debug_warning(const char *category, const char *format, ...)
158 va_list args;
160 g_return_if_fail(format != NULL);
162 va_start(args, format);
163 purple_debug_vargs(PURPLE_DEBUG_WARNING, category, format, args);
164 va_end(args);
167 void
168 purple_debug_error(const char *category, const char *format, ...)
170 va_list args;
172 g_return_if_fail(format != NULL);
174 va_start(args, format);
175 purple_debug_vargs(PURPLE_DEBUG_ERROR, category, format, args);
176 va_end(args);
179 void
180 purple_debug_fatal(const char *category, const char *format, ...)
182 va_list args;
184 g_return_if_fail(format != NULL);
186 va_start(args, format);
187 purple_debug_vargs(PURPLE_DEBUG_FATAL, category, format, args);
188 va_end(args);
191 void
192 purple_debug_set_enabled(gboolean enabled)
194 debug_enabled = enabled;
197 gboolean
198 purple_debug_is_enabled()
200 return debug_enabled;
203 void
204 purple_debug_set_ui(PurpleDebugUi *ops)
206 g_set_object(&debug_ui, ops);
209 gboolean
210 purple_debug_is_verbose()
212 return debug_verbose;
215 void
216 purple_debug_set_verbose(gboolean verbose)
218 debug_verbose = verbose;
221 gboolean
222 purple_debug_is_unsafe()
224 return debug_unsafe;
227 void
228 purple_debug_set_unsafe(gboolean unsafe)
230 debug_unsafe = unsafe;
233 void
234 purple_debug_set_colored(gboolean colored)
236 debug_colored = colored;
239 PurpleDebugUi *
240 purple_debug_get_ui(void)
242 return debug_ui;
245 G_DEFINE_INTERFACE(PurpleDebugUi, purple_debug_ui, G_TYPE_OBJECT);
247 static void
248 purple_debug_ui_default_init(PurpleDebugUiInterface *iface)
250 /* add properties and signals to the interface here */
253 void
254 purple_debug_init(void)
256 /* Read environment variables once per init */
257 if(g_getenv("PURPLE_UNSAFE_DEBUG"))
258 purple_debug_set_unsafe(TRUE);
260 if(g_getenv("PURPLE_VERBOSE_DEBUG"))
261 purple_debug_set_verbose(TRUE);
263 purple_prefs_add_none("/purple/debug");