Mercurial > pidgin
annotate finch/gntdebug.c @ 15870:66dff3dfdea6
Re-sed the copyright notices so they don't all talk about Purple.
| author | Richard Laager <rlaager@wiktel.com> |
|---|---|
| date | Wed, 21 Mar 2007 09:00:54 +0000 |
| parents | 32c366eeeb99 |
| children | 6dc5dc83a61b |
| rev | line source |
|---|---|
| 15817 | 1 /** |
| 2 * @file gntdebug.c GNT Debug API | |
| 3 * @ingroup gntui | |
| 4 * | |
|
15870
66dff3dfdea6
Re-sed the copyright notices so they don't all talk about Purple.
Richard Laager <rlaager@wiktel.com>
parents:
15822
diff
changeset
|
5 * finch |
| 15817 | 6 * |
|
15870
66dff3dfdea6
Re-sed the copyright notices so they don't all talk about Purple.
Richard Laager <rlaager@wiktel.com>
parents:
15822
diff
changeset
|
7 * Finch is the legal property of its developers, whose names are too numerous |
| 15817 | 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" | |
| 15822 | 33 #include "finch.h" |
| 15817 | 34 #include "util.h" |
| 35 | |
| 36 #include <stdio.h> | |
| 37 #include <string.h> | |
| 38 | |
| 15822 | 39 #define PREF_ROOT "/purple/gnt/debug" |
| 15817 | 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 | |
| 15822 | 70 finch_debug_print(PurpleDebugLevel level, const char *category, |
| 15817 | 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); | |
| 15822 | 81 mdate = purple_utf8_strftime("%H:%M:%S ", localtime(&mtime)); |
| 15817 | 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 { | |
| 15822 | 93 case PURPLE_DEBUG_WARNING: |
| 15817 | 94 flag |= GNT_TEXT_FLAG_UNDERLINE; |
| 15822 | 95 case PURPLE_DEBUG_ERROR: |
| 96 case PURPLE_DEBUG_FATAL: | |
| 15817 | 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 | |
| 15822 | 109 static PurpleDebugUiOps uiops = |
| 15817 | 110 { |
| 111 finch_debug_print, | |
| 112 }; | |
| 113 | |
| 15822 | 114 PurpleDebugUiOps *finch_debug_get_ui_ops() |
| 15817 | 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; | |
| 15822 | 151 purple_prefs_set_bool("/core/debug/timestamps", debug.timestamps); |
| 15817 | 152 } |
| 153 | |
| 154 /* Xerox */ | |
| 155 static void | |
| 15822 | 156 purple_glib_log_handler(const gchar *domain, GLogLevelFlags flags, |
| 15817 | 157 const gchar *msg, gpointer user_data) |
| 158 { | |
| 15822 | 159 PurpleDebugLevel level; |
| 15817 | 160 char *new_msg = NULL; |
| 161 char *new_domain = NULL; | |
| 162 | |
| 163 if ((flags & G_LOG_LEVEL_ERROR) == G_LOG_LEVEL_ERROR) | |
| 15822 | 164 level = PURPLE_DEBUG_ERROR; |
| 15817 | 165 else if ((flags & G_LOG_LEVEL_CRITICAL) == G_LOG_LEVEL_CRITICAL) |
| 15822 | 166 level = PURPLE_DEBUG_FATAL; |
| 15817 | 167 else if ((flags & G_LOG_LEVEL_WARNING) == G_LOG_LEVEL_WARNING) |
| 15822 | 168 level = PURPLE_DEBUG_WARNING; |
| 15817 | 169 else if ((flags & G_LOG_LEVEL_MESSAGE) == G_LOG_LEVEL_MESSAGE) |
| 15822 | 170 level = PURPLE_DEBUG_INFO; |
| 15817 | 171 else if ((flags & G_LOG_LEVEL_INFO) == G_LOG_LEVEL_INFO) |
| 15822 | 172 level = PURPLE_DEBUG_INFO; |
| 15817 | 173 else if ((flags & G_LOG_LEVEL_DEBUG) == G_LOG_LEVEL_DEBUG) |
| 15822 | 174 level = PURPLE_DEBUG_MISC; |
| 15817 | 175 else |
| 176 { | |
| 15822 | 177 purple_debug_warning("gntdebug", |
| 15817 | 178 "Unknown glib logging level in %d\n", flags); |
| 179 | |
| 15822 | 180 level = PURPLE_DEBUG_MISC; /* This will never happen. */ |
| 15817 | 181 } |
| 182 | |
| 183 if (msg != NULL) | |
| 15822 | 184 new_msg = purple_utf8_try_convert(msg); |
| 15817 | 185 |
| 186 if (domain != NULL) | |
| 15822 | 187 new_domain = purple_utf8_try_convert(domain); |
| 15817 | 188 |
| 189 if (new_msg != NULL) | |
| 190 { | |
| 15822 | 191 purple_debug(level, (new_domain != NULL ? new_domain : "g_log"), |
| 15817 | 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); | |
| 15822 | 205 purple_prefs_set_int(PREF_ROOT "/size/width", w); |
| 206 purple_prefs_set_int(PREF_ROOT "/size/height", h); | |
| 15817 | 207 } |
| 208 | |
| 209 void finch_debug_window_show() | |
| 210 { | |
| 211 debug.paused = FALSE; | |
| 15822 | 212 debug.timestamps = purple_prefs_get_bool("/core/debug/timestamps"); |
| 15817 | 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, | |
| 15822 | 225 purple_prefs_get_int(PREF_ROOT "/size/width"), |
| 226 purple_prefs_get_int(PREF_ROOT "/size/height")); | |
| 15817 | 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, \ | |
| 15822 | 280 purple_glib_log_handler, NULL) |
| 15817 | 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 | |
| 15822 | 292 purple_prefs_add_none(PREF_ROOT); |
| 293 purple_prefs_add_none(PREF_ROOT "/size"); | |
| 294 purple_prefs_add_int(PREF_ROOT "/size/width", 60); | |
| 295 purple_prefs_add_int(PREF_ROOT "/size/height", 15); | |
| 15817 | 296 |
| 15822 | 297 if (purple_debug_is_enabled()) |
| 15817 | 298 g_timeout_add(0, start_with_debugwin, NULL); |
| 299 } | |
| 300 | |
| 301 void finch_debug_uninit() | |
| 302 { | |
| 303 } | |
| 304 |
