diff --git a/src/service/__init__.js b/src/service/__init__.js index 12f1e8e72..8ffb29bf9 100644 --- a/src/service/__init__.js +++ b/src/service/__init__.js @@ -126,29 +126,55 @@ const _debugFunc = function (error, prefix = null) { }); }; +/** + * A special debug function for logging packet dumps with + * their contents omitted. + * @param {object} packet - A GSConnect packet payload + * @param {string} [prefix] - An optional prefix for the dump + */ +// eslint-disable-next-line func-style +const _packetDebugFunc = function (packet, prefix = null) { + globalThis.debug(packet.type, prefix); +}; + +/** + * A helper to set the function used for globalThis.debugPacket() + * @param {object} settings - a Gio.Settings instance + * @param {string} key - the settings key used to select the correct + * debug function + */ +// eslint-disable-next-line func-style +const __getPacketFunc = function (settings, key) { + const detailEnabled = settings.get_boolean(key); + return detailEnabled ? globalThis.debug : _packetDebugFunc; +}; + // Swap the function out for a no-op anonymous function for speed const settings = new Gio.Settings({ settings_schema: Config.GSCHEMA.lookup(Config.APP_ID, true), }); settings.connect('changed::debug', (settings, key) => { - globalThis.debug = settings.get_boolean(key) ? _debugFunc : () => {}; + const enabled = settings.get_boolean(key); + globalThis.debug = enabled ? _debugFunc : () => {}; + if (!enabled) + globalThis.debugPacket = () => {}; + else + globalThis.debugPacket = __getPacketFunc(settings, 'packet-detail'); }); -if (settings.get_boolean('debug')) +if (settings.get_boolean('debug')) { globalThis.debug = _debugFunc; -else + globalThis.debugPacket = __getPacketFunc(settings, 'debug-detail'); +} else { globalThis.debug = () => {}; + globalThis.debugPacket = () => {}; +} settings.connect('changed::debug-detail', (settings, key) => { - globalThis.debugDetail = settings.get_boolean(key); + globalThis.debugPacket = __getPacketFunc(settings, key); }); -if (settings.get_boolean('debug-detail')) - globalThis.debugDetail = true; -else - globalThis.debugDetail = false; - /** * A simple (for now) pre-comparison sanitizer for phone numbers * See: https://github.com/KDE/kdeconnect-kde/blob/master/smsapp/conversationlistmodel.cpp#L200-L210 diff --git a/src/service/device.js b/src/service/device.js index 4014ccc60..390578d51 100644 --- a/src/service/device.js +++ b/src/service/device.js @@ -338,10 +338,7 @@ var Device = GObject.registerClass({ let packet = null; while ((packet = await this.channel.readPacket())) { - if (globalThis.debugDetail) - debug(packet, this.name); - else - debug(packet.type, this.name); + debugPacket(packet, this.name); this.handlePacket(packet); } } catch (e) { @@ -449,10 +446,7 @@ var Device = GObject.registerClass({ while ((next = this._outputQueue.shift())) { await this.channel.sendPacket(next); - if (globalThis.debugDetail) - debug(next, this.name); - else - debug(next.type, this.name); + debugPacket(next, this.name); } this._outputLock = false;