Skip to content

Commit 4f2fe1e

Browse files
committed
Use context.storageUri for logs and support None level
1 parent a128c36 commit 4f2fe1e

File tree

5 files changed

+27
-23
lines changed

5 files changed

+27
-23
lines changed

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -798,10 +798,11 @@
798798
"Verbose",
799799
"Normal",
800800
"Warning",
801-
"Error"
801+
"Error",
802+
"None"
802803
],
803804
"default": "Normal",
804-
"description": "Sets the logging verbosity level for the PowerShell Editor Services host executable. Valid values are 'Diagnostic', 'Verbose', 'Normal', 'Warning', and 'Error'"
805+
"description": "Sets the logging verbosity level for the PowerShell Editor Services host executable. Valid values are 'Diagnostic', 'Verbose', 'Normal', 'Warning', 'Error', and 'None'"
805806
},
806807
"powershell.developer.editorServicesWaitForDebugger": {
807808
"type": "boolean",

src/logging.ts

+21-18
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export enum LogLevel {
1313
Normal,
1414
Warning,
1515
Error,
16+
None,
1617
}
1718

1819
/** Interface for logging operations. New features should use this interface for the "type" of logger.
@@ -29,19 +30,24 @@ export interface ILogger {
2930

3031
export class Logger implements ILogger {
3132

32-
public logBasePath: string;
33-
public logSessionPath: string;
33+
public logBasePath: vscode.Uri;
34+
public logSessionPath: vscode.Uri;
3435
public MinimumLogLevel: LogLevel = LogLevel.Normal;
3536

3637
private commands: vscode.Disposable[];
3738
private logChannel: vscode.OutputChannel;
38-
private logFilePath: string;
39+
private logFilePath: vscode.Uri;
3940

40-
constructor() {
41+
constructor(logBasePath: vscode.Uri) {
4142
this.logChannel = vscode.window.createOutputChannel("PowerShell Extension Logs");
4243

43-
this.logBasePath = path.resolve(__dirname, "../logs");
44-
utils.ensurePathExists(this.logBasePath);
44+
if (logBasePath === undefined) {
45+
// No workspace, we have to use another folder.
46+
this.logBasePath = vscode.Uri.file(path.resolve(__dirname, "../logs"));
47+
utils.ensurePathExists(this.logBasePath.fsPath);
48+
} else {
49+
this.logBasePath = vscode.Uri.joinPath(logBasePath, "logs");
50+
}
4551

4652
this.commands = [
4753
vscode.commands.registerCommand(
@@ -59,8 +65,8 @@ export class Logger implements ILogger {
5965
this.logChannel.dispose();
6066
}
6167

62-
public getLogFilePath(baseName: string): string {
63-
return path.resolve(this.logSessionPath, `${baseName}.log`);
68+
public getLogFilePath(baseName: string): vscode.Uri {
69+
return vscode.Uri.joinPath(this.logSessionPath, `${baseName}.log`);
6470
}
6571

6672
public writeAtLevel(logLevel: LogLevel, message: string, ...additionalMessages: string[]) {
@@ -136,17 +142,16 @@ export class Logger implements ILogger {
136142
}
137143
}
138144

139-
public startNewLog(minimumLogLevel: string = "Normal") {
145+
public async startNewLog(minimumLogLevel: string = "Normal") {
140146
this.MinimumLogLevel = this.logLevelNameToValue(minimumLogLevel.trim());
141147

142148
this.logSessionPath =
143-
path.resolve(
149+
vscode.Uri.joinPath(
144150
this.logBasePath,
145151
`${Math.floor(Date.now() / 1000)}-${vscode.env.sessionId}`);
146152

147153
this.logFilePath = this.getLogFilePath("vscode-powershell");
148-
149-
utils.ensurePathExists(this.logSessionPath);
154+
await vscode.workspace.fs.createDirectory(this.logSessionPath);
150155
}
151156

152157
private logLevelNameToValue(logLevelName: string): LogLevel {
@@ -156,6 +161,7 @@ export class Logger implements ILogger {
156161
case "normal": return LogLevel.Normal;
157162
case "warning": return LogLevel.Warning;
158163
case "error": return LogLevel.Error;
164+
case "none": return LogLevel.None;
159165
default: return LogLevel.Normal;
160166
}
161167
}
@@ -168,10 +174,7 @@ export class Logger implements ILogger {
168174
if (this.logSessionPath) {
169175
// Open the folder in VS Code since there isn't an easy way to
170176
// open the folder in the platform's file browser
171-
vscode.commands.executeCommand(
172-
"vscode.openFolder",
173-
vscode.Uri.file(this.logSessionPath),
174-
true);
177+
vscode.commands.executeCommand("vscode.openFolder", this.logSessionPath, true);
175178
}
176179
}
177180

@@ -181,9 +184,9 @@ export class Logger implements ILogger {
181184
`${now.toLocaleDateString()} ${now.toLocaleTimeString()} [${LogLevel[level].toUpperCase()}] - ${message}`;
182185

183186
this.logChannel.appendLine(timestampedMessage);
184-
if (this.logFilePath) {
187+
if (this.logFilePath && this.MinimumLogLevel !== LogLevel.None) {
185188
fs.appendFile(
186-
this.logFilePath,
189+
this.logFilePath.fsPath,
187190
timestampedMessage + os.EOL,
188191
(err) => {
189192
if (err) {

src/main.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<IPower
125125
});
126126

127127
// Setup the logger.
128-
logger = new Logger();
128+
logger = new Logger(context.storageUri);
129129
logger.MinimumLogLevel = LogLevel[extensionSettings.developer.editorServicesLogLevel];
130130

131131
sessionManager =

src/process.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export class PowerShellProcess {
5151
: "";
5252

5353
this.startPsesArgs +=
54-
`-LogPath '${PowerShellProcess.escapeSingleQuotes(editorServicesLogPath)}' ` +
54+
`-LogPath '${PowerShellProcess.escapeSingleQuotes(editorServicesLogPath.fsPath)}' ` +
5555
`-SessionDetailsPath '${PowerShellProcess.escapeSingleQuotes(this.sessionFilePath)}' ` +
5656
`-FeatureFlags @(${featureFlags}) `;
5757

src/session.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ export class SessionManager implements Middleware {
111111
this.sessionSettings.powerShellDefaultVersion = exeNameOverride;
112112
}
113113

114-
this.log.startNewLog(this.sessionSettings.developer.editorServicesLogLevel);
114+
await this.log.startNewLog(this.sessionSettings.developer.editorServicesLogLevel);
115115

116116
// Create the PowerShell executable finder now
117117
this.powershellExeFinder = new PowerShellExeFinder(

0 commit comments

Comments
 (0)