@@ -27,17 +27,28 @@ export interface ILogger {
2727}
2828
2929export 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 ) ;
0 commit comments