[gnome-software] Restore the old log handler when quitting



commit 268ec251a66dd74ffb7cd4c2e092ede2c3428811
Author: Richard Hughes <richard hughsie com>
Date:   Tue Apr 26 13:37:07 2016 +0100

    Restore the old log handler when quitting
    
    This fixes the hang when a new instance of gnome-software is started with --verbose.
    
    This commit converts the GsDebug struct to a GObject and thus hides all the
    implementation of the GsDebug internal data.

 src/gs-debug.c |   75 +++++++++++++++++++++++++++++++++++--------------------
 src/gs-debug.h |   11 ++------
 2 files changed, 51 insertions(+), 35 deletions(-)
---
diff --git a/src/gs-debug.c b/src/gs-debug.c
index 2efe98d..acd6bc0 100644
--- a/src/gs-debug.c
+++ b/src/gs-debug.c
@@ -19,11 +19,24 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
+#include "config.h"
+
 #include <stdio.h>
 #include <unistd.h>
 
 #include "gs-debug.h"
 
+struct _GsDebug
+{
+       GObject          parent_instance;
+       GMutex           mutex;
+       gboolean         use_time;
+       gboolean         use_color;
+       GLogFunc         log_func_old;
+};
+
+G_DEFINE_TYPE (GsDebug, gs_debug, G_TYPE_OBJECT)
+
 /**
  * gs_debug_handler_cb:
  **/
@@ -99,41 +112,49 @@ gs_debug_handler_cb (const gchar *log_domain,
 }
 
 /**
- * gs_debug_new:
+ * gs_debug_finalize:
  **/
-GsDebug *
-gs_debug_new (void)
+static void
+gs_debug_finalize (GObject *object)
 {
-       GsDebug *debug = g_new0 (GsDebug, 1);
-       guint i;
-       const gchar *log_domains[] = {
-               G_LOG_DOMAIN,
-               "As",
-               "Gtk",
-               "GsPlugin",
-               "PackageKit",
-               NULL };
+       GsDebug *debug = GS_DEBUG (object);
+
+       if (debug->log_func_old != NULL)
+               g_log_set_default_handler (debug->log_func_old, NULL);
+       g_mutex_clear (&debug->mutex);
+
+       G_OBJECT_CLASS (gs_debug_parent_class)->finalize (object);
+}
 
+/**
+ * gs_debug_class_init:
+ **/
+static void
+gs_debug_class_init (GsDebugClass *klass)
+{
+       GObjectClass *object_class = G_OBJECT_CLASS (klass);
+       object_class->finalize = gs_debug_finalize;
+}
+
+/**
+ * gs_debug_init:
+ **/
+static void
+gs_debug_init (GsDebug *debug)
+{
+       g_mutex_init (&debug->mutex);
        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;
+       debug->log_func_old = g_log_set_default_handler (gs_debug_handler_cb, debug);
 }
 
 /**
- * gs_debug_free:
+ * gs_debug_new:
  **/
-void
-gs_debug_free (GsDebug *debug)
+GsDebug *
+gs_debug_new (void)
 {
-       g_mutex_clear (&debug->mutex);
-       g_free (debug);
+       return GS_DEBUG (g_object_new (GS_TYPE_DEBUG, NULL));
 }
+
+/* vim: set noexpandtab: */
diff --git a/src/gs-debug.h b/src/gs-debug.h
index b6e03b3..b0bdc50 100644
--- a/src/gs-debug.h
+++ b/src/gs-debug.h
@@ -26,16 +26,11 @@
 
 G_BEGIN_DECLS
 
-typedef struct {
-       GMutex           mutex;
-       gboolean         use_time;
-       gboolean         use_color;
-} GsDebug;
+#define GS_TYPE_DEBUG (gs_debug_get_type ())
 
-GsDebug                *gs_debug_new   (void);
-void            gs_debug_free  (GsDebug        *debug);
+G_DECLARE_FINAL_TYPE (GsDebug, gs_debug, GS, DEBUG, GObject)
 
-G_DEFINE_AUTOPTR_CLEANUP_FUNC(GsDebug, gs_debug_free);
+GsDebug                *gs_debug_new           (void);
 
 G_END_DECLS
 


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