| 1 |
/** |
| 2 |
* Copyright (c) 2019 The xterm.js authors. All rights reserved. |
| 3 |
* @license MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
import { Disposable } from 'vs/base/common/lifecycle'; |
| 7 |
import { ILogService, IOptionsService, LogLevelEnum } from 'common/services/Services'; |
| 8 |
|
| 9 |
type LogType = (message?: any, ...optionalParams: any[]) => void; |
| 10 |
|
| 11 |
interface IConsole { |
| 12 |
log: LogType; |
| 13 |
error: LogType; |
| 14 |
info: LogType; |
| 15 |
trace: LogType; |
| 16 |
warn: LogType; |
| 17 |
} |
| 18 |
|
| 19 |
// console is available on both node.js and browser contexts but the common |
| 20 |
// module doesn't depend on them so we need to explicitly declare it. |
| 21 |
declare const console: IConsole; |
| 22 |
|
| 23 |
const optionsKeyToLogLevel: { [key: string]: LogLevelEnum } = { |
| 24 |
trace: LogLevelEnum.TRACE, |
| 25 |
debug: LogLevelEnum.DEBUG, |
| 26 |
info: LogLevelEnum.INFO, |
| 27 |
warn: LogLevelEnum.WARN, |
| 28 |
error: LogLevelEnum.ERROR, |
| 29 |
off: LogLevelEnum.OFF |
| 30 |
}; |
| 31 |
|
| 32 |
const LOG_PREFIX = 'xterm.js: '; |
| 33 |
|
| 34 |
export class LogService extends Disposable implements ILogService { |
| 35 |
public serviceBrand: any; |
| 36 |
|
| 37 |
private _logLevel: LogLevelEnum = LogLevelEnum.OFF; |
| 38 |
public get logLevel(): LogLevelEnum { return this._logLevel; } |
| 39 |
|
| 40 |
constructor( |
| 41 |
@IOptionsService private readonly _optionsService: IOptionsService |
| 42 |
) { |
| 43 |
super(); |
| 44 |
this._updateLogLevel(); |
| 45 |
this._register(this._optionsService.onSpecificOptionChange('logLevel', () => this._updateLogLevel())); |
| 46 |
|
| 47 |
// For trace logging, assume the latest created log service is valid |
| 48 |
traceLogger = this; |
| 49 |
} |
| 50 |
|
| 51 |
private _updateLogLevel(): void { |
| 52 |
this._logLevel = optionsKeyToLogLevel[this._optionsService.rawOptions.logLevel]; |
| 53 |
} |
| 54 |
|
| 55 |
private _evalLazyOptionalParams(optionalParams: any[]): void { |
| 56 |
for (let i = 0; i < optionalParams.length; i++) { |
| 57 |
if (typeof optionalParams[i] === 'function') { |
| 58 |
optionalParams[i] = optionalParams[i](); |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
private _log(type: LogType, message: string, optionalParams: any[]): void { |
| 64 |
this._evalLazyOptionalParams(optionalParams); |
| 65 |
type.call(console, (this._optionsService.options.logger ? '' : LOG_PREFIX) + message, ...optionalParams); |
| 66 |
} |
| 67 |
|
| 68 |
public trace(message: string, ...optionalParams: any[]): void { |
| 69 |
if (this._logLevel <= LogLevelEnum.TRACE) { |
| 70 |
this._log(this._optionsService.options.logger?.trace.bind(this._optionsService.options.logger) ?? console.log, message, optionalParams); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
public debug(message: string, ...optionalParams: any[]): void { |
| 75 |
if (this._logLevel <= LogLevelEnum.DEBUG) { |
| 76 |
this._log(this._optionsService.options.logger?.debug.bind(this._optionsService.options.logger) ?? console.log, message, optionalParams); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
public info(message: string, ...optionalParams: any[]): void { |
| 81 |
if (this._logLevel <= LogLevelEnum.INFO) { |
| 82 |
this._log(this._optionsService.options.logger?.info.bind(this._optionsService.options.logger) ?? console.info, message, optionalParams); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
public warn(message: string, ...optionalParams: any[]): void { |
| 87 |
if (this._logLevel <= LogLevelEnum.WARN) { |
| 88 |
this._log(this._optionsService.options.logger?.warn.bind(this._optionsService.options.logger) ?? console.warn, message, optionalParams); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
public error(message: string, ...optionalParams: any[]): void { |
| 93 |
if (this._logLevel <= LogLevelEnum.ERROR) { |
| 94 |
this._log(this._optionsService.options.logger?.error.bind(this._optionsService.options.logger) ?? console.error, message, optionalParams); |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
let traceLogger: ILogService; |
| 100 |
export function setTraceLogger(logger: ILogService): void { |
| 101 |
traceLogger = logger; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* A decorator that can be used to automatically log trace calls to the decorated function. |
| 106 |
*/ |
| 107 |
export function traceCall(_target: any, key: string, descriptor: any): any { |
| 108 |
if (typeof descriptor.value !== 'function') { |
| 109 |
throw new Error('not supported'); |
| 110 |
} |
| 111 |
const fnKey = 'value'; |
| 112 |
const fn = descriptor.value; |
| 113 |
descriptor[fnKey] = function (...args: any[]) { |
| 114 |
// Early exit |
| 115 |
if (traceLogger.logLevel !== LogLevelEnum.TRACE) { |
| 116 |
return fn.apply(this, args); |
| 117 |
} |
| 118 |
|
| 119 |
traceLogger.trace(`GlyphRenderer#${fn.name}(${args.map(e => JSON.stringify(e)).join(', ')})`); |
| 120 |
const result = fn.apply(this, args); |
| 121 |
traceLogger.trace(`GlyphRenderer#${fn.name} return`, result); |
| 122 |
return result; |
| 123 |
}; |
| 124 |
} |