Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hidden 'debug-detail' preference, to allow disabling packet dumps #1357

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@

"global": false,
"debug": false,
"debugPacket": false,
"_": false,
"_C": false,
"_N": false,
Expand Down
6 changes: 6 additions & 0 deletions data/org.gnome.Shell.Extensions.GSConnect.gschema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
<key name="debug" type="b">
<default>false</default>
</key>
<key name="debug-detail" type="b">
<default>true</default>
<description>
Include dumps of packet contents in debug output.
</description>
</key>
<key name="discoverable" type="b">
<default>true</default>
</key>
Expand Down
40 changes: 37 additions & 3 deletions src/service/__init__.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,20 +126,54 @@ 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, 'debug-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.debugPacket = __getPacketFunc(settings, key);
});

/**
* A simple (for now) pre-comparison sanitizer for phone numbers
Expand Down
4 changes: 2 additions & 2 deletions src/service/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ var Device = GObject.registerClass({
let packet = null;

while ((packet = await this.channel.readPacket())) {
debug(packet, this.name);
debugPacket(packet, this.name);
this.handlePacket(packet);
}
} catch (e) {
Expand Down Expand Up @@ -446,7 +446,7 @@ var Device = GObject.registerClass({

while ((next = this._outputQueue.shift())) {
await this.channel.sendPacket(next);
debug(next, this.name);
debugPacket(next, this.name);
}

this._outputLock = false;
Expand Down