[gnome-software] Show debugging logs in color



commit 662ed8e738b9af00400e4af26b3e29a921aa81a9
Author: Richard Hughes <richard hughsie com>
Date:   Tue Apr 12 11:38:17 2016 +0100

    Show debugging logs in color

 src/Makefile.am |    3 +
 src/gs-cmd.c    |    2 +
 src/gs-debug.c  |  132 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/gs-debug.h  |   44 ++++++++++++++++++
 src/gs-main.c   |    5 ++-
 5 files changed, 185 insertions(+), 1 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 6c4301d..82deb31 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -87,6 +87,7 @@ gnome_software_cmd_SOURCES =                          \
        gs-app.c                                        \
        gs-review.c                                     \
        gs-cmd.c                                        \
+       gs-debug.c                                      \
        gs-utils.c                                      \
        gs-os-release.c                                 \
        gs-plugin-loader.c                              \
@@ -114,6 +115,8 @@ gnome_software_SOURCES =                            \
        gs-app.h                                        \
        gs-category.c                                   \
        gs-category.h                                   \
+       gs-debug.c                                      \
+       gs-debug.h                                      \
        gs-app-addon-row.c                              \
        gs-app-addon-row.h                              \
        gs-app-row.c                                    \
diff --git a/src/gs-cmd.c b/src/gs-cmd.c
index d1d3af8..d626677 100644
--- a/src/gs-cmd.c
+++ b/src/gs-cmd.c
@@ -26,6 +26,7 @@
 #include <gtk/gtk.h>
 #include <locale.h>
 
+#include "gs-debug.h"
 #include "gs-plugin-loader.h"
 #include "gs-plugin-loader-sync.h"
 
@@ -205,6 +206,7 @@ main (int argc, char **argv)
        gint repeat = 1;
        int status = 0;
        g_autoptr(GError) error = NULL;
+       g_autoptr(GsDebug) debug = gs_debug_new ();
        g_autofree gchar *refine_flags_str = NULL;
        g_autoptr(GsApp) app = NULL;
        g_autoptr(GsPluginLoader) plugin_loader = NULL;
