Skip to content

Use context.storageUri for logs and support None level #4071

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
Jul 12, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -798,10 +798,11 @@
"Verbose",
"Normal",
"Warning",
"Error"
"Error",
"None"
],
"default": "Normal",
"description": "Sets the logging verbosity level for the PowerShell Editor Services host executable. Valid values are 'Diagnostic', 'Verbose', 'Normal', 'Warning', and 'Error'"
"description": "Sets the logging verbosity level for the PowerShell Editor Services host executable. Valid values are 'Diagnostic', 'Verbose', 'Normal', 'Warning', 'Error', and 'None'"
},
"powershell.developer.editorServicesWaitForDebugger": {
"type": "boolean",
Expand Down
39 changes: 21 additions & 18 deletions src/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export enum LogLevel {
Normal,
Warning,
Error,
None,
}

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

export class Logger implements ILogger {

public logBasePath: string;
public logSessionPath: string;
public logBasePath: vscode.Uri;
public logSessionPath: vscode.Uri;
public MinimumLogLevel: LogLevel = LogLevel.Normal;

private commands: vscode.Disposable[];
private logChannel: vscode.OutputChannel;
private logFilePath: string;
private logFilePath: vscode.Uri;

constructor() {
constructor(logBasePath: vscode.Uri) {
this.logChannel = vscode.window.createOutputChannel("PowerShell Extension Logs");

this.logBasePath = path.resolve(__dirname, "../logs");
utils.ensurePathExists(this.logBasePath);
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(
Expand All @@ -59,8 +65,8 @@ export class Logger implements ILogger {
this.logChannel.dispose();
}

public getLogFilePath(baseName: string): string {
return path.resolve(this.logSessionPath, `${baseName}.log`);
public getLogFilePath(baseName: string): vscode.Uri {
return vscode.Uri.joinPath(this.logSessionPath, `${baseName}.log`);
}

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

public startNewLog(minimumLogLevel: string = "Normal") {
public async startNewLog(minimumLogLevel: string = "Normal") {
this.MinimumLogLevel = this.logLevelNameToValue(minimumLogLevel.trim());

this.logSessionPath =
path.resolve(
vscode.Uri.joinPath(
this.logBasePath,
`${Math.floor(Date.now() / 1000)}-${vscode.env.sessionId}`);

this.logFilePath = this.getLogFilePath("vscode-powershell");

utils.ensurePathExists(this.logSessionPath);
await vscode.workspace.fs.createDirectory(this.logSessionPath);
}

private logLevelNameToValue(logLevelName: string): LogLevel {
Expand All @@ -156,6 +161,7 @@ export class Logger implements ILogger {
case "normal": return LogLevel.Normal;
case "warning": return LogLevel.Warning;
case "error": return LogLevel.Error;
case "none": return LogLevel.None;
default: return LogLevel.Normal;
}
}
Expand All @@ -168,10 +174,7 @@ export class Logger implements ILogger {
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);
vscode.commands.executeCommand("vscode.openFolder", this.logSessionPath, true);
}
}

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

this.logChannel.appendLine(timestampedMessage);
if (this.logFilePath) {
if (this.logFilePath && this.MinimumLogLevel !== LogLevel.None) {
fs.appendFile(
this.logFilePath,
this.logFilePath.fsPath,
timestampedMessage + os.EOL,
(err) => {
if (err) {
Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<IPower
});

// Setup the logger.
logger = new Logger();
logger = new Logger(context.storageUri);
logger.MinimumLogLevel = LogLevel[extensionSettings.developer.editorServicesLogLevel];

sessionManager =
Expand Down
2 changes: 1 addition & 1 deletion src/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class PowerShellProcess {
: "";

this.startPsesArgs +=
`-LogPath '${PowerShellProcess.escapeSingleQuotes(editorServicesLogPath)}' ` +
`-LogPath '${PowerShellProcess.escapeSingleQuotes(editorServicesLogPath.fsPath)}' ` +
`-SessionDetailsPath '${PowerShellProcess.escapeSingleQuotes(this.sessionFilePath)}' ` +
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might want to move sessionFilePath into context.storageUri too. I also need to check if we spawn an instance per workspace or not.

`-FeatureFlags @(${featureFlags}) `;

Expand Down
2 changes: 1 addition & 1 deletion src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export class SessionManager implements Middleware {
this.sessionSettings.powerShellDefaultVersion = exeNameOverride;
}

this.log.startNewLog(this.sessionSettings.developer.editorServicesLogLevel);
await this.log.startNewLog(this.sessionSettings.developer.editorServicesLogLevel);

// Create the PowerShell executable finder now
this.powershellExeFinder = new PowerShellExeFinder(
Expand Down