@@ -5,6 +5,7 @@ import utils = require("./utils");
55import os = require( "os" ) ;
66import vscode = require( "vscode" ) ;
77
8+ // NOTE: This is not a string enum because the order is used for comparison.
89export enum LogLevel {
910 Diagnostic ,
1011 Verbose ,
@@ -27,17 +28,29 @@ export interface ILogger {
2728}
2829
2930export class Logger implements ILogger {
30- public logBasePath : vscode . Uri ;
31- public logSessionPath : vscode . Uri | undefined ;
32- public MinimumLogLevel : LogLevel = LogLevel . Normal ;
31+ public logDirectoryPath : vscode . Uri ;
3332
33+ private logLevel : LogLevel ;
3434 private commands : vscode . Disposable [ ] ;
3535 private logChannel : vscode . OutputChannel ;
36- private logFilePath : vscode . Uri | undefined ;
36+ private logFilePath : vscode . Uri ;
37+ private logDirectoryCreated = false ;
3738
38- constructor ( logBasePath : vscode . Uri ) {
39+ constructor ( logLevelName : string , globalStorageUri : vscode . Uri ) {
40+ this . logLevel = Logger . logLevelNameToValue ( logLevelName ) ;
3941 this . logChannel = vscode . window . createOutputChannel ( "PowerShell Extension Logs" ) ;
40- this . logBasePath = vscode . Uri . joinPath ( logBasePath , "logs" ) ;
42+ this . logDirectoryPath = vscode . Uri . joinPath (
43+ globalStorageUri ,
44+ "logs" ,
45+ `${ Math . floor ( Date . now ( ) / 1000 ) } -${ vscode . env . sessionId } ` ) ;
46+ this . logFilePath = this . getLogFilePath ( "vscode-powershell" ) ;
47+
48+ // Early logging of the log paths for debugging.
49+ if ( LogLevel . Diagnostic >= this . logLevel ) {
50+ const uriMessage = Logger . timestampMessage ( `Global storage URI: '${ globalStorageUri } ', log file path: '${ this . logFilePath } '` , LogLevel . Diagnostic ) ;
51+ this . logChannel . appendLine ( uriMessage ) ;
52+ }
53+
4154 this . commands = [
4255 vscode . commands . registerCommand (
4356 "PowerShell.ShowLogs" ,
@@ -57,11 +70,11 @@ export class Logger implements ILogger {
5770 }
5871
5972 public getLogFilePath ( baseName : string ) : vscode . Uri {
60- return vscode . Uri . joinPath ( this . logSessionPath ! , `${ baseName } .log` ) ;
73+ return vscode . Uri . joinPath ( this . logDirectoryPath , `${ baseName } .log` ) ;
6174 }
6275
6376 private writeAtLevel ( logLevel : LogLevel , message : string , ...additionalMessages : string [ ] ) : void {
64- if ( logLevel >= this . MinimumLogLevel ) {
77+ if ( logLevel >= this . logLevel ) {
6578 void this . writeLine ( message , logLevel ) ;
6679
6780 for ( const additionalMessage of additionalMessages ) {
@@ -140,20 +153,8 @@ export class Logger implements ILogger {
140153 }
141154 }
142155
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-
155156 // TODO: Make the enum smarter about strings so this goes away.
156- public static logLevelNameToValue ( logLevelName : string ) : LogLevel {
157+ private static logLevelNameToValue ( logLevelName : string ) : LogLevel {
157158 switch ( logLevelName . trim ( ) . toLowerCase ( ) ) {
158159 case "diagnostic" : return LogLevel . Diagnostic ;
159160 case "verbose" : return LogLevel . Verbose ;
@@ -165,27 +166,40 @@ export class Logger implements ILogger {
165166 }
166167 }
167168
169+ public updateLogLevel ( logLevelName : string ) : void {
170+ this . logLevel = Logger . logLevelNameToValue ( logLevelName ) ;
171+ }
172+
168173 private showLogPanel ( ) : void {
169174 this . logChannel . show ( ) ;
170175 }
171176
172177 private async openLogFolder ( ) : Promise < void > {
173- if ( this . logSessionPath ) {
178+ if ( this . logDirectoryCreated ) {
174179 // Open the folder in VS Code since there isn't an easy way to
175180 // open the folder in the platform's file browser
176- await vscode . commands . executeCommand ( "vscode.openFolder" , this . logSessionPath , true ) ;
181+ await vscode . commands . executeCommand ( "vscode.openFolder" , this . logDirectoryPath , true ) ;
182+ } else {
183+ void this . writeAndShowError ( "Cannot open PowerShell log directory as it does not exist!" ) ;
177184 }
178185 }
179186
180- // TODO: Should we await this function above?
181- private async writeLine ( message : string , level : LogLevel = LogLevel . Normal ) : Promise < void > {
187+ private static timestampMessage ( message : string , level : LogLevel ) : string {
182188 const now = new Date ( ) ;
183- const timestampedMessage =
184- ` ${ now . toLocaleDateString ( ) } ${ now . toLocaleTimeString ( ) } [ ${ LogLevel [ level ] . toUpperCase ( ) } ] - ${ message } ${ os . EOL } ` ;
189+ return ` ${ now . toLocaleDateString ( ) } ${ now . toLocaleTimeString ( ) } [ ${ LogLevel [ level ] . toUpperCase ( ) } ] - ${ message } ${ os . EOL } ` ;
190+ }
185191
192+ // TODO: Should we await this function above?
193+ private async writeLine ( message : string , level : LogLevel = LogLevel . Normal ) : Promise < void > {
194+ const timestampedMessage = Logger . timestampMessage ( message , level ) ;
186195 this . logChannel . appendLine ( timestampedMessage ) ;
187- if ( this . logFilePath && this . MinimumLogLevel !== LogLevel . None ) {
196+ if ( this . logLevel !== LogLevel . None ) {
188197 try {
198+ if ( ! this . logDirectoryCreated ) {
199+ this . logChannel . appendLine ( Logger . timestampMessage ( `Creating log directory at: '${ this . logDirectoryPath } '` , level ) ) ;
200+ await vscode . workspace . fs . createDirectory ( this . logDirectoryPath ) ;
201+ this . logDirectoryCreated = await utils . checkIfDirectoryExists ( this . logDirectoryPath ) ;
202+ }
189203 let log = new Uint8Array ( ) ;
190204 if ( await utils . checkIfFileExists ( this . logFilePath ) ) {
191205 log = await vscode . workspace . fs . readFile ( this . logFilePath ) ;
0 commit comments