Skip to content

Commit e120519

Browse files
committed
Capture more logs
By starting the logger earlier.
1 parent b07c87d commit e120519

File tree

3 files changed

+44
-32
lines changed

3 files changed

+44
-32
lines changed

src/logging.ts

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,28 @@ export interface ILogger {
2727
}
2828

2929
export class Logger implements ILogger {
30-
public logBasePath: vscode.Uri;
31-
public logSessionPath: vscode.Uri | undefined;
32-
public MinimumLogLevel: LogLevel = LogLevel.Normal;
30+
public logDirectoryPath: vscode.Uri;
3331

32+
private logLevel: LogLevel = LogLevel.Normal;
3433
private commands: vscode.Disposable[];
3534
private logChannel: vscode.OutputChannel;
36-
private logFilePath: vscode.Uri | undefined;
35+
private logFilePath: vscode.Uri;
36+
private logDirectoryCreated = false;
3737

38-
constructor(logBasePath: vscode.Uri) {
38+
constructor(globalStorageUri: vscode.Uri) {
3939
this.logChannel = vscode.window.createOutputChannel("PowerShell Extension Logs");
40-
this.logBasePath = vscode.Uri.joinPath(logBasePath, "logs");
40+
this.logDirectoryPath = vscode.Uri.joinPath(
41+
globalStorageUri,
42+
"logs",
43+
`${Math.floor(Date.now() / 1000)}-${vscode.env.sessionId}`);
44+
this.logFilePath = this.getLogFilePath("vscode-powershell");
45+
46+
// Early logging of the log paths for debugging.
47+
if (LogLevel.Diagnostic >= this.logLevel) {
48+
const uriMessage = Logger.timestampMessage(`Global storage URI: '${globalStorageUri}', log directory path: '${this.logDirectoryPath}', log file path: '${this.logFilePath}'`, LogLevel.Diagnostic);
49+
this.logChannel.appendLine(uriMessage);
50+
}
51+
4152
this.commands = [
4253
vscode.commands.registerCommand(
4354
"PowerShell.ShowLogs",
@@ -57,11 +68,11 @@ export class Logger implements ILogger {
5768
}
5869

5970
public getLogFilePath(baseName: string): vscode.Uri {
60-
return vscode.Uri.joinPath(this.logSessionPath!, `${baseName}.log`);
71+
return vscode.Uri.joinPath(this.logDirectoryPath, `${baseName}.log`);
6172
}
6273

6374
private writeAtLevel(logLevel: LogLevel, message: string, ...additionalMessages: string[]): void {
64-
if (logLevel >= this.MinimumLogLevel) {
75+
if (logLevel >= this.logLevel) {
6576
void this.writeLine(message, logLevel);
6677

6778
for (const additionalMessage of additionalMessages) {
@@ -140,20 +151,8 @@ export class Logger implements ILogger {
140151
}
141152
}
142153

143-
public async startNewLog(minimumLogLevel = "Normal"): Promise<void> {
144-
this.MinimumLogLevel = Logger.logLevelNameToValue(minimumLogLevel);
145-
146-
this.logSessionPath =
147-
vscode.Uri.joinPath(
148-
this.logBasePath,
149-
`${Math.floor(Date.now() / 1000)}-${vscode.env.sessionId}`);
150-
151-
this.logFilePath = this.getLogFilePath("vscode-powershell");
152-
await vscode.workspace.fs.createDirectory(this.logSessionPath);
153-
}
154-
155154
// TODO: Make the enum smarter about strings so this goes away.
156-
public static logLevelNameToValue(logLevelName: string): LogLevel {
155+
private static logLevelNameToValue(logLevelName: string): LogLevel {
157156
switch (logLevelName.trim().toLowerCase()) {
158157
case "diagnostic": return LogLevel.Diagnostic;
159158
case "verbose": return LogLevel.Verbose;
@@ -165,27 +164,39 @@ export class Logger implements ILogger {
165164
}
166165
}
167166

167+
public updateLogLevel(logLevelName: string): void {
168+
this.logLevel = Logger.logLevelNameToValue(logLevelName);
169+
}
170+
168171
private showLogPanel(): void {
169172
this.logChannel.show();
170173
}
171174

172175
private async openLogFolder(): Promise<void> {
173-
if (this.logSessionPath) {
176+
if (this.logDirectoryCreated) {
174177
// Open the folder in VS Code since there isn't an easy way to
175178
// open the folder in the platform's file browser
176-
await vscode.commands.executeCommand("vscode.openFolder", this.logSessionPath, true);
179+
await vscode.commands.executeCommand("vscode.openFolder", this.logDirectoryPath, true);
180+
} else {
181+
void this.writeAndShowError("Cannot open PowerShell log directory as it does not exist!");
177182
}
178183
}
179184

180-
// TODO: Should we await this function above?
181-
private async writeLine(message: string, level: LogLevel = LogLevel.Normal): Promise<void> {
185+
private static timestampMessage(message: string, level: LogLevel): string {
182186
const now = new Date();
183-
const timestampedMessage =
184-
`${now.toLocaleDateString()} ${now.toLocaleTimeString()} [${LogLevel[level].toUpperCase()}] - ${message}${os.EOL}`;
187+
return `${now.toLocaleDateString()} ${now.toLocaleTimeString()} [${LogLevel[level].toUpperCase()}] - ${message}${os.EOL}`;
188+
}
185189

190+
// TODO: Should we await this function above?
191+
private async writeLine(message: string, level: LogLevel = LogLevel.Normal): Promise<void> {
192+
const timestampedMessage = Logger.timestampMessage(message, level);
186193
this.logChannel.appendLine(timestampedMessage);
187-
if (this.logFilePath && this.MinimumLogLevel !== LogLevel.None) {
194+
if (this.logLevel !== LogLevel.None) {
188195
try {
196+
if (!this.logDirectoryCreated) {
197+
await vscode.workspace.fs.createDirectory(this.logDirectoryPath);
198+
this.logDirectoryCreated = await utils.checkIfDirectoryExists(this.logDirectoryPath);
199+
}
189200
let log = new Uint8Array();
190201
if (await utils.checkIfFileExists(this.logFilePath)) {
191202
log = await vscode.workspace.fs.readFile(this.logFilePath);

src/main.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<IPower
5555
// as the `cwd` hasn't been validated!
5656
const earlySettings = Settings.load();
5757
logger = new Logger(context.globalStorageUri);
58-
logger.MinimumLogLevel = Logger.logLevelNameToValue(earlySettings.developer.editorServicesLogLevel);
58+
logger.updateLogLevel(earlySettings.developer.editorServicesLogLevel);
5959

6060
telemetryReporter = new TelemetryReporter(PackageJSON.name, PackageJSON.version, AI_KEY);
6161

@@ -70,6 +70,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<IPower
7070
// Load and validate settings (will prompt for 'cwd' if necessary).
7171
await Settings.validateCwdSetting(logger);
7272
const settings = Settings.load();
73+
logger.writeDiagnostic(`Loaded settings:\n${JSON.stringify(settings, undefined, 2)}`);
7374

7475
languageConfigurationDisposable = vscode.languages.setLanguageConfiguration(
7576
PowerShellLanguageId,

src/session.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,6 @@ export class SessionManager implements Middleware {
163163
}
164164
// Create a folder for the session files.
165165
await vscode.workspace.fs.createDirectory(this.sessionsFolder);
166-
await this.logger.startNewLog(this.sessionSettings.developer.editorServicesLogLevel);
167166
await this.promptPowerShellExeSettingsCleanup();
168167
await this.migrateWhitespaceAroundPipeSetting();
169168
this.PowerShellExeDetails = await this.findPowerShell();
@@ -386,6 +385,7 @@ export class SessionManager implements Middleware {
386385

387386
private async onConfigurationUpdated() {
388387
const settings = Settings.load();
388+
this.logger.updateLogLevel(settings.developer.editorServicesLogLevel);
389389

390390
// Detect any setting changes that would affect the session
391391
if (!this.suppressRestartPrompt &&
@@ -534,8 +534,8 @@ export class SessionManager implements Middleware {
534534
if (await utils.checkIfDirectoryExists(path.join(devBundledModulesPath, "PowerShellEditorServices/bin"))) {
535535
bundledModulesPath = devBundledModulesPath;
536536
} else {
537-
this.logger.write(
538-
"\nWARNING: In development mode but PowerShellEditorServices dev module path cannot be " +
537+
void this.logger.writeAndShowWarning(
538+
"In development mode but PowerShellEditorServices dev module path cannot be " +
539539
`found (or has not been built yet): ${devBundledModulesPath}\n`);
540540
}
541541
}

0 commit comments

Comments
 (0)