-
Notifications
You must be signed in to change notification settings - Fork 733
Expand file tree
/
Copy pathlogger.ts
More file actions
74 lines (61 loc) · 2.54 KB
/
logger.ts
File metadata and controls
74 lines (61 loc) · 2.54 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as vscode from 'vscode';
import { Disposable } from './lifecycle';
export const PR_TREE = 'PullRequestTree';
interface Stringish {
toString: () => string;
}
class Log extends Disposable {
private readonly _outputChannel: vscode.LogOutputChannel;
private readonly _activePerfMarkers: Map<string, number> = new Map();
constructor() {
super();
this._outputChannel = this._register(vscode.window.createOutputChannel('GitHub Pull Request', { log: true }));
}
public startPerfMarker(marker: string) {
const startTime = performance.now();
this._outputChannel.appendLine(`[PERF_MARKER] Start ${marker}`);
this._activePerfMarkers.set(marker, startTime);
}
public endPerfMarker(marker: string) {
const endTime = performance.now();
this._outputChannel.appendLine(`[PERF_MARKER] End ${marker}: ${endTime - this._activePerfMarkers.get(marker)!} ms`);
this._activePerfMarkers.delete(marker);
}
private logString(message: string | Error | Stringish | Object, component?: string): string {
let logMessage: string;
if (typeof message !== 'string') {
const asString = message as Partial<Stringish>;
if (message instanceof Error) {
logMessage = message.message;
} else if (asString.toString) {
logMessage = asString.toString();
} else {
logMessage = JSON.stringify(message);
}
} else {
logMessage = message;
}
return component ? `[${component}] ${logMessage}` : logMessage;
}
public trace(message: string | Error | Stringish | Object, component: string) {
this._outputChannel.trace(this.logString(message, component));
}
public debug(message: string | Error | Stringish | Object, component: string) {
this._outputChannel.debug(this.logString(message, component));
}
public appendLine(message: string | Error | Stringish | Object, component: string) {
this._outputChannel.info(this.logString(message, component));
}
public warn(message: string | Error | Stringish | Object, component?: string) {
this._outputChannel.warn(this.logString(message, component));
}
public error(message: string | Error | Stringish | Object, component: string) {
this._outputChannel.error(this.logString(message, component));
}
}
const Logger = new Log();
export default Logger;