From 293f54e8dd9c8d46232a762b807e723eb64ace7a Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Thu, 12 Sep 2019 11:04:41 -0700 Subject: [PATCH 1/3] Improve debug logging by printing character codes --- src/InputHandler.ts | 2 +- src/common/services/CoreService.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/InputHandler.ts b/src/InputHandler.ts index 56b2d8be..7a0aa8c8 100644 --- a/src/InputHandler.ts +++ b/src/InputHandler.ts @@ -329,7 +329,7 @@ export class InputHandler extends Disposable implements IInputHandler { const cursorStartX = buffer.x; const cursorStartY = buffer.y; - this._logService.debug('parsing data', data); + this._logService.debug(`parsing data "${data}"`, data.split('').map(e => e.charCodeAt(0))); if (this._parseBuffer.length < data.length) { this._parseBuffer = new Uint32Array(data.length); diff --git a/src/common/services/CoreService.ts b/src/common/services/CoreService.ts index d17b0c93..71f45e47 100644 --- a/src/common/services/CoreService.ts +++ b/src/common/services/CoreService.ts @@ -54,7 +54,7 @@ export class CoreService implements ICoreService { } // Fire onData API - this._logService.debug('sending data', data); + this._logService.debug(`sending data "${data}"`, data.split('').map(e => e.charCodeAt(0))); this._onData.fire(data); } } From fc4f34544663f6a0bf0762a4574c3148f6274146 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Thu, 12 Sep 2019 11:24:04 -0700 Subject: [PATCH 2/3] Don't transform incoming data This would run for all data --- src/InputHandler.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/InputHandler.ts b/src/InputHandler.ts index 7a0aa8c8..56b2d8be 100644 --- a/src/InputHandler.ts +++ b/src/InputHandler.ts @@ -329,7 +329,7 @@ export class InputHandler extends Disposable implements IInputHandler { const cursorStartX = buffer.x; const cursorStartY = buffer.y; - this._logService.debug(`parsing data "${data}"`, data.split('').map(e => e.charCodeAt(0))); + this._logService.debug('parsing data', data); if (this._parseBuffer.length < data.length) { this._parseBuffer = new Uint32Array(data.length); From 761e437c69da456b41c007e9196426903676ad43 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Thu, 12 Sep 2019 13:38:30 -0700 Subject: [PATCH 3/3] Allow lazy evaluated log params --- src/common/services/CoreService.ts | 2 +- src/common/services/LogService.ts | 33 ++++++++++++++++++++++-------- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/common/services/CoreService.ts b/src/common/services/CoreService.ts index 71f45e47..0e0ba609 100644 --- a/src/common/services/CoreService.ts +++ b/src/common/services/CoreService.ts @@ -54,7 +54,7 @@ export class CoreService implements ICoreService { } // Fire onData API - this._logService.debug(`sending data "${data}"`, data.split('').map(e => e.charCodeAt(0))); + this._logService.debug(`sending data "${data}"`, () => data.split('').map(e => e.charCodeAt(0))); this._onData.fire(data); } } diff --git a/src/common/services/LogService.ts b/src/common/services/LogService.ts index 6740ad4a..4f48d8fe 100644 --- a/src/common/services/LogService.ts +++ b/src/common/services/LogService.ts @@ -5,12 +5,14 @@ import { ILogService, IOptionsService } from 'common/services/Services'; +type LogType = (message?: any, ...optionalParams: any[]) => void; + interface IConsole { - log(message?: any, ...optionalParams: any[]): void; - error(message?: any, ...optionalParams: any[]): void; - info(message?: any, ...optionalParams: any[]): void; - trace(message?: any, ...optionalParams: any[]): void; - warn(message?: any, ...optionalParams: any[]): void; + log: LogType; + error: LogType; + info: LogType; + trace: LogType; + warn: LogType; } // console is available on both node.js and browser contexts but the common @@ -56,27 +58,40 @@ export class LogService implements ILogService { this._logLevel = optionsKeyToLogLevel[this._optionsService.options.logLevel]; } + private _evalLazyOptionalParams(optionalParams: any[]): void { + for (let i = 0; i < optionalParams.length; i++) { + if (typeof optionalParams[i] === 'function') { + optionalParams[i] = optionalParams[i](); + } + } + } + + private _log(type: LogType, message: string, optionalParams: any[]): void { + this._evalLazyOptionalParams(optionalParams); + type.call(console, LOG_PREFIX + message, ...optionalParams); + } + debug(message: string, ...optionalParams: any[]): void { if (this._logLevel <= LogLevel.DEBUG) { - console.log.call(console, LOG_PREFIX + message, ...optionalParams); + this._log(console.log, message, optionalParams); } } info(message: string, ...optionalParams: any[]): void { if (this._logLevel <= LogLevel.INFO) { - console.info.call(console, LOG_PREFIX + message, ...optionalParams); + this._log(console.info, message, optionalParams); } } warn(message: string, ...optionalParams: any[]): void { if (this._logLevel <= LogLevel.WARN) { - console.warn.call(console, LOG_PREFIX + message, ...optionalParams); + this._log(console.warn, message, optionalParams); } } error(message: string, ...optionalParams: any[]): void { if (this._logLevel <= LogLevel.ERROR) { - console.error.call(console, LOG_PREFIX + message, ...optionalParams); + this._log(console.error, message, optionalParams); } } }