Skip to content

Commit e8e615a

Browse files
authored
Merge pull request #2444 from UltimateHackingKeyboard/chore-usb-ops
chore: usbOps logging option
2 parents 31ef659 + bdd1c77 commit e8e615a

File tree

10 files changed

+82
-48
lines changed

10 files changed

+82
-48
lines changed

packages/uhk-agent/src/services/logger.service.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,19 @@ export class ElectronLogService extends LogService {
5252
} else {
5353
this.log('%c' + args.join(' '), 'color:red');
5454
}
55-
} else if (LogRegExps.transferRegExp.test(args[0])) {
56-
this.log('%c' + args.join(' '), 'color:orange');
5755
} else {
5856
this.log(...args);
5957
}
6058
}
6159

60+
usbOps(...args) {
61+
if (!this._options.usbOps) {
62+
return;
63+
}
64+
65+
this.log('%c' + args.join(' '), 'color:orange');
66+
}
67+
6268
protected log(...args: any[]): void {
6369
log.log(...args);
6470
}

packages/uhk-agent/src/util/command-line.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ const sections: commandLineUsage.Section[] = [
5757
{
5858
name: 'log',
5959
description: 'Set logging categories. --log=misc,usb. Default is "misc"',
60-
typeLabel: 'config | misc | usb | all'
60+
typeLabel: 'config | misc | usb | usbOps | all'
6161
},
6262
{
6363
name: 'no-report-id',

packages/uhk-common/src/log/get-log-options.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export function getLogOptions(options: CommandLineArgs): LogOptions {
1515
logOptions.config = true;
1616
logOptions.misc = true;
1717
logOptions.usb = true;
18+
logOptions.usbOps = true;
1819
break;
1920

2021
case 'config':
@@ -27,6 +28,11 @@ export function getLogOptions(options: CommandLineArgs): LogOptions {
2728

2829
case 'usb':
2930
logOptions.usb = true;
31+
logOptions.usbOps = true;
32+
break;
33+
34+
case 'usbOps':
35+
logOptions.usbOps = true;
3036
break;
3137

3238
default:
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
export namespace LogRegExps {
2-
export const transferRegExp = /USB\[T]:/;
32
export const writeRegExp = /USB\[W]:/;
43
export const readRegExp = /USB\[R]:/;
54
}

packages/uhk-common/src/log/logger.service.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ export class LogService {
4444
this.log(...args);
4545
}
4646

47+
usbOps(...args: any[]): void {
48+
if (!this._options.usbOps) {
49+
return;
50+
}
51+
52+
this.log(...args);
53+
}
54+
4755
protected log(...args: any[]): void {
4856
console.log(...args);
4957
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
export interface LogOptions {
2+
/**
3+
* Log every USB operations USB[T], USB[W], USB[R]
4+
*/
25
usb?: boolean;
6+
/**
7+
* Log only USB[T] log entries
8+
*/
9+
usbOps?: boolean;
310
config?: boolean;
411
misc?: boolean;
512
}

packages/uhk-usb/src/uhk-hid-device.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -148,23 +148,23 @@ export class UhkHidDevice {
148148
public async deleteBond(address: number[]): Promise<void> {
149149
await this.assertDeviceSupportWirelessUSBCommands();
150150

151-
this.logService.usb('[UhkHidDevice] USB[T]: Delete all bonds');
151+
this.logService.usbOps('[UhkHidDevice] USB[T]: Delete all bonds');
152152
const buffer = Buffer.from([UsbCommand.UnpairAll, ...address]);
153153
await this.write(buffer);
154154
}
155155

156156
public async deleteAllBonds(): Promise<void> {
157157
await this.assertDeviceSupportWirelessUSBCommands();
158158

159-
this.logService.usb('[UhkHidDevice] USB[T]: Delete all bonds');
159+
this.logService.usbOps('[UhkHidDevice] USB[T]: Delete all bonds');
160160
const buffer = Buffer.from([UsbCommand.UnpairAll, 0, 0, 0, 0, 0, 0]);
161161
await this.write(buffer);
162162
}
163163

164164
public async getBleAddress(): Promise<number[]> {
165165
await this.assertDeviceSupportWirelessUSBCommands();
166166

167-
this.logService.usb('[UhkHidDevice] USB[T]: get BLE address');
167+
this.logService.usbOps('[UhkHidDevice] USB[T]: get BLE address');
168168
const buffer = Buffer.from([UsbCommand.GetProperty, DevicePropertyIds.BleAddress]);
169169
const responseBuffer = await this.write(buffer);
170170

@@ -180,7 +180,7 @@ export class UhkHidDevice {
180180
public async getPairedRightPairBleAddress(): Promise<number[]> {
181181
await this.assertDeviceSupportWirelessUSBCommands();
182182

183-
this.logService.usb('[UhkHidDevice] USB[T]: get paired right pair BLE address');
183+
this.logService.usbOps('[UhkHidDevice] USB[T]: get paired right pair BLE address');
184184
const buffer = Buffer.from([UsbCommand.GetProperty, DevicePropertyIds.PairedRightPeerBleAddress]);
185185
const responseBuffer = await this.write(buffer);
186186

@@ -196,7 +196,7 @@ export class UhkHidDevice {
196196
public async getPairingInfo(): Promise<PairingInfo> {
197197
await this.assertDeviceSupportWirelessUSBCommands();
198198

199-
this.logService.usb('[UhkHidDevice] USB[T]: read pairing info');
199+
this.logService.usbOps('[UhkHidDevice] USB[T]: read pairing info');
200200
const buffer = Buffer.from([UsbCommand.GetPairingData]);
201201
const responseBuffer = await this.write(buffer);
202202
const numbers = convertBufferToIntArray(responseBuffer);
@@ -216,7 +216,7 @@ export class UhkHidDevice {
216216
public async getPairingStatus(): Promise<PairingStatuses> {
217217
await this.assertDeviceSupportWirelessUSBCommands();
218218

219-
this.logService.usb('[UhkHidDevice] USB[T]: read pairing info');
219+
this.logService.usbOps('[UhkHidDevice] USB[T]: read pairing info');
220220
const buffer = Buffer.from([UsbCommand.GetProperty, DevicePropertyIds.PairingStatus]);
221221
const responseBuffer = await this.write(buffer);
222222

@@ -237,7 +237,7 @@ export class UhkHidDevice {
237237
public async isPairedWith(address: number[]): Promise<boolean> {
238238
await this.assertDeviceSupportWirelessUSBCommands();
239239

240-
this.logService.usb('[UhkHidDevice] USB[T]: is paired with');
240+
this.logService.usbOps('[UhkHidDevice] USB[T]: is paired with');
241241
const buffer = Buffer.from([
242242
UsbCommand.IsPaired,
243243
...address,
@@ -252,23 +252,23 @@ export class UhkHidDevice {
252252
public async pairCentral(): Promise<void> {
253253
await this.assertDeviceSupportWirelessUSBCommands();
254254

255-
this.logService.usb('[UhkHidDevice] USB[T]: pair central');
255+
this.logService.usbOps('[UhkHidDevice] USB[T]: pair central');
256256
const buffer = Buffer.from([UsbCommand.PairCentral]);
257257
await this.write(buffer);
258258
}
259259

260260
public async pairPeripheral(): Promise<void> {
261261
await this.assertDeviceSupportWirelessUSBCommands();
262262

263-
this.logService.usb('[UhkHidDevice] USB[T]: pair peripheral');
263+
this.logService.usbOps('[UhkHidDevice] USB[T]: pair peripheral');
264264
const buffer = Buffer.from([UsbCommand.PairPeripheral]);
265265
await this.write(buffer);
266266
}
267267

268268
public async setPairingInfo(pairId: PairIds, info: PairingInfo): Promise<void> {
269269
await this.assertDeviceSupportWirelessUSBCommands();
270270

271-
this.logService.usb('[UhkHidDevice] USB[T]: set pairing info');
271+
this.logService.usbOps('[UhkHidDevice] USB[T]: set pairing info');
272272
const buffer = Buffer.from([
273273
UsbCommand.SetPairingData,
274274
pairId,
@@ -283,7 +283,7 @@ export class UhkHidDevice {
283283
public async switchToPairingMode(): Promise<void> {
284284
await this.assertDeviceSupportWirelessUSBCommands();
285285

286-
this.logService.usb('[UhkHidDevice] USB[T]: switch to pairing mode');
286+
this.logService.usbOps('[UhkHidDevice] USB[T]: switch to pairing mode');
287287
const buffer = Buffer.from([UsbCommand.EnterPairingMode]);
288288
await this.write(buffer);
289289
}
@@ -558,7 +558,7 @@ export class UhkHidDevice {
558558
(timeout & 0xff << 24) >> 24
559559
]);
560560
const data = this.getTransferData(message, reportId);
561-
this.logService.usb(`[UhkHidDevice] USB[T]: Enumerated device, mode: ${reenumMode}`);
561+
this.logService.usbOps(`[UhkHidDevice] USB[T]: Enumerated device, mode: ${reenumMode}`);
562562
this.logService.usb('[UhkHidDevice] USB[W]:', bufferToString(data).substr(3));
563563
try {
564564
keyboardDevice.write(data);
@@ -576,7 +576,7 @@ export class UhkHidDevice {
576576
}
577577
jumped = true;
578578
} else {
579-
this.logService.usb('[UhkHidDevice] USB[T]: Enumerated device is not ready yet');
579+
this.logService.usbOps('[UhkHidDevice] USB[T]: Enumerated device is not ready yet');
580580
}
581581
}
582582
else {
@@ -592,7 +592,7 @@ export class UhkHidDevice {
592592

593593
async sendKbootCommandToModule(module: ModuleSlotToI2cAddress, command: KbootCommands, maxTry = 1): Promise<any> {
594594
let transfer;
595-
this.logService.usb(`[UhkHidDevice] USB[T]: Send KbootCommand ${mapI2cAddressToModuleName(module)} ${KbootCommands[command].toString()}`);
595+
this.logService.usbOps(`[UhkHidDevice] USB[T]: Send KbootCommand ${mapI2cAddressToModuleName(module)} ${KbootCommands[command].toString()}`);
596596
if (command === KbootCommands.idle) {
597597
transfer = Buffer.from([UsbCommand.SendKbootCommandToModule, command]);
598598
} else {
@@ -678,7 +678,7 @@ export class UhkHidDevice {
678678
return this._protocolVersions;
679679
}
680680

681-
this.logService.usb('[UhkHidDevice] USB[T]: Read device protocol version information');
681+
this.logService.usbOps('[UhkHidDevice] USB[T]: Read device protocol version information');
682682
const command = Buffer.from([UsbCommand.GetProperty, DevicePropertyIds.ProtocolVersions]);
683683
const buffer = await this.write(command);
684684
const uhkBuffer = UhkBuffer.fromArray(convertBufferToIntArray(buffer));

0 commit comments

Comments
 (0)