Skip to content

Commit

Permalink
Define debugPacket() for dumps
Browse files Browse the repository at this point in the history
  • Loading branch information
ferdnyc committed May 22, 2022
1 parent b78937b commit e23903b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 17 deletions.
44 changes: 35 additions & 9 deletions src/service/__init__.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 2 additions & 8 deletions src/service/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit e23903b

Please sign in to comment.