-
Notifications
You must be signed in to change notification settings - Fork 510
/
Copy pathlogging.ts
167 lines (132 loc) · 4.96 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
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/
import fs = require('fs');
import os = require('os');
import path = require('path');
import vscode = require('vscode');
import utils = require('./utils');
import jsonrpc = require('vscode-jsonrpc');
export enum LogLevel {
Verbose,
Normal,
Warning,
Error
}
export class Logger {
private commands: vscode.Disposable[];
private logChannel: vscode.OutputChannel;
private logFilePath: string;
public logBasePath: string;
public logSessionPath: string;
public MinimumLogLevel: LogLevel = LogLevel.Normal;
constructor() {
this.logChannel = vscode.window.createOutputChannel("PowerShell Extension Logs");
this.logBasePath = path.resolve(__dirname, "../logs");
utils.ensurePathExists(this.logBasePath);
this.commands = [
vscode.commands.registerCommand(
'PowerShell.ShowLogs',
() => { this.showLogPanel(); }),
vscode.commands.registerCommand(
'PowerShell.OpenLogFolder',
() => { this.openLogFolder(); })
]
}
public getLogFilePath(baseName: string): string {
return path.resolve(this.logSessionPath, `${baseName}.log`);
}
public writeAtLevel(logLevel: LogLevel, message: string, ...additionalMessages: string[]) {
if (logLevel >= this.MinimumLogLevel) {
this.writeLine(message)
additionalMessages.forEach((line) => {
this.writeLine(message);
});
}
}
public write(message: string, ...additionalMessages: string[]) {
this.writeAtLevel(LogLevel.Normal, 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 startNewLog(minimumLogLevel: string = "Normal") {
this.MinimumLogLevel = this.logLevelNameToValue(minimumLogLevel.trim());
this.logSessionPath =
path.resolve(
this.logBasePath,
`${Math.floor(Date.now() / 1000)}-${vscode.env.sessionId}`);
this.logFilePath = this.getLogFilePath("vscode-powershell");
utils.ensurePathExists(this.logSessionPath);
}
private logLevelNameToValue(logLevelName: string): LogLevel {
switch (logLevelName.toLowerCase()) {
case "normal": return LogLevel.Normal;
case "verbose": return LogLevel.Verbose;
case "warning": return LogLevel.Warning;
case "error": return LogLevel.Error;
default: return LogLevel.Normal;
}
}
public dispose() {
this.commands.forEach((command) => { command.dispose() });
this.logChannel.dispose();
}
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',
vscode.Uri.file(this.logSessionPath),
true);
}
}
private writeLine(message: string) {
// TODO: Add timestamp
this.logChannel.appendLine(message);
if (this.logFilePath) {
fs.appendFile(this.logFilePath, message + os.EOL);
}
}
}
export class LanguageClientLogger implements jsonrpc.Logger {
constructor(private logger: Logger) { }
public error(message: string) {
this.logger.writeError(message);
}
public warn(message: string) {
this.logger.writeWarning(message);
}
public info(message: string) {
this.logger.write(message);
}
public log(message: string) {
this.logger.writeVerbose(message);
}
}