@@ -13,6 +13,7 @@ export enum LogLevel {
13
13
Normal ,
14
14
Warning ,
15
15
Error ,
16
+ None ,
16
17
}
17
18
18
19
/** Interface for logging operations. New features should use this interface for the "type" of logger.
@@ -29,19 +30,24 @@ export interface ILogger {
29
30
30
31
export class Logger implements ILogger {
31
32
32
- public logBasePath : string ;
33
- public logSessionPath : string ;
33
+ public logBasePath : vscode . Uri ;
34
+ public logSessionPath : vscode . Uri ;
34
35
public MinimumLogLevel : LogLevel = LogLevel . Normal ;
35
36
36
37
private commands : vscode . Disposable [ ] ;
37
38
private logChannel : vscode . OutputChannel ;
38
- private logFilePath : string ;
39
+ private logFilePath : vscode . Uri ;
39
40
40
- constructor ( ) {
41
+ constructor ( logBasePath : vscode . Uri ) {
41
42
this . logChannel = vscode . window . createOutputChannel ( "PowerShell Extension Logs" ) ;
42
43
43
- this . logBasePath = path . resolve ( __dirname , "../logs" ) ;
44
- utils . ensurePathExists ( this . logBasePath ) ;
44
+ if ( logBasePath === undefined ) {
45
+ // No workspace, we have to use another folder.
46
+ this . logBasePath = vscode . Uri . file ( path . resolve ( __dirname , "../logs" ) ) ;
47
+ utils . ensurePathExists ( this . logBasePath . fsPath ) ;
48
+ } else {
49
+ this . logBasePath = vscode . Uri . joinPath ( logBasePath , "logs" ) ;
50
+ }
45
51
46
52
this . commands = [
47
53
vscode . commands . registerCommand (
@@ -59,8 +65,8 @@ export class Logger implements ILogger {
59
65
this . logChannel . dispose ( ) ;
60
66
}
61
67
62
- public getLogFilePath ( baseName : string ) : string {
63
- return path . resolve ( this . logSessionPath , `${ baseName } .log` ) ;
68
+ public getLogFilePath ( baseName : string ) : vscode . Uri {
69
+ return vscode . Uri . joinPath ( this . logSessionPath , `${ baseName } .log` ) ;
64
70
}
65
71
66
72
public writeAtLevel ( logLevel : LogLevel , message : string , ...additionalMessages : string [ ] ) {
@@ -136,17 +142,16 @@ export class Logger implements ILogger {
136
142
}
137
143
}
138
144
139
- public startNewLog ( minimumLogLevel : string = "Normal" ) {
145
+ public async startNewLog ( minimumLogLevel : string = "Normal" ) {
140
146
this . MinimumLogLevel = this . logLevelNameToValue ( minimumLogLevel . trim ( ) ) ;
141
147
142
148
this . logSessionPath =
143
- path . resolve (
149
+ vscode . Uri . joinPath (
144
150
this . logBasePath ,
145
151
`${ Math . floor ( Date . now ( ) / 1000 ) } -${ vscode . env . sessionId } ` ) ;
146
152
147
153
this . logFilePath = this . getLogFilePath ( "vscode-powershell" ) ;
148
-
149
- utils . ensurePathExists ( this . logSessionPath ) ;
154
+ await vscode . workspace . fs . createDirectory ( this . logSessionPath ) ;
150
155
}
151
156
152
157
private logLevelNameToValue ( logLevelName : string ) : LogLevel {
@@ -156,6 +161,7 @@ export class Logger implements ILogger {
156
161
case "normal" : return LogLevel . Normal ;
157
162
case "warning" : return LogLevel . Warning ;
158
163
case "error" : return LogLevel . Error ;
164
+ case "none" : return LogLevel . None ;
159
165
default : return LogLevel . Normal ;
160
166
}
161
167
}
@@ -168,10 +174,7 @@ export class Logger implements ILogger {
168
174
if ( this . logSessionPath ) {
169
175
// Open the folder in VS Code since there isn't an easy way to
170
176
// open the folder in the platform's file browser
171
- vscode . commands . executeCommand (
172
- "vscode.openFolder" ,
173
- vscode . Uri . file ( this . logSessionPath ) ,
174
- true ) ;
177
+ vscode . commands . executeCommand ( "vscode.openFolder" , this . logSessionPath , true ) ;
175
178
}
176
179
}
177
180
@@ -181,9 +184,9 @@ export class Logger implements ILogger {
181
184
`${ now . toLocaleDateString ( ) } ${ now . toLocaleTimeString ( ) } [${ LogLevel [ level ] . toUpperCase ( ) } ] - ${ message } ` ;
182
185
183
186
this . logChannel . appendLine ( timestampedMessage ) ;
184
- if ( this . logFilePath ) {
187
+ if ( this . logFilePath && this . MinimumLogLevel !== LogLevel . None ) {
185
188
fs . appendFile (
186
- this . logFilePath ,
189
+ this . logFilePath . fsPath ,
187
190
timestampedMessage + os . EOL ,
188
191
( err ) => {
189
192
if ( err ) {
0 commit comments