Fix crashes when filenames end up being NULL in some prpls.
[pidgin-git.git] / libpurple / debug.c
blob3682c9e708719c5bc4bc88fbda847ea7397f3263
1 /**
2 * @file debug.c Debug API
3 * @ingroup core
4 */
6 /* purple
8 * Purple is the legal property of its developers, whose names are too numerous
9 * to list here. Please refer to the COPYRIGHT file distributed with this
10 * source distribution.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
26 #include "debug.h"
27 #include "internal.h"
28 #include "prefs.h"
29 #include "util.h"
31 static PurpleDebugUiOps *debug_ui_ops = NULL;
34 * This determines whether debug info should be written to the
35 * console or not.
37 * It doesn't make sense to make this a normal Purple preference
38 * because it's a command line option. This will always be FALSE,
39 * unless the user explicitly started Purple with the -d flag.
40 * It doesn't matter what this value was the last time Purple was
41 * started, so it doesn't make sense to save it in prefs.
43 static gboolean debug_enabled = FALSE;
45 static void
46 purple_debug_vargs(PurpleDebugLevel level, const char *category,
47 const char *format, va_list args)
49 PurpleDebugUiOps *ops;
50 char *arg_s = NULL;
52 g_return_if_fail(level != PURPLE_DEBUG_ALL);
53 g_return_if_fail(format != NULL);
55 ops = purple_debug_get_ui_ops();
57 if (!debug_enabled && ((ops == NULL) || (ops->print == NULL) ||
58 (ops->is_enabled && !ops->is_enabled(level, category))))
59 return;
61 arg_s = g_strdup_vprintf(format, args);
63 if (debug_enabled) {
64 gchar *ts_s;
65 const char *mdate;
66 time_t mtime = time(NULL);
69 mdate = purple_utf8_strftime("%H:%M:%S", localtime(&mtime));
70 ts_s = g_strdup_printf("(%s) ", mdate);
72 if (category == NULL)
73 g_print("%s%s", ts_s, arg_s);
74 else
75 g_print("%s%s: %s", ts_s, category, arg_s);
77 g_free(ts_s);
80 if (ops != NULL && ops->print != NULL)
81 ops->print(level, category, arg_s);
83 g_free(arg_s);
86 void
87 purple_debug(PurpleDebugLevel level, const char *category,
88 const char *format, ...)
90 va_list args;
92 g_return_if_fail(level != PURPLE_DEBUG_ALL);
93 g_return_if_fail(format != NULL);
95 va_start(args, format);
96 purple_debug_vargs(level, category, format, args);
97 va_end(args);
100 void
101 purple_debug_misc(const char *category, const char *format, ...)
103 va_list args;
105 g_return_if_fail(format != NULL);
107 va_start(args, format);
108 purple_debug_vargs(PURPLE_DEBUG_MISC, category, format, args);
109 va_end(args);
112 void
113 purple_debug_info(const char *category, const char *format, ...)
115 va_list args;
117 g_return_if_fail(format != NULL);
119 va_start(args, format);
120 purple_debug_vargs(PURPLE_DEBUG_INFO, category, format, args);
121 va_end(args);
124 void
125 purple_debug_warning(const char *category, const char *format, ...)
127 va_list args;
129 g_return_if_fail(format != NULL);
131 va_start(args, format);
132 purple_debug_vargs(PURPLE_DEBUG_WARNING, category, format, args);
133 va_end(args);
136 void
137 purple_debug_error(const char *category, const char *format, ...)
139 va_list args;
141 g_return_if_fail(format != NULL);
143 va_start(args, format);
144 purple_debug_vargs(PURPLE_DEBUG_ERROR, category, format, args);
145 va_end(args);
148 void
149 purple_debug_fatal(const char *category, const char *format, ...)
151 va_list args;
153 g_return_if_fail(format != NULL);
155 va_start(args, format);
156 purple_debug_vargs(PURPLE_DEBUG_FATAL, category, format, args);
157 va_end(args);
160 void
161 purple_debug_set_enabled(gboolean enabled)
163 debug_enabled = enabled;
166 gboolean
167 purple_debug_is_enabled()
169 return debug_enabled;
172 void
173 purple_debug_set_ui_ops(PurpleDebugUiOps *ops)
175 debug_ui_ops = ops;
178 PurpleDebugUiOps *
179 purple_debug_get_ui_ops(void)
181 return debug_ui_ops;
184 void
185 purple_debug_init(void)
187 purple_prefs_add_none("/purple/debug");
190 * This pref is obsolete and no longer referenced anywhere. It only
191 * survives here because it would be an API break if we removed it.
192 * Remove this when we get to 3.0.0 :)
194 purple_prefs_add_bool("/purple/debug/timestamps", TRUE);