forked from irinazheltisheva/vscode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbufferLog.ts
More file actions
96 lines (78 loc) · 2.52 KB
/
Copy pathbufferLog.ts
File metadata and controls
96 lines (78 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { ILogService, LogLevel, AbstractLogService, DEFAULT_LOG_LEVEL } from 'vs/platform/log/common/log';
interface ILog {
level: LogLevel;
args: any[];
}
function getLogFunction(logger: ILogService, level: LogLevel): Function {
switch (level) {
case LogLevel.Trace: return logger.trace;
case LogLevel.Debug: return logger.debug;
case LogLevel.Info: return logger.info;
case LogLevel.Warning: return logger.warn;
case LogLevel.Error: return logger.error;
case LogLevel.Critical: return logger.critical;
default: throw new Error('Invalid log level');
}
}
export class BufferLogService extends AbstractLogService implements ILogService {
declare readonly _serviceBrand: undefined;
private buffer: ILog[] = [];
private _logger: ILogService | undefined = undefined;
constructor(logLevel: LogLevel = DEFAULT_LOG_LEVEL) {
super();
this.setLevel(logLevel);
this._register(this.onDidChangeLogLevel(level => {
if (this._logger) {
this._logger.setLevel(level);
}
}));
}
set logger(logger: ILogService) {
this._logger = logger;
for (const { level, args } of this.buffer) {
const fn = getLogFunction(logger, level);
fn.apply(logger, args);
}
this.buffer = [];
}
private _log(level: LogLevel, ...args: any[]): void {
if (this._logger) {
const fn = getLogFunction(this._logger, level);
fn.apply(this._logger, args);
} else if (this.getLevel() <= level) {
this.buffer.push({ level, args });
}
}
trace(message: string, ...args: any[]): void {
this._log(LogLevel.Trace, message, ...args);
}
debug(message: string, ...args: any[]): void {
this._log(LogLevel.Debug, message, ...args);
}
info(message: string, ...args: any[]): void {
this._log(LogLevel.Info, message, ...args);
}
warn(message: string, ...args: any[]): void {
this._log(LogLevel.Warning, message, ...args);
}
error(message: string | Error, ...args: any[]): void {
this._log(LogLevel.Error, message, ...args);
}
critical(message: string | Error, ...args: any[]): void {
this._log(LogLevel.Critical, message, ...args);
}
dispose(): void {
if (this._logger) {
this._logger.dispose();
}
}
flush(): void {
if (this._logger) {
this._logger.flush();
}
}
}