[meld/meld-3-16] bin/meld: Redirect glib's new structured logging (bgo#779008)



commit 7d7e189a6fde29067d133de2975b5c821d633c36
Author: Kai Willadsen <kai willadsen gmail com>
Date:   Fri Feb 24 06:44:23 2017 +1000

    bin/meld: Redirect glib's new structured logging (bgo#779008)
    
    We already tried to redirect logging, but in 2.50 glib added support
    for structured logging that inexplicably bypassed all of the API
    that was already being used by applications to do logging redirection.

 bin/meld |   22 +++++++++++++++++++++-
 1 files changed, 21 insertions(+), 1 deletions(-)
---
diff --git a/bin/meld b/bin/meld
index 1e4c75d..86a3f51 100755
--- a/bin/meld
+++ b/bin/meld
@@ -246,6 +246,9 @@ def setup_logging():
 def setup_glib_logging():
     from gi.repository import GLib
     levels = {
+        GLib.LogLevelFlags.LEVEL_DEBUG: logging.DEBUG,
+        GLib.LogLevelFlags.LEVEL_INFO: logging.INFO,
+        GLib.LogLevelFlags.LEVEL_MESSAGE: logging.INFO,
         GLib.LogLevelFlags.LEVEL_WARNING: logging.WARNING,
         GLib.LogLevelFlags.LEVEL_ERROR: logging.ERROR,
         GLib.LogLevelFlags.LEVEL_CRITICAL: logging.CRITICAL,
@@ -259,8 +262,10 @@ def setup_glib_logging():
     log_domain = "Gtk"
     log = logging.getLogger(log_domain)
 
+    # This logging handler is for "old" glib logging using a simple
+    # syslog-style API.
     def log_adapter(domain, level, message, user_data):
-        log.log(levels[level], message)
+        log.log(levels.get(level, logging.WARNING), message)
 
     try:
         GLib.log_set_handler(log_domain, level_flag, log_adapter, None)
@@ -268,6 +273,21 @@ def setup_glib_logging():
         # Only present in glib 2.46+
         pass
 
+    # This logging handler is for new glib logging using a structured
+    # API. Unfortunately, it was added in such a way that the old
+    # redirection API became a no-op, so we need to hack both of these
+    # handlers to get it to work.
+    def structured_log_adapter(level, fields, field_count, user_data):
+        message = GLib.log_writer_format_fields(level, fields, True)
+        log.log(levels.get(level, logging.WARNING), message)
+        return GLib.LogWriterOutput.HANDLED
+
+    try:
+        GLib.log_set_writer_func(structured_log_adapter, None)
+    except AttributeError:
+        # Only present in glib 2.50+
+        pass
+
 
 def environment_hacks():
     # We manage cwd ourselves for git operations, and GIT_DIR in particular


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