[polari] main: Split out logging setup
- From: Florian Müllner <fmuellner src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [polari] main: Split out logging setup
- Date: Sat, 13 Feb 2021 21:58:24 +0000 (UTC)
commit e32b88b0b5a3550505fd08297da1e46de717ca34
Author: Florian Müllner <fmuellner gnome org>
Date: Sat Feb 13 11:07:14 2021 +0100
main: Split out logging setup
Now that the file no longer contains a main() function, but
is itself being executed as a script, it makes sense to keep
it as simple and standard as possible.
https://gitlab.gnome.org/GNOME/polari/-/merge_requests/177
src/logging.js | 35 +++++++++++++++++++++++++++++++++++
src/main.js | 36 +++---------------------------------
src/meson.build | 1 +
3 files changed, 39 insertions(+), 33 deletions(-)
---
diff --git a/src/logging.js b/src/logging.js
new file mode 100644
index 00000000..9b241f86
--- /dev/null
+++ b/src/logging.js
@@ -0,0 +1,35 @@
+import GLib from 'gi://GLib';
+
+const LOG_DOMAIN = 'Polari';
+
+function log(level, message) {
+ const { stack } = new Error();
+ let [, caller] = stack.split('\n');
+
+ // Map from resource- to source location
+ caller = caller.replace('resource:///org/gnome/Polari/js', 'src');
+
+ const [code, line] = caller.split(':');
+ const [func, file] = code.split(/\W*@/);
+ GLib.log_structured(LOG_DOMAIN, level, {
+ 'MESSAGE': `${message}`,
+ 'SYSLOG_IDENTIFIER': 'org.gnome.Polari',
+ 'CODE_FILE': file,
+ 'CODE_FUNC': func,
+ 'CODE_LINE': line,
+ });
+}
+
+export function init() {
+ const { LogLevelFlags } = GLib;
+ globalThis.log = msg => log(LogLevelFlags.LEVEL_MESSAGE, msg);
+ globalThis.debug = msg => log(LogLevelFlags.LEVEL_DEBUG, msg);
+ globalThis.info = msg => log(LogLevelFlags.LEVEL_INFO, msg);
+ globalThis.warning = msg => log(LogLevelFlags.LEVEL_WARNING, msg);
+ globalThis.critical = msg => log(LogLevelFlags.LEVEL_CRITICAL, msg);
+ globalThis.error = msg => log(LogLevelFlags.LEVEL_ERROR, msg);
+
+ // Log all messages when connected to the journal
+ if (GLib.log_writer_is_journald(2))
+ GLib.setenv('G_MESSAGES_DEBUG', LOG_DOMAIN, false);
+}
diff --git a/src/main.js b/src/main.js
index 87db61d1..7978597e 100755
--- a/src/main.js
+++ b/src/main.js
@@ -1,6 +1,7 @@
import GLib from 'gi://GLib';
import * as Config from './config.js';
+import * as Log from './logging.js';
import { ngettext } from 'gettext';
import { programInvocationName } from 'system';
@@ -30,40 +31,9 @@ pkg.requireSymbol('GLib', '2.0', 'log_variant');
pkg.requireSymbol('Gspell', '1', 'Entry');
pkg.requireSymbol('Gtk', '3.0', 'ScrolledWindow.propagate_natural_width');
-import { Application } from './application.js';
-
-var LOG_DOMAIN = 'Polari';
-
-function _makeLogFunction(level) {
- return message => {
- let { stack } = new Error();
- let [, caller] = stack.split('\n');
-
- // Map from resource- to source location
- caller = caller.replace('resource:///org/gnome/Polari/js', 'src');
+Log.init();
- let [code, line] = caller.split(':');
- let [func, file] = code.split(/\W*@/);
- GLib.log_structured(LOG_DOMAIN, level, {
- 'MESSAGE': `${message}`,
- 'SYSLOG_IDENTIFIER': 'org.gnome.Polari',
- 'CODE_FILE': file,
- 'CODE_FUNC': func,
- 'CODE_LINE': line,
- });
- };
-}
-
-globalThis.log = _makeLogFunction(GLib.LogLevelFlags.LEVEL_MESSAGE);
-globalThis.debug = _makeLogFunction(GLib.LogLevelFlags.LEVEL_DEBUG);
-globalThis.info = _makeLogFunction(GLib.LogLevelFlags.LEVEL_INFO);
-globalThis.warning = _makeLogFunction(GLib.LogLevelFlags.LEVEL_WARNING);
-globalThis.critical = _makeLogFunction(GLib.LogLevelFlags.LEVEL_CRITICAL);
-globalThis.error = _makeLogFunction(GLib.LogLevelFlags.LEVEL_ERROR);
-
-// Log all messages when connected to the journal
-if (GLib.log_writer_is_journald(2))
- GLib.setenv('G_MESSAGES_DEBUG', LOG_DOMAIN, false);
+import { Application } from './application.js';
let application = new Application();
if (GLib.getenv('POLARI_PERSIST'))
diff --git a/src/meson.build b/src/meson.build
index efc01f96..efd7108f 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -15,6 +15,7 @@ js_sources = [
'initialSetup.js',
'ircParser.js',
'joinDialog.js',
+ 'logging.js',
'main.js',
'mainWindow.js',
'networksManager.js',
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]