[polari/wip/fmuellner/logging: 2/4] main: Use structured log for logging



commit b3abd675ec61f648490e4edabf5b6da4e015940d
Author: Florian Müllner <fmuellner gnome org>
Date:   Fri Sep 9 18:28:36 2016 +0200

    main: Use structured log for logging
    
    GLib gained the ability to log structured data rather than plain
    strings this cycle, and with the addition of g_log_variant(), we
    are now able to use it from introspection to add additional useful
    information to our log messages when connected to the journal. And
    when logging to the console, we are at least using the standard
    GLib logging, which means no custom env variables anymore, support
    for fatal-warnings etc.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=771218

 configure.ac |    2 +-
 src/main.js  |   25 ++++++++++++++++++++++++-
 src/utils.js |   16 ----------------
 3 files changed, 25 insertions(+), 18 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index f647238..45a89ee 100644
--- a/configure.ac
+++ b/configure.ac
@@ -44,7 +44,7 @@ AX_PROG_GJS
 AX_CHECK_GIRS_GJS([Gdk], [3.0])
 AX_CHECK_GIRS_GJS([GdkPixbuf], [2.0])
 AX_CHECK_GIR_SYMBOLS_GJS([Gio], [2.0], [Application.send_notification])
-AX_CHECK_GIRS_GJS([GLib], [2.0])
+AX_CHECK_GIR_SYMBOLS_GJS([GLib], [2.0], [log_variant])
 AX_CHECK_GIRS_GJS([GObject], [2.0])
 AX_CHECK_GIR_SYMBOLS_GJS([Gtk], [3.0], [ScrolledWindow.set_propagate_natural_width])
 AX_CHECK_GIRS_GJS([Pango], [1.0])
diff --git a/src/main.js b/src/main.js
index 8ce3f07..4075f64 100755
--- a/src/main.js
+++ b/src/main.js
@@ -10,7 +10,30 @@ pkg.require({ 'Gio': '2.0',
 const Application = imports.application;
 const GLib = imports.gi.GLib;
 
-window.debug = imports.utils.debug;
+const LOG_DOMAIN = 'Polari';
+
+function _makeLogFunction(level) {
+    return message => {
+        let stack = (new Error()).stack;
+        let caller = stack.split('\n')[1];
+
+        let [, func, file, line] = new RegExp('(.+)?@(.+):(\\d+)').exec(caller);
+        GLib.log_variant(LOG_DOMAIN, level, new GLib.Variant('a{sv}', {
+            'MESSAGE': new GLib.Variant('s', message),
+            'SYSLOG_IDENTIFIER': new GLib.Variant('s', 'org.gnome.Polari'),
+            'CODE_FILE': new GLib.Variant('s', file),
+            'CODE_FUNC': new GLib.Variant('s', func),
+            'CODE_LINE': new GLib.Variant('s', line)
+        }));
+    };
+}
+
+window.log      = _makeLogFunction(GLib.LogLevelFlags.LEVEL_MESSAGE);
+window.debug    = _makeLogFunction(GLib.LogLevelFlags.LEVEL_DEBUG);
+window.info     = _makeLogFunction(GLib.LogLevelFlags.LEVEL_INFO);
+window.warning  = _makeLogFunction(GLib.LogLevelFlags.LEVEL_WARNING);
+window.critical = _makeLogFunction(GLib.LogLevelFlags.LEVEL_CRITICAL);
+window.error    = _makeLogFunction(GLib.LogLevelFlags.LEVEL_ERROR);
 
 function main(args) {
     let application = new Application.Application();
diff --git a/src/utils.js b/src/utils.js
index fc3c1bd..66b11f5 100644
--- a/src/utils.js
+++ b/src/utils.js
@@ -79,22 +79,6 @@ const _urlRegexp = new RegExp(
 
 const _channelRegexp = new RegExp('(^| )#([\\w\\+\\.-]+)','g');
 
-let debugInit = false;
-let debugEnabled = false;
-
-function debug(str) {
-    if (!debugInit) {
-        let env = GLib.getenv('POLARI_DEBUG');
-        if (env)
-            debugEnabled = true;
-
-        debugInit = true;
-    }
-
-    if (debugEnabled)
-        log('DEBUG: ' + str);
-}
-
 function getURISchemes() {
     let apps = Gio.AppInfo.get_all();
     let prefix = 'x-scheme-handler/';


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