comparison finch/gntdebug.c @ 15817:0e3a8505ebbe

renamed gaim-text to finch
author Sean Egan <seanegan@gmail.com>
date Sun, 18 Mar 2007 19:38:15 +0000
parents
children 32c366eeeb99
comparison
equal deleted inserted replaced
15816:317e7613e581 15817:0e3a8505ebbe
1 /**
2 * @file gntdebug.c GNT Debug API
3 * @ingroup gntui
4 *
5 * gaim
6 *
7 * Gaim is the legal property of its developers, whose names are too numerous
8 * to list here. Please refer to the COPYRIGHT file distributed with this
9 * source distribution.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
25 #include <gnt.h>
26 #include <gntbox.h>
27 #include <gnttextview.h>
28 #include <gntbutton.h>
29 #include <gntcheckbox.h>
30 #include <gntline.h>
31
32 #include "gntdebug.h"
33 #include "gntgaim.h"
34 #include "util.h"
35
36 #include <stdio.h>
37 #include <string.h>
38
39 #define PREF_ROOT "/gaim/gnt/debug"
40
41 static struct
42 {
43 GntWidget *window;
44 GntWidget *tview;
45 gboolean paused;
46 gboolean timestamps;
47 } debug;
48
49 static gboolean
50 debug_window_kpress_cb(GntWidget *wid, const char *key, GntTextView *view)
51 {
52 if (key[0] == 27)
53 {
54 if (strcmp(key, GNT_KEY_DOWN) == 0)
55 gnt_text_view_scroll(view, 1);
56 else if (strcmp(key, GNT_KEY_UP) == 0)
57 gnt_text_view_scroll(view, -1);
58 else if (strcmp(key, GNT_KEY_PGDOWN) == 0)
59 gnt_text_view_scroll(view, wid->priv.height - 2);
60 else if (strcmp(key, GNT_KEY_PGUP) == 0)
61 gnt_text_view_scroll(view, -(wid->priv.height - 2));
62 else
63 return FALSE;
64 return TRUE;
65 }
66 return FALSE;
67 }
68
69 static void
70 finch_debug_print(GaimDebugLevel level, const char *category,
71 const char *args)
72 {
73 if (debug.window && !debug.paused)
74 {
75 int pos = gnt_text_view_get_lines_below(GNT_TEXT_VIEW(debug.tview));
76 GntTextFormatFlags flag = GNT_TEXT_FLAG_NORMAL;
77
78 if (debug.timestamps) {
79 const char *mdate;
80 time_t mtime = time(NULL);
81 mdate = gaim_utf8_strftime("%H:%M:%S ", localtime(&mtime));
82 gnt_text_view_append_text_with_flags(GNT_TEXT_VIEW(debug.tview),
83 mdate, flag);
84 }
85
86 gnt_text_view_append_text_with_flags(GNT_TEXT_VIEW(debug.tview),
87 category, GNT_TEXT_FLAG_BOLD);
88 gnt_text_view_append_text_with_flags(GNT_TEXT_VIEW(debug.tview),
89 ": ", GNT_TEXT_FLAG_BOLD);
90
91 switch (level)
92 {
93 case GAIM_DEBUG_WARNING:
94 flag |= GNT_TEXT_FLAG_UNDERLINE;
95 case GAIM_DEBUG_ERROR:
96 case GAIM_DEBUG_FATAL:
97 flag |= GNT_TEXT_FLAG_BOLD;
98 break;
99 default:
100 break;
101 }
102
103 gnt_text_view_append_text_with_flags(GNT_TEXT_VIEW(debug.tview), args, flag);
104 if (pos <= 1)
105 gnt_text_view_scroll(GNT_TEXT_VIEW(debug.tview), 0);
106 }
107 }
108
109 static GaimDebugUiOps uiops =
110 {
111 finch_debug_print,
112 };
113
114 GaimDebugUiOps *finch_debug_get_ui_ops()
115 {
116 return &uiops;
117 }
118
119 static void
120 reset_debug_win(GntWidget *w, gpointer null)
121 {
122 debug.window = debug.tview = NULL;
123 }
124
125 static void
126 clear_debug_win(GntWidget *w, GntTextView *tv)
127 {
128 gnt_text_view_clear(tv);
129 }
130
131 static void
132 print_stderr(const char *string)
133 {
134 g_printerr("%s", string);
135 }
136
137 static void
138 suppress_error_messages(const char *message)
139 {}
140
141 static void
142 toggle_pause(GntWidget *w, gpointer n)
143 {
144 debug.paused = !debug.paused;
145 }
146
147 static void
148 toggle_timestamps(GntWidget *w, gpointer n)
149 {
150 debug.timestamps = !debug.timestamps;
151 gaim_prefs_set_bool("/core/debug/timestamps", debug.timestamps);
152 }
153
154 /* Xerox */
155 static void
156 gaim_glib_log_handler(const gchar *domain, GLogLevelFlags flags,
157 const gchar *msg, gpointer user_data)
158 {
159 GaimDebugLevel level;
160 char *new_msg = NULL;
161 char *new_domain = NULL;
162
163 if ((flags & G_LOG_LEVEL_ERROR) == G_LOG_LEVEL_ERROR)
164 level = GAIM_DEBUG_ERROR;
165 else if ((flags & G_LOG_LEVEL_CRITICAL) == G_LOG_LEVEL_CRITICAL)
166 level = GAIM_DEBUG_FATAL;
167 else if ((flags & G_LOG_LEVEL_WARNING) == G_LOG_LEVEL_WARNING)
168 level = GAIM_DEBUG_WARNING;
169 else if ((flags & G_LOG_LEVEL_MESSAGE) == G_LOG_LEVEL_MESSAGE)
170 level = GAIM_DEBUG_INFO;
171 else if ((flags & G_LOG_LEVEL_INFO) == G_LOG_LEVEL_INFO)
172 level = GAIM_DEBUG_INFO;
173 else if ((flags & G_LOG_LEVEL_DEBUG) == G_LOG_LEVEL_DEBUG)
174 level = GAIM_DEBUG_MISC;
175 else
176 {
177 gaim_debug_warning("gntdebug",
178 "Unknown glib logging level in %d\n", flags);
179
180 level = GAIM_DEBUG_MISC; /* This will never happen. */
181 }
182
183 if (msg != NULL)
184 new_msg = gaim_utf8_try_convert(msg);
185
186 if (domain != NULL)
187 new_domain = gaim_utf8_try_convert(domain);
188
189 if (new_msg != NULL)
190 {
191 gaim_debug(level, (new_domain != NULL ? new_domain : "g_log"),
192 "%s\n", new_msg);
193
194 g_free(new_msg);
195 }
196
197 g_free(new_domain);
198 }
199
200 static void
201 size_changed_cb(GntWidget *widget, int oldw, int oldh)
202 {
203 int w, h;
204 gnt_widget_get_size(widget, &w, &h);
205 gaim_prefs_set_int(PREF_ROOT "/size/width", w);
206 gaim_prefs_set_int(PREF_ROOT "/size/height", h);
207 }
208
209 void finch_debug_window_show()
210 {
211 debug.paused = FALSE;
212 debug.timestamps = gaim_prefs_get_bool("/core/debug/timestamps");
213 if (debug.window == NULL)
214 {
215 GntWidget *wid, *box;
216 debug.window = gnt_vbox_new(FALSE);
217 gnt_box_set_toplevel(GNT_BOX(debug.window), TRUE);
218 gnt_box_set_title(GNT_BOX(debug.window), _("Debug Window"));
219 gnt_box_set_pad(GNT_BOX(debug.window), 0);
220 gnt_box_set_alignment(GNT_BOX(debug.window), GNT_ALIGN_MID);
221
222 debug.tview = gnt_text_view_new();
223 gnt_box_add_widget(GNT_BOX(debug.window), debug.tview);
224 gnt_widget_set_size(debug.tview,
225 gaim_prefs_get_int(PREF_ROOT "/size/width"),
226 gaim_prefs_get_int(PREF_ROOT "/size/height"));
227 g_signal_connect(G_OBJECT(debug.tview), "size_changed", G_CALLBACK(size_changed_cb), NULL);
228
229 gnt_box_add_widget(GNT_BOX(debug.window), gnt_line_new(FALSE));
230
231 box = gnt_hbox_new(FALSE);
232 gnt_box_set_alignment(GNT_BOX(box), GNT_ALIGN_MID);
233 gnt_box_set_fill(GNT_BOX(box), FALSE);
234
235 /* XXX: Setting the GROW_Y for the following widgets don't make sense. But right now
236 * it's necessary to make the width of the debug window resizable ... like I said,
237 * it doesn't make sense. The bug is likely in the packing in gntbox.c.
238 */
239 wid = gnt_button_new(_("Clear"));
240 g_signal_connect(G_OBJECT(wid), "activate", G_CALLBACK(clear_debug_win), debug.tview);
241 GNT_WIDGET_SET_FLAGS(wid, GNT_WIDGET_GROW_Y);
242 gnt_box_add_widget(GNT_BOX(box), wid);
243
244 wid = gnt_check_box_new(_("Pause"));
245 g_signal_connect(G_OBJECT(wid), "toggled", G_CALLBACK(toggle_pause), NULL);
246 GNT_WIDGET_SET_FLAGS(wid, GNT_WIDGET_GROW_Y);
247 gnt_box_add_widget(GNT_BOX(box), wid);
248
249 wid = gnt_check_box_new(_("Timestamps"));
250 gnt_check_box_set_checked(GNT_CHECK_BOX(wid), debug.timestamps);
251 g_signal_connect(G_OBJECT(wid), "toggled", G_CALLBACK(toggle_timestamps), NULL);
252 GNT_WIDGET_SET_FLAGS(wid, GNT_WIDGET_GROW_Y);
253 gnt_box_add_widget(GNT_BOX(box), wid);
254
255 gnt_box_add_widget(GNT_BOX(debug.window), box);
256 GNT_WIDGET_SET_FLAGS(box, GNT_WIDGET_GROW_Y);
257
258 gnt_widget_set_name(debug.window, "debug-window");
259
260 g_signal_connect(G_OBJECT(debug.window), "destroy", G_CALLBACK(reset_debug_win), NULL);
261 g_signal_connect(G_OBJECT(debug.window), "key_pressed", G_CALLBACK(debug_window_kpress_cb), debug.tview);
262 }
263
264 gnt_widget_show(debug.window);
265 }
266
267 static gboolean
268 start_with_debugwin(gpointer null)
269 {
270 finch_debug_window_show();
271 return FALSE;
272 }
273
274 void finch_debug_init()
275 {
276 /* Xerox */
277 #define REGISTER_G_LOG_HANDLER(name) \
278 g_log_set_handler((name), G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL \
279 | G_LOG_FLAG_RECURSION, \
280 gaim_glib_log_handler, NULL)
281
282 /* Register the glib log handlers. */
283 REGISTER_G_LOG_HANDLER(NULL);
284 REGISTER_G_LOG_HANDLER("GLib");
285 REGISTER_G_LOG_HANDLER("GModule");
286 REGISTER_G_LOG_HANDLER("GLib-GObject");
287 REGISTER_G_LOG_HANDLER("GThread");
288
289 g_set_print_handler(print_stderr); /* Redirect the debug messages to stderr */
290 g_set_printerr_handler(suppress_error_messages);
291
292 gaim_prefs_add_none(PREF_ROOT);
293 gaim_prefs_add_none(PREF_ROOT "/size");
294 gaim_prefs_add_int(PREF_ROOT "/size/width", 60);
295 gaim_prefs_add_int(PREF_ROOT "/size/height", 15);
296
297 if (gaim_debug_is_enabled())
298 g_timeout_add(0, start_with_debugwin, NULL);
299 }
300
301 void finch_debug_uninit()
302 {
303 }
304