-
Notifications
You must be signed in to change notification settings - Fork 511
/
Copy pathlogging.ts
199 lines (164 loc) · 6.8 KB
/
logging.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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import fs = require("fs");
import os = require("os");
import path = require("path");
import vscode = require("vscode");
import utils = require("./utils");
export enum LogLevel {
Diagnostic,
Verbose,
Normal,
Warning,
Error,
None,
}
/** Interface for logging operations. New features should use this interface for the "type" of logger.
* This will allow for easy mocking of the logger during unit tests.
*/
export interface ILogger {
write(message: string, ...additionalMessages: string[]);
writeDiagnostic(message: string, ...additionalMessages: string[]);
writeVerbose(message: string, ...additionalMessages: string[]);
writeWarning(message: string, ...additionalMessages: string[]);
writeAndShowWarning(message: string, ...additionalMessages: string[]);
writeError(message: string, ...additionalMessages: string[]);
}
export class Logger implements ILogger {
public logBasePath: vscode.Uri;
public logSessionPath: vscode.Uri;
public MinimumLogLevel: LogLevel = LogLevel.Normal;
private commands: vscode.Disposable[];
private logChannel: vscode.OutputChannel;
private logFilePath: vscode.Uri;
constructor(logBasePath: vscode.Uri) {
this.logChannel = vscode.window.createOutputChannel("PowerShell Extension Logs");
if (logBasePath === undefined) {
// No workspace, we have to use another folder.
this.logBasePath = vscode.Uri.file(path.resolve(__dirname, "../logs"));
utils.ensurePathExists(this.logBasePath.fsPath);
} else {
this.logBasePath = vscode.Uri.joinPath(logBasePath, "logs");
}
this.commands = [
vscode.commands.registerCommand(
"PowerShell.ShowLogs",
() => { this.showLogPanel(); }),
vscode.commands.registerCommand(
"PowerShell.OpenLogFolder",
() => { this.openLogFolder(); }),
];
}
public dispose() {
this.commands.forEach((command) => { command.dispose(); });
this.logChannel.dispose();
}
public getLogFilePath(baseName: string): vscode.Uri {
return vscode.Uri.joinPath(this.logSessionPath, `${baseName}.log`);
}
public writeAtLevel(logLevel: LogLevel, message: string, ...additionalMessages: string[]) {
if (logLevel >= this.MinimumLogLevel) {
this.writeLine(message, logLevel);
additionalMessages.forEach((line) => {
this.writeLine(line, logLevel);
});
}
}
public write(message: string, ...additionalMessages: string[]) {
this.writeAtLevel(LogLevel.Normal, message, ...additionalMessages);
}
public writeDiagnostic(message: string, ...additionalMessages: string[]) {
this.writeAtLevel(LogLevel.Diagnostic, message, ...additionalMessages);
}
public writeVerbose(message: string, ...additionalMessages: string[]) {
this.writeAtLevel(LogLevel.Verbose, message, ...additionalMessages);
}
public writeWarning(message: string, ...additionalMessages: string[]) {
this.writeAtLevel(LogLevel.Warning, message, ...additionalMessages);
}
public writeAndShowWarning(message: string, ...additionalMessages: string[]) {
this.writeWarning(message, ...additionalMessages);
vscode.window.showWarningMessage(message, "Show Logs").then((selection) => {
if (selection !== undefined) {
this.showLogPanel();
}
});
}
public writeError(message: string, ...additionalMessages: string[]) {
this.writeAtLevel(LogLevel.Error, message, ...additionalMessages);
}
public writeAndShowError(message: string, ...additionalMessages: string[]) {
this.writeError(message, ...additionalMessages);
vscode.window.showErrorMessage(message, "Show Logs").then((selection) => {
if (selection !== undefined) {
this.showLogPanel();
}
});
}
public async writeAndShowErrorWithActions(
message: string,
actions: { prompt: string; action: () => Promise<void> }[]) {
this.writeError(message);
const fullActions = [
...actions,
{ prompt: "Show Logs", action: async () => { this.showLogPanel(); } },
];
const actionKeys: string[] = fullActions.map((action) => action.prompt);
const choice = await vscode.window.showErrorMessage(message, ...actionKeys);
if (choice) {
for (const action of fullActions) {
if (choice === action.prompt) {
await action.action();
return;
}
}
}
}
public async startNewLog(minimumLogLevel: string = "Normal") {
this.MinimumLogLevel = this.logLevelNameToValue(minimumLogLevel.trim());
this.logSessionPath =
vscode.Uri.joinPath(
this.logBasePath,
`${Math.floor(Date.now() / 1000)}-${vscode.env.sessionId}`);
this.logFilePath = this.getLogFilePath("vscode-powershell");
await vscode.workspace.fs.createDirectory(this.logSessionPath);
}
private logLevelNameToValue(logLevelName: string): LogLevel {
switch (logLevelName.toLowerCase()) {
case "diagnostic": return LogLevel.Diagnostic;
case "verbose": return LogLevel.Verbose;
case "normal": return LogLevel.Normal;
case "warning": return LogLevel.Warning;
case "error": return LogLevel.Error;
case "none": return LogLevel.None;
default: return LogLevel.Normal;
}
}
private showLogPanel() {
this.logChannel.show();
}
private openLogFolder() {
if (this.logSessionPath) {
// Open the folder in VS Code since there isn't an easy way to
// open the folder in the platform's file browser
vscode.commands.executeCommand("vscode.openFolder", this.logSessionPath, true);
}
}
private writeLine(message: string, level: LogLevel = LogLevel.Normal) {
const now = new Date();
const timestampedMessage =
`${now.toLocaleDateString()} ${now.toLocaleTimeString()} [${LogLevel[level].toUpperCase()}] - ${message}`;
this.logChannel.appendLine(timestampedMessage);
if (this.logFilePath && this.MinimumLogLevel !== LogLevel.None) {
fs.appendFile(
this.logFilePath.fsPath,
timestampedMessage + os.EOL,
(err) => {
if (err) {
// tslint:disable-next-line:no-console
console.log(`Error writing to vscode-powershell log file: ${err}`);
}
});
}
}
}