forked from dotnet/vscode-csharp
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlogger.ts
68 lines (53 loc) · 1.82 KB
/
logger.ts
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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
let Subscriber: (message: string) => void;
export function SubscribeToAllLoggers(subscriber: (message: string) => void) {
Subscriber = subscriber;
}
export class Logger {
private _writer: (message: string) => void;
private _prefix: string | undefined;
private _indentLevel = 0;
private _indentSize = 4;
private _atLineStart = false;
constructor(writer: (message: string) => void, prefix?: string) {
this._writer = writer;
this._prefix = prefix;
}
private _appendCore(message: string): void {
if (this._atLineStart) {
if (this._indentLevel > 0) {
const indent = ' '.repeat(this._indentLevel * this._indentSize);
this.write(indent);
}
if (this._prefix !== undefined) {
this.write(`[${this._prefix}] `);
}
this._atLineStart = false;
}
this.write(message);
}
public increaseIndent(): void {
this._indentLevel += 1;
}
public decreaseIndent(): void {
if (this._indentLevel > 0) {
this._indentLevel -= 1;
}
}
public append(message = ''): void {
this._appendCore(message);
}
public appendLine(message = ''): void {
this._appendCore(message + '\n');
this._atLineStart = true;
}
private write(message: string) {
this._writer(message);
if (Subscriber) {
Subscriber(message);
}
}
}