[gimp] app: add gimp_log_{set,remove}_handler()



commit a143bfdf1df9190a869db022352a7d360236d44a
Author: Ell <ell_se yahoo com>
Date:   Sun Aug 2 10:52:34 2020 +0300

    app: add gimp_log_{set,remove}_handler()
    
    ... which set/remove a GLIB log handler for all domains used by
    GIMP.
    
    Use the new functions in errors.c, instead of using
    g_log_set_handler() directly.

 app/errors.c   | 89 ++++++++++++++++++----------------------------------------
 app/gimp-log.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++
 app/gimp-log.h | 31 ++++++++++++--------
 3 files changed, 125 insertions(+), 73 deletions(-)
---
diff --git a/app/errors.c b/app/errors.c
index 1c57814807..e9756d333b 100644
--- a/app/errors.c
+++ b/app/errors.c
@@ -46,6 +46,7 @@
 #include "pdb/gimppdb.h"
 
 #include "errors.h"
+#include "gimp-log.h"
 
 #ifdef G_OS_WIN32
 #include <windows.h>
@@ -53,49 +54,15 @@
 
 /*  private variables  */
 
-static const gchar * const log_domains[] =
-{
-  "Gimp",
-  "Gimp-Actions",
-  "Gimp-Base",
-  "Gimp-Composite",
-  "Gimp-Config",
-  "Gimp-Core",
-  "Gimp-Dialogs",
-  "Gimp-Display",
-  "Gimp-File",
-  "Gimp-GEGL",
-  "Gimp-GUI",
-  "Gimp-Menus",
-  "Gimp-Operations",
-  "Gimp-PDB",
-  "Gimp-Paint",
-  "Gimp-Paint-Funcs",
-  "Gimp-Plug-In",
-  "Gimp-Text",
-  "Gimp-Tools",
-  "Gimp-Vectors",
-  "Gimp-Widgets",
-  "Gimp-XCF",
-  "LibGimpBase",
-  "LibGimpColor",
-  "LibGimpConfig",
-  "LibGimpMath",
-  "LibGimpModule",
-  "LibGimpThumb",
-  "LibGimpWidgets"
-};
-
-static Gimp                *the_errors_gimp   = NULL;
-static gboolean             use_debug_handler = FALSE;
-static GimpStackTraceMode   stack_trace_mode  = GIMP_STACK_TRACE_QUERY;
-static gchar               *full_prog_name    = NULL;
-static gchar               *backtrace_file    = NULL;
-static gchar               *backup_path       = NULL;
-static GFile               *backup_file       = NULL;
-static guint                log_domain_handler_ids[G_N_ELEMENTS (log_domains)];
-static guint                gegl_handler_id   = 0;
-static guint                global_handler_id = 0;
+static Gimp                *the_errors_gimp    = NULL;
+static gboolean             use_debug_handler  = FALSE;
+static GimpStackTraceMode   stack_trace_mode   = GIMP_STACK_TRACE_QUERY;
+static gchar               *full_prog_name     = NULL;
+static gchar               *backtrace_file     = NULL;
+static gchar               *backup_path        = NULL;
+static GFile               *backup_file        = NULL;
+static GimpLogHandler       log_domain_handler = 0;
+static guint                global_handler_id  = 0;
 
 
 /*  local function prototypes  */
