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 PurpleDebugUi
*debug_ui
= 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
)
55 PurpleDebugUiInterface
*iface
;
58 g_return_if_fail(level
!= PURPLE_DEBUG_ALL
);
59 g_return_if_fail(format
!= NULL
);
61 ops
= purple_debug_get_ui();
64 iface
= PURPLE_DEBUG_UI_GET_IFACE(ops
);
68 if (!debug_enabled
&& ((iface
== NULL
) || (iface
->print
== NULL
) ||
69 (iface
->is_enabled
&& !iface
->is_enabled(ops
, level
, category
))))
72 arg_s
= g_strdup_vprintf(format
, args
);
73 g_strchomp(arg_s
); /* strip trailing linefeeds */
78 time_t mtime
= time(NULL
);
79 const gchar
*format_pre
, *format_post
;
86 else if (level
== PURPLE_DEBUG_MISC
)
87 format_pre
= "\033[0;37m";
88 else if (level
== PURPLE_DEBUG_INFO
)
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
);
106 g_print("%s%s%s: %s%s\n", format_pre
, ts_s
, category
, arg_s
, format_post
);
111 if (iface
!= NULL
&& iface
->print
!= NULL
)
112 iface
->print(ops
, level
, category
, arg_s
);
118 purple_debug(PurpleDebugLevel level
, const char *category
,
119 const char *format
, ...)
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
);
132 purple_debug_misc(const char *category
, const char *format
, ...)
136 g_return_if_fail(format
!= NULL
);
138 va_start(args
, format
);
139 purple_debug_vargs(PURPLE_DEBUG_MISC
, category
, format
, args
);
144 purple_debug_info(const char *category
, const char *format
, ...)
148 g_return_if_fail(format
!= NULL
);
150 va_start(args
, format
);
151 purple_debug_vargs(PURPLE_DEBUG_INFO
, category
, format
, args
);
156 purple_debug_warning(const char *category
, const char *format
, ...)
160 g_return_if_fail(format
!= NULL
);
162 va_start(args
, format
);
163 purple_debug_vargs(PURPLE_DEBUG_WARNING
, category
, format
, args
);
168 purple_debug_error(const char *category
, const char *format
, ...)
172 g_return_if_fail(format
!= NULL
);
174 va_start(args
, format
);
175 purple_debug_vargs(PURPLE_DEBUG_ERROR
, category
, format
, args
);
180 purple_debug_fatal(const char *category
, const char *format
, ...)
184 g_return_if_fail(format
!= NULL
);
186 va_start(args
, format
);
187 purple_debug_vargs(PURPLE_DEBUG_FATAL
, category
, format
, args
);
192 purple_debug_set_enabled(gboolean enabled
)
194 debug_enabled
= enabled
;
198 purple_debug_is_enabled()
200 return debug_enabled
;
204 purple_debug_set_ui(PurpleDebugUi
*ops
)
206 g_set_object(&debug_ui
, ops
);
210 purple_debug_is_verbose()
212 return debug_verbose
;
216 purple_debug_set_verbose(gboolean verbose
)
218 debug_verbose
= verbose
;
222 purple_debug_is_unsafe()
228 purple_debug_set_unsafe(gboolean unsafe
)
230 debug_unsafe
= unsafe
;
234 purple_debug_set_colored(gboolean colored
)
236 debug_colored
= colored
;
240 purple_debug_get_ui(void)
245 G_DEFINE_INTERFACE(PurpleDebugUi
, purple_debug_ui
, G_TYPE_OBJECT
);
248 purple_debug_ui_default_init(PurpleDebugUiInterface
*iface
)
250 /* add properties and signals to the interface here */
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");