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
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
26 static PurpleDebugUiOps
*debug_ui_ops
= NULL
;
29 * This determines whether debug info should be written to the
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
;
51 purple_debug_vargs(PurpleDebugLevel level
, const char *category
,
52 const char *format
, va_list args
)
54 PurpleDebugUiOps
*ops
;
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
))))
66 arg_s
= g_strdup_vprintf(format
, args
);
67 g_strchomp(arg_s
); /* strip trailing linefeeds */
72 time_t mtime
= time(NULL
);
73 const gchar
*format_pre
, *format_post
;
80 else if (level
== PURPLE_DEBUG_MISC
)
81 format_pre
= "\033[0;37m";
82 else if (level
== PURPLE_DEBUG_INFO
)
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
);
98 g_print("%s%s%s%s\n", format_pre
, ts_s
, arg_s
, format_post
);
100 g_print("%s%s%s: %s%s\n", format_pre
, ts_s
, category
, arg_s
, format_post
);
105 if (ops
!= NULL
&& ops
->print
!= NULL
)
106 ops
->print(level
, category
, arg_s
);
112 purple_debug(PurpleDebugLevel level
, const char *category
,
113 const char *format
, ...)
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
);
126 purple_debug_misc(const char *category
, const char *format
, ...)
130 g_return_if_fail(format
!= NULL
);
132 va_start(args
, format
);
133 purple_debug_vargs(PURPLE_DEBUG_MISC
, category
, format
, args
);
138 purple_debug_info(const char *category
, const char *format
, ...)
142 g_return_if_fail(format
!= NULL
);
144 va_start(args
, format
);
145 purple_debug_vargs(PURPLE_DEBUG_INFO
, category
, format
, args
);
150 purple_debug_warning(const char *category
, const char *format
, ...)
154 g_return_if_fail(format
!= NULL
);
156 va_start(args
, format
);
157 purple_debug_vargs(PURPLE_DEBUG_WARNING
, category
, format
, args
);
162 purple_debug_error(const char *category
, const char *format
, ...)
166 g_return_if_fail(format
!= NULL
);
168 va_start(args
, format
);
169 purple_debug_vargs(PURPLE_DEBUG_ERROR
, category
, format
, args
);
174 purple_debug_fatal(const char *category
, const char *format
, ...)
178 g_return_if_fail(format
!= NULL
);
180 va_start(args
, format
);
181 purple_debug_vargs(PURPLE_DEBUG_FATAL
, category
, format
, args
);
186 purple_debug_set_enabled(gboolean enabled
)
188 debug_enabled
= enabled
;
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);
211 purple_debug_ui_ops_get_type(void)
213 static GType type
= 0;
216 type
= g_boxed_type_register_static("PurpleDebugUiOps",
217 (GBoxedCopyFunc
)purple_debug_ui_ops_copy
,
218 (GBoxedFreeFunc
)g_free
);
225 purple_debug_set_ui_ops(PurpleDebugUiOps
*ops
)
231 purple_debug_is_verbose()
233 return debug_verbose
;
237 purple_debug_set_verbose(gboolean verbose
)
239 debug_verbose
= verbose
;
243 purple_debug_is_unsafe()
249 purple_debug_set_unsafe(gboolean unsafe
)
251 debug_unsafe
= unsafe
;
255 purple_debug_set_colored(gboolean colored
)
257 debug_colored
= colored
;
261 purple_debug_get_ui_ops(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");