Skip to content

Commit

Permalink
Add logger delegate option
Browse files Browse the repository at this point in the history
This allows embedders to route the logs whereever they want, when this
is set the xterm.js prefix is excluded.
  • Loading branch information
Tyriar committed Jun 16, 2023
1 parent 380e680 commit f779e4d
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 6 deletions.
10 changes: 5 additions & 5 deletions src/common/services/LogService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,30 +57,30 @@ export class LogService extends Disposable implements ILogService {

private _log(type: LogType, message: string, optionalParams: any[]): void {
this._evalLazyOptionalParams(optionalParams);
type.call(console, LOG_PREFIX + message, ...optionalParams);
type.call(console, (this._optionsService.options.logger ? '' : LOG_PREFIX) + message, ...optionalParams);
}

public debug(message: string, ...optionalParams: any[]): void {
if (this.logLevel <= LogLevelEnum.DEBUG) {
this._log(console.log, message, optionalParams);
this._log(this._optionsService.options.logger?.debug ?? console.log, message, optionalParams);
}
}

public info(message: string, ...optionalParams: any[]): void {
if (this.logLevel <= LogLevelEnum.INFO) {
this._log(console.info, message, optionalParams);
this._log(this._optionsService.options.logger?.info ?? console.info, message, optionalParams);
}
}

public warn(message: string, ...optionalParams: any[]): void {
if (this.logLevel <= LogLevelEnum.WARN) {
this._log(console.warn, message, optionalParams);
this._log(this._optionsService.options.logger?.warn ?? console.warn, message, optionalParams);
}
}

public error(message: string, ...optionalParams: any[]): void {
if (this.logLevel <= LogLevelEnum.ERROR) {
this._log(console.error, message, optionalParams);
this._log(this._optionsService.options.logger?.error ?? console.error, message, optionalParams);
}
}
}
1 change: 1 addition & 0 deletions src/common/services/OptionsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const DEFAULT_OPTIONS: Readonly<Required<ITerminalOptions>> = {
letterSpacing: 0,
linkHandler: null,
logLevel: 'info',
logger: null,
scrollback: 1000,
scrollOnUserInput: true,
scrollSensitivity: 1,
Expand Down
3 changes: 2 additions & 1 deletion src/common/services/Services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { IEvent, IEventEmitter } from 'common/EventEmitter';
import { IBuffer, IBufferSet } from 'common/buffer/Types';
import { IDecPrivateModes, ICoreMouseEvent, CoreMouseEncoding, ICoreMouseProtocol, CoreMouseEventType, ICharset, IWindowOptions, IModes, IAttributeData, ScrollSource, IDisposable, IColor, CursorStyle, IOscLinkData } from 'common/Types';
import { createDecorator } from 'common/services/ServiceRegistry';
import { IDecorationOptions, IDecoration, ILinkHandler, IWindowsPty } from 'xterm';
import { IDecorationOptions, IDecoration, ILinkHandler, IWindowsPty, ILogger } from 'xterm';

export const IBufferService = createDecorator<IBufferService>('BufferService');
export interface IBufferService {
Expand Down Expand Up @@ -229,6 +229,7 @@ export interface ITerminalOptions {
lineHeight?: number;
linkHandler?: ILinkHandler | null;
logLevel?: LogLevel;
logger?: ILogger | null;
macOptionIsMeta?: boolean;
macOptionClickForcesSelection?: boolean;
minimumContrastRatio?: number;
Expand Down
31 changes: 31 additions & 0 deletions typings/xterm-headless.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ declare module 'xterm-headless' {
*/
logLevel?: LogLevel;

/**
* A logger to use instead of `console`.
*/
logger?: ILogger | null;

/**
* Whether to treat option as the meta key.
*/
Expand Down Expand Up @@ -304,6 +309,32 @@ declare module 'xterm-headless' {
buildNumber?: number;
}

/**
* A replacement logger for `console`.
*/
export interface ILogger {
/**
* Log a debug message, this will only be called if {@link ITerminalOptions.logLevel} is set to
* debug.
*/
debug(message: string, ...args: any[]): void;
/**
* Log a debug message, this will only be called if {@link ITerminalOptions.logLevel} is set to
* info or below.
*/
info(message: string, ...args: any[]): void;
/**
* Log a debug message, this will only be called if {@link ITerminalOptions.logLevel} is set to
* warn or below.
*/
warn(message: string, ...args: any[]): void;
/**
* Log a debug message, this will only be called if {@link ITerminalOptions.logLevel} is set to
* error or below.
*/
error(message: string | Error, ...args: any[]): void;
}

/**
* An object that can be disposed via a dispose function.
*/
Expand Down
31 changes: 31 additions & 0 deletions typings/xterm.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ declare module 'xterm' {
*/
logLevel?: LogLevel;

/**
* A logger to use instead of `console`.
*/
logger?: ILogger | null;

/**
* Whether to treat option as the meta key.
*/
Expand Down Expand Up @@ -365,6 +370,32 @@ declare module 'xterm' {
buildNumber?: number;
}

/**
* A replacement logger for `console`.
*/
export interface ILogger {
/**
* Log a debug message, this will only be called if {@link ITerminalOptions.logLevel} is set to
* debug.
*/
debug(message: string, ...args: any[]): void;
/**
* Log a debug message, this will only be called if {@link ITerminalOptions.logLevel} is set to
* info or below.
*/
info(message: string, ...args: any[]): void;
/**
* Log a debug message, this will only be called if {@link ITerminalOptions.logLevel} is set to
* warn or below.
*/
warn(message: string, ...args: any[]): void;
/**
* Log a debug message, this will only be called if {@link ITerminalOptions.logLevel} is set to
* error or below.
*/
error(message: string | Error, ...args: any[]): void;
}

/**
* An object that can be disposed via a dispose function.
*/
Expand Down

0 comments on commit f779e4d

Please sign in to comment.