Skip to content

Make Logger.WriteLine thread-safe and fix bug with UNC paths #4276

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/features/ExternalApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,9 @@ export class ExternalApiFeature extends LanguageClientConsumer implements IPower
}

public getStorageUri(): vscode.Uri {
return this.extensionContext.globalStorageUri;
// We have to override the scheme because it defaults to
// 'vscode-userdata' which breaks UNC paths.
return this.extensionContext.globalStorageUri.with({ scheme: "file"});
}

public dispose() {
Expand Down
14 changes: 12 additions & 2 deletions src/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,15 @@ export class Logger implements ILogger {
private logChannel: vscode.OutputChannel;
private logFilePath: vscode.Uri;
private logDirectoryCreated = false;
private writingLog = false;

constructor(logLevelName: string, globalStorageUri: vscode.Uri) {
this.logLevel = Logger.logLevelNameToValue(logLevelName);
this.logChannel = vscode.window.createOutputChannel("PowerShell Extension Logs");
// We have to override the scheme because it defaults to
// 'vscode-userdata' which breaks UNC paths.
this.logDirectoryPath = vscode.Uri.joinPath(
globalStorageUri,
globalStorageUri.with({ scheme: "file" }),
"logs",
`${Math.floor(Date.now() / 1000)}-${vscode.env.sessionId}`);
this.logFilePath = this.getLogFilePath("vscode-powershell");
Expand Down Expand Up @@ -194,11 +197,16 @@ export class Logger implements ILogger {
const timestampedMessage = Logger.timestampMessage(message, level);
this.logChannel.appendLine(timestampedMessage);
if (this.logLevel !== LogLevel.None) {
// A simple lock because this function isn't re-entrant.
while (this.writingLog) {
await utils.sleep(300);
}
try {
this.writingLog = true;
if (!this.logDirectoryCreated) {
this.logChannel.appendLine(Logger.timestampMessage(`Creating log directory at: '${this.logDirectoryPath}'`, level));
await vscode.workspace.fs.createDirectory(this.logDirectoryPath);
this.logDirectoryCreated = await utils.checkIfDirectoryExists(this.logDirectoryPath);
this.logDirectoryCreated = true;
}
let log = new Uint8Array();
if (await utils.checkIfFileExists(this.logFilePath)) {
Expand All @@ -209,6 +217,8 @@ export class Logger implements ILogger {
Buffer.concat([log, Buffer.from(timestampedMessage)]));
} catch (e) {
console.log(`Error writing to vscode-powershell log file: ${e}`);
} finally {
this.writingLog = false;
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ export class SessionManager implements Middleware {

// Create the language status item
this.languageStatusItem = this.createStatusBarItem();
this.sessionsFolder = vscode.Uri.joinPath(extensionContext.globalStorageUri, "sessions");
// We have to override the scheme because it defaults to
// 'vscode-userdata' which breaks UNC paths.
this.sessionsFolder = vscode.Uri.joinPath(extensionContext.globalStorageUri.with({ scheme: "file"}), "sessions");
this.platformDetails = getPlatformDetails();
this.HostName = hostName;
this.HostVersion = hostVersion;
Expand Down