/pidgin/main: d31a4f3b547e: Debug log: optional colored output a...

Tomasz Wasilczyk tomkiewicz at cpw.pidgin.im
Fri Aug 16 03:29:13 EDT 2013


Changeset: d31a4f3b547e20477ce35ac949a11774bd215f46
Author:	 Tomasz Wasilczyk <tomkiewicz at cpw.pidgin.im>
Date:	 2013-08-16 09:29 +0200
Branch:	 default
URL: https://hg.pidgin.im/pidgin/main/rev/d31a4f3b547e

Description:

Debug log: optional colored output and no need for add linefeed at the end of each message

diffstat:

 libpurple/debug.c |  32 ++++++++++++++++++++++++++++++--
 libpurple/debug.h |   7 +++++++
 pidgin/gtkmain.c  |  10 +++++++---
 3 files changed, 44 insertions(+), 5 deletions(-)

diffs (138 lines):

diff --git a/libpurple/debug.c b/libpurple/debug.c
--- a/libpurple/debug.c
+++ b/libpurple/debug.c
@@ -50,6 +50,8 @@ static gboolean debug_enabled = FALSE;
 static gboolean debug_verbose = FALSE;
 static gboolean debug_unsafe = FALSE;
 
+static gboolean debug_colored = FALSE;
+
 static void
 purple_debug_vargs(PurpleDebugLevel level, const char *category,
 				 const char *format, va_list args)
@@ -67,20 +69,40 @@ purple_debug_vargs(PurpleDebugLevel leve
 		return;
 
 	arg_s = g_strdup_vprintf(format, args);
+	g_strchomp(arg_s); /* strip trailing linefeeds */
 
 	if (debug_enabled) {
 		gchar *ts_s;
 		const char *mdate;
 		time_t mtime = time(NULL);
+		const gchar *format_pre, *format_post;
 
+		format_pre = "";
+		format_post = "";
+
+		if (!debug_colored)
+			format_pre = "";
+		else if (level == PURPLE_DEBUG_MISC)
+			format_pre = "\033[0;37m";
+		else if (level == PURPLE_DEBUG_INFO)
+			format_pre = "";
+		else if (level == PURPLE_DEBUG_WARNING)
+			format_pre = "\033[0;33m";
+		else if (level == PURPLE_DEBUG_ERROR)
+			format_pre = "\033[1;31m";
+		else if (level == PURPLE_DEBUG_FATAL)
+			format_pre = "\033[1;33;41m";
+
+		if (format_pre[0] != '\0')
+			format_post = "\033[0m";
 
 		mdate = purple_utf8_strftime("%H:%M:%S", localtime(&mtime));
 		ts_s = g_strdup_printf("(%s) ", mdate);
 
 		if (category == NULL)
-			g_print("%s%s", ts_s, arg_s);
+			g_print("%s%s%s%s\n", format_pre, ts_s, arg_s, format_post);
 		else
-			g_print("%s%s: %s", ts_s, category, arg_s);
+			g_print("%s%s%s: %s%s\n", format_pre, ts_s, category, arg_s, format_post);
 
 		g_free(ts_s);
 	}
@@ -207,6 +229,12 @@ purple_debug_set_unsafe(gboolean unsafe)
 	debug_unsafe = unsafe;
 }
 
+void
+purple_debug_set_colored(gboolean colored)
+{
+	debug_colored = colored;
+}
+
 PurpleDebugUiOps *
 purple_debug_get_ui_ops(void)
 {
diff --git a/libpurple/debug.h b/libpurple/debug.h
--- a/libpurple/debug.h
+++ b/libpurple/debug.h
@@ -189,6 +189,13 @@ void purple_debug_set_unsafe(gboolean un
  */
 gboolean purple_debug_is_unsafe(void);
 
+/**
+ * Enable or disable colored output for bash console.
+ *
+ * @param colored TRUE to enable colored output, FALSE to disable it.
+ */
+void purple_debug_set_colored(gboolean colored);
+
 /*@}*/
 
 /**************************************************************************/
diff --git a/pidgin/gtkmain.c b/pidgin/gtkmain.c
--- a/pidgin/gtkmain.c
+++ b/pidgin/gtkmain.c
@@ -388,7 +388,7 @@ show_usage(const char *name, gboolean te
 		g_string_append_printf(str, _("Usage: %s [OPTION]...\n\n"), name);
 		g_string_append_printf(str, "  -c, --config=%s    %s\n",
 				_("DIR"), _("use DIR for config files"));
-		g_string_append_printf(str, "  -d, --debug         %s\n",
+		g_string_append_printf(str, "  -d, --debug[=colored] %s\n",
 				_("print debugging messages to stdout"));
 		g_string_append_printf(str, "  -f, --force-online  %s\n",
 				_("force online, regardless of network status"));
@@ -458,13 +458,13 @@ int main(int argc, char *argv[])
 #endif
 	int opt;
 	gboolean gui_check;
-	gboolean debug_enabled;
+	gboolean debug_enabled, debug_colored;
 	GList *active_accounts;
 	GStatBuf st;
 
 	struct option long_options[] = {
 		{"config",       required_argument, NULL, 'c'},
-		{"debug",        no_argument,       NULL, 'd'},
+		{"debug",        optional_argument, NULL, 'd'},
 		{"force-online", no_argument,       NULL, 'f'},
 		{"help",         no_argument,       NULL, 'h'},
 		{"login",        optional_argument, NULL, 'l'},
@@ -477,6 +477,7 @@ int main(int argc, char *argv[])
 		{0, 0, 0, 0}
 	};
 
+	debug_colored = FALSE;
 #ifdef DEBUG
 	debug_enabled = TRUE;
 #else
@@ -624,6 +625,8 @@ int main(int argc, char *argv[])
 			break;
 		case 'd':	/* debug */
 			debug_enabled = TRUE;
+			if (g_strcmp0(optarg, "colored") == 0)
+				debug_colored = TRUE;
 			break;
 		case 'f':	/* force-online */
 			opt_force_online = TRUE;
@@ -703,6 +706,7 @@ int main(int argc, char *argv[])
 	 */
 
 	purple_debug_set_enabled(debug_enabled);
+	purple_debug_set_colored(debug_colored);
 
 #if !GTK_CHECK_VERSION(3,0,0)
 	search_path = g_build_filename(purple_user_dir(), "gtkrc-2.0", NULL);



More information about the Commits mailing list