@@ -123,8 +90,6 @@ errors_init (Gimp               *gimp,
              GimpStackTraceMode  _stack_trace_mode,
              const gchar        *_backtrace_file)
 {
-  gint i;
-
   g_return_if_fail (GIMP_IS_GIMP (gimp));
   g_return_if_fail (_full_prog_name != NULL);
   g_return_if_fail (full_prog_name == NULL);
@@ -154,18 +119,12 @@ errors_init (Gimp               *gimp,
 
   backup_file = g_file_new_for_path (backup_path);
 
-  for (i = 0; i < G_N_ELEMENTS (log_domains); i++)
-    log_domain_handler_ids[i] = g_log_set_handler (log_domains[i],
-                                                   G_LOG_LEVEL_WARNING |
-                                                   G_LOG_LEVEL_MESSAGE |
-                                                   G_LOG_LEVEL_CRITICAL,
-                                                   gimp_message_log_func, gimp);
-
-  gegl_handler_id   = g_log_set_handler ("GEGL",
-                                         G_LOG_LEVEL_WARNING |
-                                         G_LOG_LEVEL_MESSAGE |
-                                         G_LOG_LEVEL_CRITICAL,
-                                         gimp_message_log_func, gimp);
+  log_domain_handler = gimp_log_set_handler (FALSE,
+                                             G_LOG_LEVEL_WARNING |
+                                             G_LOG_LEVEL_MESSAGE |
+                                             G_LOG_LEVEL_CRITICAL,
+                                             gimp_message_log_func, gimp);
+
   global_handler_id = g_log_set_handler (NULL,
                                          G_LOG_LEVEL_ERROR | G_LOG_FLAG_FATAL,
                                          gimp_error_log_func, gimp);
@@ -174,13 +133,19 @@ errors_init (Gimp               *gimp,
 void
 errors_exit (void)
 {
-  gint i;
+  if (log_domain_handler)
+    {
+      gimp_log_remove_handler (log_domain_handler);
 
-  for (i = 0; i < G_N_ELEMENTS (log_domains); i++)
-    g_log_remove_handler (log_domains[i], log_domain_handler_ids[i]);
+      log_domain_handler = 0;
+    }
+
+  if (global_handler_id)
+    {
+      g_log_remove_handler (NULL, global_handler_id);
 
-  g_log_remove_handler ("GEGL", gegl_handler_id);
-  g_log_remove_handler (NULL, global_handler_id);
+      global_handler_id = 0;
+    }
 
   the_errors_gimp = NULL;
 
diff --git a/app/gimp-log.c b/app/gimp-log.c
index eddfd513a8..30a74a4c18 100644
--- a/app/gimp-log.c
+++ b/app/gimp-log.c
@@ -48,6 +48,41 @@ static const GDebugKey log_keys[] =
   { "xcf",                GIMP_LOG_XCF                }
 };
 
+static const gchar * const log_domains[] =
+{
+  "Gimp",
+  "Gimp-Actions",
+  "Gimp-Base",
+  "Gimp-Composite",
+  "Gimp-Config",
+  "Gimp-Core",
+  "Gimp-Dialogs",
+  "Gimp-Display",
+  "Gimp-File",
+  "Gimp-GEGL",
+  "Gimp-GUI",
+  "Gimp-Menus",
+  "Gimp-Operations",
+  "Gimp-PDB",
+  "Gimp-Paint",
+  "Gimp-Paint-Funcs",
+  "Gimp-Plug-In",
+  "Gimp-Text",
+  "Gimp-Tools",
+  "Gimp-Vectors",
+  "Gimp-Widgets",
+  "Gimp-XCF",
+  "LibGimpBase",
+  "LibGimpColor",
+  "LibGimpConfig",
+  "LibGimpMath",
+  "LibGimpModule",
+  "LibGimpThumb",
+  "LibGimpWidgets",
+  "GEGL",
+  NULL
+};
+
 
 GimpLogFlags gimp_log_flags = 0;
 
@@ -139,3 +174,46 @@ gimp_logv (GimpLogFlags  flags,
 
   g_free (message);
 }
+
+GimpLogHandler
+gimp_log_set_handler (gboolean       global,
+                      GLogLevelFlags log_levels,
+                      GLogFunc       log_func,
+                      gpointer       user_data)
+{
+  GimpLogHandler handler;
+  gint           n;
+  gint           i;
+
+  g_return_val_if_fail (log_func != NULL, NULL);
+
+  n = G_N_ELEMENTS (log_domains) - (global ? 1 : 0);
+
+  handler = g_new (guint, n + 1);
+
+  handler[0] = n;
+
+  for (i = 0; i < n; i++)
+    {
+      handler[i + 1] = g_log_set_handler (log_domains[i], log_levels,
+                                          log_func, user_data);
+    }
+
+  return handler;
+}
+
+void
+gimp_log_remove_handler (GimpLogHandler handler)
+{
+  gint n;
+  gint i;
+
+  g_return_if_fail (handler != NULL);
+
+  n = handler[0];
+
+  for (i = 0; i < n; i++)
+    g_log_remove_handler (log_domains[i], handler[i + 1]);
+
+  g_free (handler);
+}
diff --git a/app/gimp-log.h b/app/gimp-log.h
index 39ce8dd22a..41e0589f2c 100644
--- a/app/gimp-log.h
+++ b/app/gimp-log.h
@@ -19,6 +19,9 @@
 #define __GIMP_LOG_H__
 
 
+typedef guint *GimpLogHandler;
+
+
 typedef enum
 {
   GIMP_LOG_TOOL_EVENTS        = 1 << 0,
@@ -49,17 +52,23 @@ typedef enum
 extern GimpLogFlags gimp_log_flags;
 
 
-void   gimp_log_init (void);
-void   gimp_log      (GimpLogFlags  flags,
-                      const gchar  *function,
-                      gint          line,
-                      const gchar  *format,
-                      ...) G_GNUC_PRINTF (4, 5);
-void   gimp_logv     (GimpLogFlags  flags,
-                      const gchar  *function,
-                      gint          line,
-                      const gchar  *format,
-                      va_list       args) G_GNUC_PRINTF (4, 0);
+void             gimp_log_init           (void);
+void             gimp_log                (GimpLogFlags    flags,
+                                          const gchar    *function,
+                                          gint            line,
+                                          const gchar    *format,
+                                          ...) G_GNUC_PRINTF (4, 5);
+void             gimp_logv               (GimpLogFlags    flags,
+                                          const gchar    *function,
+                                          gint            line,
+                                          const gchar    *format,
+                                          va_list         args) G_GNUC_PRINTF (4, 0);
+
+GimpLogHandler   gimp_log_set_handler    (gboolean        global,
+                                          GLogLevelFlags  log_levels,
+                                          GLogFunc        log_func,
+                                          gpointer        user_data);
+void             gimp_log_remove_handler (GimpLogHandler  handler);
 
 
 #ifdef G_HAVE_ISO_VARARGS


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