Skip to content

Commit 4110c78

Browse files
committed
Define debugPacket() for dumps
1 parent ed0a720 commit 4110c78

File tree

2 files changed

+26
-14
lines changed

2 files changed

+26
-14
lines changed

src/service/__init__.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,17 @@ const _debugFunc = function (error, prefix = null) {
126126
});
127127
};
128128

129+
/**
130+
* A special debug function for logging packet dumps with
131+
* their contents omitted.
132+
* @param {object} packet - A GSConnect packet payload
133+
* @param {string} [prefix] - An optional prefix for the dump
134+
*/
135+
// eslint-disable-next-line func-style
136+
const _packetDebugFunc = function (packet, prefix = null) {
137+
_debugFunc(packet.type, prefix);
138+
};
139+
129140
// Swap the function out for a no-op anonymous function for speed
130141
const settings = new Gio.Settings({
131142
settings_schema: Config.GSCHEMA.lookup(Config.APP_ID, true),
@@ -138,16 +149,23 @@ settings.connect('changed::debug', (settings, key) => {
138149
if (settings.get_boolean('debug'))
139150
globalThis.debug = _debugFunc;
140151
else
141-
globalThis.debug = () => {};
152+
globalThis.debug = () => { };
153+
154+
// eslint-disable-next-line func-style
155+
const _getPacketFunc = function (settings, debugKey, detailKey) {
156+
const debugEnabled = settings.get_boolean(debugKey);
157+
const debugDetail = settings.get_boolean(detailKey);
158+
if (debugEnabled && !debugDetail)
159+
return _packetDebugFunc;
160+
else
161+
return globalThis.debug;
162+
};
142163

143164
settings.connect('changed::debug-detail', (settings, key) => {
144-
globalThis.debugDetail = settings.get_boolean(key);
165+
globalThis.debugPacket = _getPacketFunc(settings, 'debug', key);
145166
});
146167

147-
if (settings.get_boolean('debug-detail'))
148-
globalThis.debugDetail = true;
149-
else
150-
globalThis.debugDetail = false;
168+
globalThis.debugPacket = _getPacketFunc(settings, 'debug', 'debug-detail');
151169

152170
/**
153171
* A simple (for now) pre-comparison sanitizer for phone numbers

src/service/device.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -338,10 +338,7 @@ var Device = GObject.registerClass({
338338
let packet = null;
339339

340340
while ((packet = await this.channel.readPacket())) {
341-
if (globalThis.debugDetail)
342-
debug(packet, this.name);
343-
else
344-
debug(packet.type, this.name);
341+
debugPacket(packet, this.name);
345342
this.handlePacket(packet);
346343
}
347344
} catch (e) {
@@ -449,10 +446,7 @@ var Device = GObject.registerClass({
449446

450447
while ((next = this._outputQueue.shift())) {
451448
await this.channel.sendPacket(next);
452-
if (globalThis.debugDetail)
453-
debug(next, this.name);
454-
else
455-
debug(next.type, this.name);
449+
debugPacket(next, this.name);
456450
}
457451

458452
this._outputLock = false;

0 commit comments

Comments
 (0)