@@ -27,17 +27,28 @@ export interface ILogger {
27
27
}
28
28
29
29
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 ;
33
31
32
+ private logLevel : LogLevel = LogLevel . Normal ;
34
33
private commands : vscode . Disposable [ ] ;
35
34
private logChannel : vscode . OutputChannel ;
36
- private logFilePath : vscode . Uri | undefined ;
35
+ private logFilePath : vscode . Uri ;
36
+ private logDirectoryCreated = false ;
37
37
38
- constructor ( logBasePath : vscode . Uri ) {
38
+ constructor ( globalStorageUri : vscode . Uri ) {
39
39
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
+
41
52
this . commands = [
42
53
vscode . commands . registerCommand (
43
54
"PowerShell.ShowLogs" ,
@@ -57,11 +68,11 @@ export class Logger implements ILogger {
57
68
}
58
69
59
70
public getLogFilePath ( baseName : string ) : vscode . Uri {
60
- return vscode . Uri . joinPath ( this . logSessionPath ! , `${ baseName } .log` ) ;
71
+ return vscode . Uri . joinPath ( this . logDirectoryPath , `${ baseName } .log` ) ;
61
72
}
62
73
63
74
private writeAtLevel ( logLevel : LogLevel , message : string , ...additionalMessages : string [ ] ) : void {
64
- if ( logLevel >= this . MinimumLogLevel ) {
75
+ if ( logLevel >= this . logLevel ) {
65
76
void this . writeLine ( message , logLevel ) ;
66
77
67
78
for ( const additionalMessage of additionalMessages ) {
@@ -140,20 +151,8 @@ export class Logger implements ILogger {
140
151
}
141
152
}
142
153
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
-
155
154
// 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 {
157
156
switch ( logLevelName . trim ( ) . toLowerCase ( ) ) {
158
157
case "diagnostic" : return LogLevel . Diagnostic ;
159
158
case "verbose" : return LogLevel . Verbose ;
@@ -165,27 +164,39 @@ export class Logger implements ILogger {
165
164
}
166
165
}
167
166
167
+ public updateLogLevel ( logLevelName : string ) : void {
168
+ this . logLevel = Logger . logLevelNameToValue ( logLevelName ) ;
169
+ }
170
+
168
171
private showLogPanel ( ) : void {
169
172
this . logChannel . show ( ) ;
170
173
}
171
174
172
175
private async openLogFolder ( ) : Promise < void > {
173
- if ( this . logSessionPath ) {
176
+ if ( this . logDirectoryCreated ) {
174
177
// Open the folder in VS Code since there isn't an easy way to
175
178
// 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!" ) ;
177
182
}
178
183
}
179
184
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 {
182
186
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
+ }
185
189
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 ) ;
186
193
this . logChannel . appendLine ( timestampedMessage ) ;
187
- if ( this . logFilePath && this . MinimumLogLevel !== LogLevel . None ) {
194
+ if ( this . logLevel !== LogLevel . None ) {
188
195
try {
196
+ if ( ! this . logDirectoryCreated ) {
197
+ await vscode . workspace . fs . createDirectory ( this . logDirectoryPath ) ;
198
+ this . logDirectoryCreated = await utils . checkIfDirectoryExists ( this . logDirectoryPath ) ;
199
+ }
189
200
let log = new Uint8Array ( ) ;
190
201
if ( await utils . checkIfFileExists ( this . logFilePath ) ) {
191
202
log = await vscode . workspace . fs . readFile ( this . logFilePath ) ;
0 commit comments