diff --git a/packages/uhk-agent/src/services/logger.service.ts b/packages/uhk-agent/src/services/logger.service.ts index 3df775e5cf9..ed7148fdcc0 100644 --- a/packages/uhk-agent/src/services/logger.service.ts +++ b/packages/uhk-agent/src/services/logger.service.ts @@ -52,13 +52,19 @@ export class ElectronLogService extends LogService { } else { this.log('%c' + args.join(' '), 'color:red'); } - } else if (LogRegExps.transferRegExp.test(args[0])) { - this.log('%c' + args.join(' '), 'color:orange'); } else { this.log(...args); } } + usbOps(...args) { + if (!this._options.usbOps) { + return; + } + + this.log('%c' + args.join(' '), 'color:orange'); + } + protected log(...args: any[]): void { log.log(...args); } diff --git a/packages/uhk-agent/src/util/command-line.ts b/packages/uhk-agent/src/util/command-line.ts index 259c5e63c05..bf6b55d4055 100644 --- a/packages/uhk-agent/src/util/command-line.ts +++ b/packages/uhk-agent/src/util/command-line.ts @@ -57,7 +57,7 @@ const sections: commandLineUsage.Section[] = [ { name: 'log', description: 'Set logging categories. --log=misc,usb. Default is "misc"', - typeLabel: 'config | misc | usb | all' + typeLabel: 'config | misc | usb | usbOps | all' }, { name: 'no-report-id', diff --git a/packages/uhk-common/src/log/get-log-options.ts b/packages/uhk-common/src/log/get-log-options.ts index bdf9a77b86b..7339ebe2ea3 100644 --- a/packages/uhk-common/src/log/get-log-options.ts +++ b/packages/uhk-common/src/log/get-log-options.ts @@ -15,6 +15,7 @@ export function getLogOptions(options: CommandLineArgs): LogOptions { logOptions.config = true; logOptions.misc = true; logOptions.usb = true; + logOptions.usbOps = true; break; case 'config': @@ -27,6 +28,11 @@ export function getLogOptions(options: CommandLineArgs): LogOptions { case 'usb': logOptions.usb = true; + logOptions.usbOps = true; + break; + + case 'usbOps': + logOptions.usbOps = true; break; default: diff --git a/packages/uhk-common/src/log/log-reg-exps.ts b/packages/uhk-common/src/log/log-reg-exps.ts index 6871e6786d4..1f7131b1700 100644 --- a/packages/uhk-common/src/log/log-reg-exps.ts +++ b/packages/uhk-common/src/log/log-reg-exps.ts @@ -1,5 +1,4 @@ export namespace LogRegExps { - export const transferRegExp = /USB\[T]:/; export const writeRegExp = /USB\[W]:/; export const readRegExp = /USB\[R]:/; } diff --git a/packages/uhk-common/src/log/logger.service.ts b/packages/uhk-common/src/log/logger.service.ts index 713914b4435..8f1f7161a7f 100644 --- a/packages/uhk-common/src/log/logger.service.ts +++ b/packages/uhk-common/src/log/logger.service.ts @@ -44,6 +44,14 @@ export class LogService { this.log(...args); } + usbOps(...args: any[]): void { + if (!this._options.usbOps) { + return; + } + + this.log(...args); + } + protected log(...args: any[]): void { console.log(...args); } diff --git a/packages/uhk-common/src/models/log-options.ts b/packages/uhk-common/src/models/log-options.ts index 110228ae818..0a23e937f95 100644 --- a/packages/uhk-common/src/models/log-options.ts +++ b/packages/uhk-common/src/models/log-options.ts @@ -1,5 +1,12 @@ export interface LogOptions { + /** + * Log every USB operations USB[T], USB[W], USB[R] + */ usb?: boolean; + /** + * Log only USB[T] log entries + */ + usbOps?: boolean; config?: boolean; misc?: boolean; } diff --git a/packages/uhk-usb/src/uhk-hid-device.ts b/packages/uhk-usb/src/uhk-hid-device.ts index 1813a8ce23a..19e87ef7951 100644 --- a/packages/uhk-usb/src/uhk-hid-device.ts +++ b/packages/uhk-usb/src/uhk-hid-device.ts @@ -148,7 +148,7 @@ export class UhkHidDevice { public async deleteBond(address: number[]): Promise { await this.assertDeviceSupportWirelessUSBCommands(); - this.logService.usb('[UhkHidDevice] USB[T]: Delete all bonds'); + this.logService.usbOps('[UhkHidDevice] USB[T]: Delete all bonds'); const buffer = Buffer.from([UsbCommand.UnpairAll, ...address]); await this.write(buffer); } @@ -156,7 +156,7 @@ export class UhkHidDevice { public async deleteAllBonds(): Promise { await this.assertDeviceSupportWirelessUSBCommands(); - this.logService.usb('[UhkHidDevice] USB[T]: Delete all bonds'); + this.logService.usbOps('[UhkHidDevice] USB[T]: Delete all bonds'); const buffer = Buffer.from([UsbCommand.UnpairAll, 0, 0, 0, 0, 0, 0]); await this.write(buffer); } @@ -164,7 +164,7 @@ export class UhkHidDevice { public async getBleAddress(): Promise { await this.assertDeviceSupportWirelessUSBCommands(); - this.logService.usb('[UhkHidDevice] USB[T]: get BLE address'); + this.logService.usbOps('[UhkHidDevice] USB[T]: get BLE address'); const buffer = Buffer.from([UsbCommand.GetProperty, DevicePropertyIds.BleAddress]); const responseBuffer = await this.write(buffer); @@ -180,7 +180,7 @@ export class UhkHidDevice { public async getPairedRightPairBleAddress(): Promise { await this.assertDeviceSupportWirelessUSBCommands(); - this.logService.usb('[UhkHidDevice] USB[T]: get paired right pair BLE address'); + this.logService.usbOps('[UhkHidDevice] USB[T]: get paired right pair BLE address'); const buffer = Buffer.from([UsbCommand.GetProperty, DevicePropertyIds.PairedRightPeerBleAddress]); const responseBuffer = await this.write(buffer); @@ -196,7 +196,7 @@ export class UhkHidDevice { public async getPairingInfo(): Promise { await this.assertDeviceSupportWirelessUSBCommands(); - this.logService.usb('[UhkHidDevice] USB[T]: read pairing info'); + this.logService.usbOps('[UhkHidDevice] USB[T]: read pairing info'); const buffer = Buffer.from([UsbCommand.GetPairingData]); const responseBuffer = await this.write(buffer); const numbers = convertBufferToIntArray(responseBuffer); @@ -216,7 +216,7 @@ export class UhkHidDevice { public async getPairingStatus(): Promise { await this.assertDeviceSupportWirelessUSBCommands(); - this.logService.usb('[UhkHidDevice] USB[T]: read pairing info'); + this.logService.usbOps('[UhkHidDevice] USB[T]: read pairing info'); const buffer = Buffer.from([UsbCommand.GetProperty, DevicePropertyIds.PairingStatus]); const responseBuffer = await this.write(buffer); @@ -237,7 +237,7 @@ export class UhkHidDevice { public async isPairedWith(address: number[]): Promise { await this.assertDeviceSupportWirelessUSBCommands(); - this.logService.usb('[UhkHidDevice] USB[T]: is paired with'); + this.logService.usbOps('[UhkHidDevice] USB[T]: is paired with'); const buffer = Buffer.from([ UsbCommand.IsPaired, ...address, @@ -252,7 +252,7 @@ export class UhkHidDevice { public async pairCentral(): Promise { await this.assertDeviceSupportWirelessUSBCommands(); - this.logService.usb('[UhkHidDevice] USB[T]: pair central'); + this.logService.usbOps('[UhkHidDevice] USB[T]: pair central'); const buffer = Buffer.from([UsbCommand.PairCentral]); await this.write(buffer); } @@ -260,7 +260,7 @@ export class UhkHidDevice { public async pairPeripheral(): Promise { await this.assertDeviceSupportWirelessUSBCommands(); - this.logService.usb('[UhkHidDevice] USB[T]: pair peripheral'); + this.logService.usbOps('[UhkHidDevice] USB[T]: pair peripheral'); const buffer = Buffer.from([UsbCommand.PairPeripheral]); await this.write(buffer); } @@ -268,7 +268,7 @@ export class UhkHidDevice { public async setPairingInfo(pairId: PairIds, info: PairingInfo): Promise { await this.assertDeviceSupportWirelessUSBCommands(); - this.logService.usb('[UhkHidDevice] USB[T]: set pairing info'); + this.logService.usbOps('[UhkHidDevice] USB[T]: set pairing info'); const buffer = Buffer.from([ UsbCommand.SetPairingData, pairId, @@ -283,7 +283,7 @@ export class UhkHidDevice { public async switchToPairingMode(): Promise { await this.assertDeviceSupportWirelessUSBCommands(); - this.logService.usb('[UhkHidDevice] USB[T]: switch to pairing mode'); + this.logService.usbOps('[UhkHidDevice] USB[T]: switch to pairing mode'); const buffer = Buffer.from([UsbCommand.EnterPairingMode]); await this.write(buffer); } @@ -558,7 +558,7 @@ export class UhkHidDevice { (timeout & 0xff << 24) >> 24 ]); const data = this.getTransferData(message, reportId); - this.logService.usb(`[UhkHidDevice] USB[T]: Enumerated device, mode: ${reenumMode}`); + this.logService.usbOps(`[UhkHidDevice] USB[T]: Enumerated device, mode: ${reenumMode}`); this.logService.usb('[UhkHidDevice] USB[W]:', bufferToString(data).substr(3)); try { keyboardDevice.write(data); @@ -576,7 +576,7 @@ export class UhkHidDevice { } jumped = true; } else { - this.logService.usb('[UhkHidDevice] USB[T]: Enumerated device is not ready yet'); + this.logService.usbOps('[UhkHidDevice] USB[T]: Enumerated device is not ready yet'); } } else { @@ -592,7 +592,7 @@ export class UhkHidDevice { async sendKbootCommandToModule(module: ModuleSlotToI2cAddress, command: KbootCommands, maxTry = 1): Promise { let transfer; - this.logService.usb(`[UhkHidDevice] USB[T]: Send KbootCommand ${mapI2cAddressToModuleName(module)} ${KbootCommands[command].toString()}`); + this.logService.usbOps(`[UhkHidDevice] USB[T]: Send KbootCommand ${mapI2cAddressToModuleName(module)} ${KbootCommands[command].toString()}`); if (command === KbootCommands.idle) { transfer = Buffer.from([UsbCommand.SendKbootCommandToModule, command]); } else { @@ -678,7 +678,7 @@ export class UhkHidDevice { return this._protocolVersions; } - this.logService.usb('[UhkHidDevice] USB[T]: Read device protocol version information'); + this.logService.usbOps('[UhkHidDevice] USB[T]: Read device protocol version information'); const command = Buffer.from([UsbCommand.GetProperty, DevicePropertyIds.ProtocolVersions]); const buffer = await this.write(command); const uhkBuffer = UhkBuffer.fromArray(convertBufferToIntArray(buffer)); diff --git a/packages/uhk-usb/src/uhk-operations.ts b/packages/uhk-usb/src/uhk-operations.ts index 151ef5706c2..fcd21a020c2 100644 --- a/packages/uhk-usb/src/uhk-operations.ts +++ b/packages/uhk-usb/src/uhk-operations.ts @@ -71,7 +71,7 @@ export class UhkOperations { } public async jumpToBootloaderModule(module: ModuleSlotToId): Promise { - this.logService.usb(`[UhkHidDevice] USB[T]: Jump to bootloader. Module: ${ModuleSlotToId[module].toString()}`); + this.logService.usbOps(`[UhkHidDevice] USB[T]: Jump to bootloader. Module: ${ModuleSlotToId[module].toString()}`); const transfer = Buffer.from([UsbCommand.JumpToModuleBootloader, module]); await this.device.write(transfer); } @@ -285,7 +285,7 @@ export class UhkOperations { const configName = configBufferIdToName[configBufferId]; try { - this.logService.usb(`[DeviceOperation] USB[T]: Read ${configName} size from keyboard`); + this.logService.usbOps(`[DeviceOperation] USB[T]: Read ${configName} size from keyboard`); let configSize = await this.getConfigSizeFromKeyboard(configBufferId); const originalConfigSize = configSize; this.logService.usb(`[DeviceOperation] getConfigSize() configSize: ${configSize}`); @@ -294,7 +294,7 @@ export class UhkOperations { let configBuffer = Buffer.alloc(0); let firstRead = true; - this.logService.usb(`[DeviceOperation] USB[T]: Read ${configName} from keyboard`); + this.logService.usbOps(`[DeviceOperation] USB[T]: Read ${configName} from keyboard`); while (offset < configSize) { const chunkSizeToRead = Math.min(chunkSize, configSize - offset); const writeBuffer = Buffer.from( @@ -348,10 +348,10 @@ export class UhkOperations { public async saveUserConfiguration(buffer: Buffer): Promise { try { - this.logService.usb('[DeviceOperation] USB[T]: Write user configuration to keyboard'); + this.logService.usbOps('[DeviceOperation] USB[T]: Write user configuration to keyboard'); await this.sendConfigToKeyboard(buffer, true); await this.applyConfiguration(); - this.logService.usb('[DeviceOperation] USB[T]: Write user configuration to EEPROM'); + this.logService.usbOps('[DeviceOperation] USB[T]: Write user configuration to EEPROM'); await this.writeConfigToEeprom(ConfigBufferId.validatedUserConfig); await this.waitUntilKeyboardBusy(); } catch (error) { @@ -387,13 +387,13 @@ export class UhkOperations { } public async writeConfigToEeprom(configBufferId: ConfigBufferId): Promise { - this.logService.usb('[DeviceOperation] USB[T]: Write Config to Eeprom'); + this.logService.usbOps('[DeviceOperation] USB[T]: Write Config to Eeprom'); await this.device.write(Buffer.from([UsbCommand.LaunchEepromTransfer, EepromOperation.write, configBufferId])); await this.waitUntilKeyboardBusy(); } public async enableUsbStackTest(): Promise { - this.logService.usb('[DeviceOperation] USB[T]: Enable USB Stack test'); + this.logService.usbOps('[DeviceOperation] USB[T]: Enable USB Stack test'); await this.device.write(Buffer.from([UsbCommand.SetVariable, UsbVariables.testUsbStack, 1])); await this.waitUntilKeyboardBusy(); } @@ -431,7 +431,7 @@ export class UhkOperations { public async getModuleProperty({ module, property } : GetModulePropertyArguments): Promise { const moduleSlotName = getSlotIdName(module); - this.logService.usb(`[DeviceOperation] USB[T]: Read "${moduleSlotName}" module "${ModulePropertyId[property]}" property information as string`); + this.logService.usbOps(`[DeviceOperation] USB[T]: Read "${moduleSlotName}" module "${ModulePropertyId[property]}" property information as string`); const command = Buffer.from([ UsbCommand.GetModuleProperty, @@ -469,7 +469,7 @@ export class UhkOperations { const moduleSlotName = getSlotIdName(module); try { this.logService.misc(`[DeviceOperation] Read "${moduleSlotName}" version information`); - this.logService.usb('[DeviceOperation] USB[T]: Read module version information'); + this.logService.usbOps('[DeviceOperation] USB[T]: Read module version information'); const uhkBuffer = await this.getModuleProperty({ module, property: ModulePropertyId.protocolVersions }); // skip the first 2 byte @@ -506,7 +506,7 @@ export class UhkOperations { } public async getRightModuleProperty(property: DevicePropertyIds, args: Array = []): Promise { - this.logService.usb(`[DeviceOperation] USB[T]: Device module "${DevicePropertyIds[property]}" property information`); + this.logService.usbOps(`[DeviceOperation] USB[T]: Device module "${DevicePropertyIds[property]}" property information`); const command = Buffer.from([UsbCommand.GetProperty, property, ...args]); const buffer = await this.device.write(command); @@ -514,7 +514,7 @@ export class UhkOperations { } public async getRightModuleFirmwareRepoInfo(): Promise { - this.logService.usb('[DeviceOperation] USB[T]: Read device firmware repo information'); + this.logService.usbOps('[DeviceOperation] USB[T]: Read device firmware repo information'); return { firmwareGitRepo: readUhkResponseAs0EndString(await this.getRightModuleProperty(DevicePropertyIds.GitRepo)), @@ -524,7 +524,7 @@ export class UhkOperations { public async getDeviceVersionInfo(): Promise { // TODO: read device name from UHK Device - this.logService.usb('[DeviceOperation] USB[T]: Device information'); + this.logService.usbOps('[DeviceOperation] USB[T]: Device information'); const protocolVersions = await this.device.getProtocolVersions(); @@ -549,47 +549,47 @@ export class UhkOperations { } public async setLedPwmBrightness(percent: number): Promise { - this.logService.usb('[DeviceOperation] USB[T]: Set LED PWM Brightness'); + this.logService.usbOps('[DeviceOperation] USB[T]: Set LED PWM Brightness'); const command = Buffer.from([UsbCommand.SetLedPwmBrightness, percent]); await this.device.write(command); } public async applyConfiguration(): Promise { - this.logService.usb('[DeviceOperation] USB[T]: Apply user configuration to keyboard'); + this.logService.usbOps('[DeviceOperation] USB[T]: Apply user configuration to keyboard'); const applyBuffer = Buffer.from([UsbCommand.ApplyConfig]); await this.device.write(applyBuffer); } public async setTestLedsState(on: boolean): Promise { - this.logService.usb('[DeviceOperation] USB[T]: Set test LEDs state'); + this.logService.usbOps('[DeviceOperation] USB[T]: Set test LEDs state'); const buffer = Buffer.from([UsbCommand.SetTestLed, on ? 1 : 0]); await this.device.write(buffer); } public async launchEepromTransfer(operation: EepromOperation, bufferId: ConfigBufferId): Promise { - this.logService.usb('[DeviceOperation] USB[T]: Launch EEPORM transfer'); + this.logService.usbOps('[DeviceOperation] USB[T]: Launch EEPORM transfer'); const buffer = Buffer.from([UsbCommand.LaunchEepromTransfer, operation, bufferId]); return this.device.write(buffer); } public async eraseHardwareConfig(): Promise { - this.logService.usb('[DeviceOperation] USB[T]: Erase hardware configuration'); + this.logService.usbOps('[DeviceOperation] USB[T]: Erase hardware configuration'); const buffer = Buffer.from(Array(64).fill(0xff)); await this.sendConfigToKeyboard(buffer, false); await this.writeConfigToEeprom(ConfigBufferId.hardwareConfig); } public async eraseUserConfig(): Promise { - this.logService.usb('[DeviceOperation] USB[T]: Erase user configuration'); + this.logService.usbOps('[DeviceOperation] USB[T]: Erase user configuration'); const buffer = Buffer.from(Array(2 ** 15 - 64).fill(0xff)); await this.sendConfigToKeyboard(buffer, false); await this.writeConfigToEeprom(ConfigBufferId.stagingUserConfig); } public async switchKeymap(keymapAbbreviation: string): Promise { - this.logService.usb('[DeviceOperation] USB[T]: Switch keymap'); + this.logService.usbOps('[DeviceOperation] USB[T]: Switch keymap'); const keymapAbbreviationAscii = keymapAbbreviation.split('').map(char => char.charCodeAt(0)); const buffer = Buffer.from([UsbCommand.SwitchKeymap, keymapAbbreviationAscii.length, ...keymapAbbreviationAscii]); @@ -597,14 +597,14 @@ export class UhkOperations { } public async getAdcValue(): Promise { - this.logService.usb('[DeviceOperation] USB[T]: get ADC Value'); + this.logService.usbOps('[DeviceOperation] USB[T]: get ADC Value'); const buffer = Buffer.from([UsbCommand.GetAdcValue]); return this.device.write(buffer); } public async getDebugInfo(): Promise { - this.logService.usb('[DeviceOperation] USB[T]: get Debug info'); + this.logService.usbOps('[DeviceOperation] USB[T]: get Debug info'); const buffer = Buffer.from([UsbCommand.GetDebugBuffer]); const responseBuffer = await this.device.write(buffer); @@ -626,7 +626,7 @@ export class UhkOperations { } public async getI2CBaudRate(): Promise { - this.logService.usb('[DeviceOperation] USB[T]: get I2C Baud rate'); + this.logService.usbOps('[DeviceOperation] USB[T]: get I2C Baud rate'); const buffer = Buffer.from([UsbCommand.GetProperty, DevicePropertyIds.I2cBaudRate]); const responseBuffer = await this.device.write(buffer); @@ -639,7 +639,7 @@ export class UhkOperations { } public async getUptime(): Promise { - this.logService.usb('[DeviceOperation] USB[T]: get uptime'); + this.logService.usbOps('[DeviceOperation] USB[T]: get uptime'); const buffer = Buffer.from([UsbCommand.GetProperty, DevicePropertyIds.Uptime]); const responseBuffer = await this.device.write(buffer); @@ -647,7 +647,7 @@ export class UhkOperations { } public async getI2cSlaveErrors(slaveId: number): Promise { - this.logService.usb('[DeviceOperation] USB[T]: get I2C Slave errors'); + this.logService.usbOps('[DeviceOperation] USB[T]: get I2C Slave errors'); const buffer = Buffer.from([UsbCommand.GetSlaveI2cErrors, slaveId]); const responseBuffer = await this.device.write(buffer); @@ -655,7 +655,7 @@ export class UhkOperations { } public async getVariable(variableId: UsbVariables, iteration: number = 0): Promise { - this.logService.usb(`[DeviceOperation] USB[T]: get variable: ${UsbVariables[variableId]}. Iteration: ${iteration}`); + this.logService.usbOps(`[DeviceOperation] USB[T]: get variable: ${UsbVariables[variableId]}. Iteration: ${iteration}`); const buffer = Buffer.from([UsbCommand.GetVariable, variableId]); const responseBuffer = await this.device.write(buffer); @@ -811,13 +811,13 @@ export class UhkOperations { } public async setVariable(variable: UsbVariables, value: number): Promise { - this.logService.usb('[DeviceOperation] USB[T]: Set Variable'); + this.logService.usbOps('[DeviceOperation] USB[T]: Set Variable'); await this.device.write(Buffer.from([UsbCommand.SetVariable, variable, value])); await this.waitUntilKeyboardBusy(); } public async setI2CBaudRate(rate: number): Promise { - this.logService.usb('[DeviceOperation] USB[T]: Set I2C Baud Rate'); + this.logService.usbOps('[DeviceOperation] USB[T]: Set I2C Baud Rate'); const buffer = Buffer.alloc(5); buffer.writeUInt8(UsbCommand.SetI2cBaudRate, 0); buffer.writeUInt32LE(rate, 1); @@ -843,7 +843,7 @@ export class UhkOperations { } public async execMacroCommand(cmd: string): Promise { - this.logService.usb('[DeviceOperation] USB[T]: Execute Macro Command'); + this.logService.usbOps('[DeviceOperation] USB[T]: Execute Macro Command'); if (cmd.length <= 63) { const b1 = Buffer.from([UsbCommand.ExecMacroCommand]); const b2 = Buffer.from(cmd); diff --git a/packages/uhk-web/src/renderer/services/electron-log.service.ts b/packages/uhk-web/src/renderer/services/electron-log.service.ts index 7012f7f6db5..d4d6d4672c9 100644 --- a/packages/uhk-web/src/renderer/services/electron-log.service.ts +++ b/packages/uhk-web/src/renderer/services/electron-log.service.ts @@ -49,6 +49,14 @@ export class ElectronLogService extends LogService { this.log(...args); } + usbOps(...args: any[]): void { + if (!this._options.usbOps) { + return; + } + + this.log(...args); + } + protected log(...args: any[]): void { log.log(...args); } diff --git a/packages/usb/src/command-line.ts b/packages/usb/src/command-line.ts index cccbcaaeb8a..2fbc8e75fab 100644 --- a/packages/usb/src/command-line.ts +++ b/packages/usb/src/command-line.ts @@ -17,7 +17,7 @@ export const yargs = Yargs(hideBin(process.argv)) description: 'Display help message' }) .option('log', { - description: 'Set logging categories. --log=misc,usb. Options: {all|config|misc|none|usb} Default is "none"', + description: 'Set logging categories. --log=misc,usb. Options: {all|config|misc|none|usb|usbOps} Default is "none"', type: 'string', default: 'none', })