diff --git a/App.js b/App.js index 7671770..04a3a0e 100644 --- a/App.js +++ b/App.js @@ -9,6 +9,13 @@ const { DeviceMonitor } = Me.imports.net.DeviceMonitor; const { Logger } = Me.imports.utils.Logger; class AppClass { + static instance() { + return this._instance || (this._instance = new this()); + } + + static deleteInstance() { + this._instance = undefined; + } constructor() { const logger = new Logger(); diff --git a/extension.js b/extension.js index 3886998..df4889f 100644 --- a/extension.js +++ b/extension.js @@ -5,26 +5,20 @@ const Me = ExtensionUtils.getCurrentExtension(); const { Logger } = Me.imports.utils.Logger; const { App } = Me.imports.App; const { initBrodcasters, deinitBrodcasters } = Me.imports.utils.Broadcasters; -const Prefs = Me.imports.prefs; -let logger = undefined; -let app = undefined; function init() { - logger = new Logger; - logger.info(`initializing 1 ${Me.metadata.name}`); + Logger.info(`initializing ${Me.metadata.name}`); } function enable() { - logger.info(`enabling ${Me.metadata.name}`); + Logger.info(`enabling ${Me.metadata.name}`); initBrodcasters(); - app = new App(); - app.start(); + App.instance().start(); } function disable() { - logger.info(`disabling ${Me.metadata.name}`); - app.stop(); + Logger.info(`disabling ${Me.metadata.name}`); + App.instance().stop(); deinitBrodcasters(); - app = undefined; - logger = undefined; + App.deleteInstance(); } diff --git a/metadata.json b/metadata.json index 1c034be..d17601c 100644 --- a/metadata.json +++ b/metadata.json @@ -20,5 +20,5 @@ "41" ], "url" : "https://github.com/noroadsleft000/gnome-network-stats", - "version": 12 + "version": 13 } diff --git a/prefs.js b/prefs.js index 22b503b..b4763ae 100644 --- a/prefs.js +++ b/prefs.js @@ -78,7 +78,6 @@ const SettingRowOrder = Object.freeze({ class PrefsApp { constructor() { - this._logger = new Logger; this._rows = {}; this.main = new Gtk.Grid({ margin_top: 10, @@ -126,7 +125,7 @@ class PrefsApp { _hideRow(row) { const label = this.main.get_child_at(0, row); const input = this.main.get_child_at(1, row); - //this._logger.log(`${row}. label: ${label} input: ${input}`); + //Logger.log(`${row}. label: ${label} input: ${input}`); if (label) { this.main.remove(label); } @@ -137,7 +136,7 @@ class PrefsApp { _showRow(row) { const { label, input } = this._rows[row]; - //this._logger.log(`${row}. label: ${label} input: ${input}`); + //Logger.log(`${row}. label: ${label} input: ${input}`); if (!label.parent && !input.parent) { this.main.attach(label, 0, row, 1, 1); this.main.attach(input, 1, row, 1, 1); @@ -379,7 +378,7 @@ class PrefsApp { updateControls() { const resetSchedule = this.schema.get_string(SettingKeys.RESET_SCHEDULE); - //this._logger.log(`resetSchedule: ${resetSchedule}`); + //Logger.log(`resetSchedule: ${resetSchedule}`); switch(resetSchedule) { default: case ResetSchedule.DAILY: @@ -420,8 +419,7 @@ class PrefsApp { /** Initialize language/locale */ function init() { - const logger = new Logger; - logger.debug("init"); + Logger.debug("init"); const localeDir = Me.dir.get_child("locale"); if (localeDir.query_exists(null)) { Gettext.bindtextdomain("network-stats", localeDir.get_path()); @@ -430,8 +428,7 @@ function init() { /** Build settings view */ function buildPrefsWidget() { - const logger = new Logger; - logger.debug("buildPrefsWidget"); + Logger.debug("buildPrefsWidget"); const widget = new PrefsApp(); if (isGtk3()) { widget.main.show_all(); diff --git a/utils/Logger.js b/utils/Logger.js index f91b4d0..90e8e93 100644 --- a/utils/Logger.js +++ b/utils/Logger.js @@ -9,8 +9,28 @@ const Me = ExtensionUtils.getCurrentExtension(); */ class LoggerClass { - constructor() { - this.test = "Hello"; + static instance() { + return this._instance || (this._instance = new this()); + } + + static log(...args) { + this.instance()._printLog("LOG", args); + } + + static debug(...args) { + this.instance()._printLog("DEBUG", args); + } + + static info(...args) { + this.instance()._printLog("INFO", args); + } + + static error(...args) { + this.instance()._printLog("ERROR", args); + } + + static critical(...args) { + this.instance()._printLog("**CRITICAL", args); } _callerInfo(level=3) {