diff --git a/src/gs-debug.c b/src/gs-debug.c
new file mode 100644
index 0000000..abc5bec
--- /dev/null
+++ b/src/gs-debug.c
@@ -0,0 +1,132 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2016 Richard Hughes <richard hughsie com>
+ *
+ * Licensed under the GNU General Public License Version 2
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include <stdio.h>
+#include <unistd.h>
+
+#include "gs-debug.h"
+
+/**
+ * gs_debug_handler_cb:
+ **/
+static void
+gs_debug_handler_cb (const gchar *log_domain,
+                    GLogLevelFlags log_level,
+                    const gchar *message,
+                    gpointer user_data)
+{
+       GsDebug *debug = (GsDebug *) user_data;
+       guint i;
+       g_autofree gchar *tmp = NULL;
+       g_autoptr(GDateTime) dt = g_date_time_new_now_utc ();
+       g_autoptr(GString) domain = NULL;
+       g_autoptr(GMutexLocker) locker = g_mutex_locker_new (&debug->mutex);
+
+       /* time header */
+       if (debug->use_time) {
+               tmp = g_strdup_printf ("%02i:%02i:%02i:%04i",
+                                      g_date_time_get_hour (dt),
+                                      g_date_time_get_minute (dt),
+                                      g_date_time_get_second (dt),
+                                      g_date_time_get_microsecond (dt) / 1000);
+       }
+
+       /* make these shorter */
+       if (g_strcmp0 (log_domain, "PackageKit") == 0) {
+               log_domain = "PK";
+       } else if (g_strcmp0 (log_domain, "GsPlugin") == 0) {
+               log_domain = "Gs";
+       }
+
+       /* pad out domain */
+       domain = g_string_new (log_domain);
+       for (i = domain->len; i < 3; i++)
+               g_string_append (domain, " ");
+
+       /* to file */
+       if (!debug->use_color) {
+               if (tmp != NULL)
+                       g_print ("%s ", tmp);
+               g_print ("%s ", domain->str);
+               g_print ("%s\n", message);
+               return;
+       }
+
+       /* to screen */
+       switch (log_level) {
+       case G_LOG_LEVEL_ERROR:
+       case G_LOG_LEVEL_CRITICAL:
+       case G_LOG_LEVEL_WARNING:
+               /* critical in red */
+               if (tmp != NULL)
+                       g_print ("%c[%dm%s ", 0x1B, 32, tmp);
+               g_print ("%s ", domain->str);
+               g_print ("%c[%dm%s\n%c[%dm", 0x1B, 31, message, 0x1B, 0);
+               break;
+       default:
+               /* debug in blue */
+               if (tmp != NULL)
+                       g_print ("%c[%dm%s ", 0x1B, 32, tmp);
+               g_print ("%s ", domain->str);
+               g_print ("%c[%dm%s\n%c[%dm", 0x1B, 34, message, 0x1B, 0);
+               break;
+       }
+}
+
+/**
+ * gs_debug_new:
+ **/
+GsDebug *
+gs_debug_new (void)
+{
+       GsDebug *debug = g_new0 (GsDebug, 1);
+       guint i;
+       const gchar *log_domains[] = {
+               G_LOG_DOMAIN,
+               "As",
+               "Gtk",
+               "GsPlugin",
+               "PackageKit",
+               NULL };
+
+       debug->use_time = g_getenv ("GS_DEBUG_NO_TIME") == NULL;
+       debug->use_color = (isatty (fileno (stdout)) == 1);
+       g_mutex_init (&debug->mutex);
+       for (i = 0; log_domains[i] != NULL; i++) {
+               g_log_set_handler (log_domains[i],
+                                  G_LOG_LEVEL_ERROR |
+                                  G_LOG_LEVEL_CRITICAL |
+                                  G_LOG_LEVEL_DEBUG |
+                                  G_LOG_LEVEL_WARNING,
+                                  gs_debug_handler_cb, debug);
+       }
+       return debug;
+}
+
+/**
+ * gs_debug_free:
+ **/
+void
+gs_debug_free (GsDebug *debug)
+{
+       g_mutex_clear (&debug->mutex);
+       g_free (debug);
+}
diff --git a/src/gs-debug.h b/src/gs-debug.h
new file mode 100644
index 0000000..b6e03b3
--- /dev/null
+++ b/src/gs-debug.h
@@ -0,0 +1,44 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2016 Richard Hughes <richard hughsie com>
+ *
+ * Licensed under the GNU General Public License Version 2
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef __GS_DEBUG_H
+#define __GS_DEBUG_H
+
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+typedef struct {
+       GMutex           mutex;
+       gboolean         use_time;
+       gboolean         use_color;
+} GsDebug;
+
+GsDebug                *gs_debug_new   (void);
+void            gs_debug_free  (GsDebug        *debug);
+
+G_DEFINE_AUTOPTR_CLEANUP_FUNC(GsDebug, gs_debug_free);
+
+G_END_DECLS
+
+#endif /* __GS_DEBUG_H */
+
+/* vim: set noexpandtab: */
diff --git a/src/gs-main.c b/src/gs-main.c
index 2a7bd3a..bd6bda9 100644
--- a/src/gs-main.c
+++ b/src/gs-main.c
@@ -29,12 +29,14 @@
 #include <locale.h>
 
 #include "gs-application.h"
+#include "gs-debug.h"
 
 int
 main (int argc, char **argv)
 {
        int status = 0;
        g_autoptr(GsApplication) application = NULL;
+       g_autoptr(GsDebug) debug = gs_debug_new ();
        g_autoptr(AsProfile) profile = NULL;
        g_autoptr(AsProfileTask) ptask = NULL;
 
@@ -46,9 +48,10 @@ main (int argc, char **argv)
 
        profile = as_profile_new ();
        ptask = as_profile_start_literal (profile, "GsMain");
+
+       /* redirect logs */
        application = gs_application_new ();
        status = g_application_run (G_APPLICATION (application), argc, argv);
-
        return status;
 }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]