2 * Copyright (c) 2019 The xterm.js authors. All rights reserved.
6 import { ILogService, IOptionsService } from 'common/services/Services';
8 type LogType = (message?: any, ...optionalParams: any[]) => void;
18 // console is available on both node.js and browser contexts but the common
19 // module doesn't depend on them so we need to explicitly declare it.
20 declare const console: IConsole;
23 export enum LogLevel {
31 const optionsKeyToLogLevel: { [key: string]: LogLevel } = {
32 debug: LogLevel.DEBUG,
35 error: LogLevel.ERROR,
39 const LOG_PREFIX = 'xterm.js: ';
41 export class LogService implements ILogService {
44 private _logLevel!: LogLevel;
47 @IOptionsService private readonly _optionsService: IOptionsService
49 this._updateLogLevel();
50 this._optionsService.onOptionChange(key => {
51 if (key === 'logLevel') {
52 this._updateLogLevel();
57 private _updateLogLevel(): void {
58 this._logLevel = optionsKeyToLogLevel[this._optionsService.options.logLevel];
61 private _evalLazyOptionalParams(optionalParams: any[]): void {
62 for (let i = 0; i < optionalParams.length; i++) {
63 if (typeof optionalParams[i] === 'function') {
64 optionalParams[i] = optionalParams[i]();
69 private _log(type: LogType, message: string, optionalParams: any[]): void {
70 this._evalLazyOptionalParams(optionalParams);
71 type.call(console, LOG_PREFIX + message, ...optionalParams);
74 debug(message: string, ...optionalParams: any[]): void {
75 if (this._logLevel <= LogLevel.DEBUG) {
76 this._log(console.log, message, optionalParams);
80 info(message: string, ...optionalParams: any[]): void {
81 if (this._logLevel <= LogLevel.INFO) {
82 this._log(console.info, message, optionalParams);
86 warn(message: string, ...optionalParams: any[]): void {
87 if (this._logLevel <= LogLevel.WARN) {
88 this._log(console.warn, message, optionalParams);
92 error(message: string, ...optionalParams: any[]): void {
93 if (this._logLevel <= LogLevel.ERROR) {
94 this._log(console.error, message, optionalParams);