From 43fa70c29b14ea0cc4cf03bf4d7101a425b6bf7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20M=C3=BCllner?= Date: Thu, 26 Jul 2018 13:37:30 +0200 Subject: [PATCH 1/4] main: Handle non-string arguments to log() GLib.Variant is very strict about types, so passing anything other than a string to one of our log functions currently throws an error. While this doesn't affect "real" logging, using log(someVar) is useful in debugging, so allow that. https://gitlab.gnome.org/GNOME/polari/merge_requests/55 --- src/main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.js b/src/main.js index cbe105f0..e047fcef 100755 --- a/src/main.js +++ b/src/main.js @@ -28,7 +28,7 @@ function _makeLogFunction(level) { 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), + '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), -- GitLab From a2ffc7497c48962763f4febc95a1b1053deb2e10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20M=C3=BCllner?= Date: Thu, 26 Jul 2018 14:51:08 +0200 Subject: [PATCH 2/4] main: Map resource location to source file For the code information in the logs, the location in the sources is generally more interesting than the installed one. With all our code in a resource, the mapping is trivial and straight-forward (and more readable), so use the source location for the journal. https://gitlab.gnome.org/GNOME/polari/merge_requests/55 --- src/main.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main.js b/src/main.js index e047fcef..9ac746c0 100755 --- a/src/main.js +++ b/src/main.js @@ -26,6 +26,9 @@ function _makeLogFunction(level) { let stack = (new Error()).stack; let caller = stack.split('\n')[1]; + // Map from resource- to source location + caller = caller.replace('resource:///org/gnome/Polari/js', 'src'); + 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}`), -- GitLab From 972fd2e3c9c06ad9d1f7e5a821457ab74362190e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20M=C3=BCllner?= Date: Thu, 26 Jul 2018 14:52:55 +0200 Subject: [PATCH 3/4] main: Use log_structured() convenience wrapper GJS has included an override for the non-introspectable g_log_structured() for a while now, which takes care of wrapping the field values into GVariants before calling g_log_variant(). https://gitlab.gnome.org/GNOME/polari/merge_requests/55 --- src/main.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main.js b/src/main.js index 9ac746c0..0d3d7c29 100755 --- a/src/main.js +++ b/src/main.js @@ -30,13 +30,13 @@ function _makeLogFunction(level) { caller = caller.replace('resource:///org/gnome/Polari/js', 'src'); 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) - })); + GLib.log_structured(LOG_DOMAIN, level, { + 'MESSAGE': `${message}`, + 'SYSLOG_IDENTIFIER': 'org.gnome.Polari', + 'CODE_FILE': file, + 'CODE_FUNC': func, + 'CODE_LINE': line + }); }; } -- GitLab From e528127c02defe6e762a1b29491af781eb14461f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20M=C3=BCllner?= Date: Thu, 26 Jul 2018 14:41:29 +0200 Subject: [PATCH 4/4] main: Replace tedious regex with simpler string methods We include fields about the code location where a message was logged by throwing an error and parsing the resulting stack trace. The regex we are currently using for that has at least two issues: - it grabs the column- instead of the line number - it doesn't handle some weird characters SpiderMonkey appends to some method names Instead of increasing the complexity of the regex, use two simpler split() operations to first retrieve the line information, and then separate function and file. https://gitlab.gnome.org/GNOME/polari/merge_requests/55 --- src/main.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main.js b/src/main.js index 0d3d7c29..9e75e707 100755 --- a/src/main.js +++ b/src/main.js @@ -29,7 +29,8 @@ function _makeLogFunction(level) { // Map from resource- to source location caller = caller.replace('resource:///org/gnome/Polari/js', 'src'); - let [, func, file, line] = new RegExp('(.+)?@(.+):(\\d+)').exec(caller); + let [code, line] = caller.split(':'); + let [func, file] = code.split(/\W*@/); GLib.log_structured(LOG_DOMAIN, level, { 'MESSAGE': `${message}`, 'SYSLOG_IDENTIFIER': 'org.gnome.Polari', -- GitLab