1
1
// Copyright (c) Microsoft Corporation.
2
2
// Licensed under the MIT License.
3
3
4
- import os = require( "os" ) ;
5
- import { sleep , checkIfFileExists } from "./utils" ;
6
- import { LogOutputChannel , Uri , Disposable , LogLevel , window , env , commands , workspace } from "vscode" ;
4
+ import { LogOutputChannel , Uri , Disposable , LogLevel , window , commands } from "vscode" ;
7
5
8
6
/** Interface for logging operations. New features should use this interface for the "type" of logger.
9
7
* This will allow for easy mocking of the logger during unit tests.
@@ -28,23 +26,16 @@ export class Logger implements ILogger {
28
26
private commands : Disposable [ ] ;
29
27
// Log output channel handles all the verbosity management so we don't have to.
30
28
private logChannel : LogOutputChannel ;
31
- private logFilePath : Uri ; // The client's logs
32
- private logDirectoryCreated = false ;
33
- private writingLog = false ;
34
29
public get logLevel ( ) : LogLevel { return this . logChannel . logLevel ; }
35
30
36
- constructor ( globalStorageUri : Uri , logChannel ?: LogOutputChannel ) {
31
+ constructor ( logPath : Uri , logChannel ?: LogOutputChannel ) {
37
32
this . logChannel = logChannel ?? window . createOutputChannel ( "PowerShell" , { log : true } ) ;
38
33
// We have to override the scheme because it defaults to
39
34
// 'vscode-userdata' which breaks UNC paths.
40
- this . logDirectoryPath = Uri . joinPath (
41
- globalStorageUri . with ( { scheme : "file" } ) ,
42
- "logs" ,
43
- `${ Math . floor ( Date . now ( ) / 1000 ) } -${ env . sessionId } ` ) ;
44
- this . logFilePath = Uri . joinPath ( this . logDirectoryPath , "vscode-powershell.log" ) ;
35
+ this . logDirectoryPath = logPath ;
45
36
46
37
// Early logging of the log paths for debugging.
47
- if ( this . logLevel >= LogLevel . Trace ) {
38
+ if ( this . logLevel > LogLevel . Off ) {
48
39
this . logChannel . trace ( `Log directory: ${ this . logDirectoryPath . fsPath } ` ) ;
49
40
}
50
41
@@ -151,57 +142,21 @@ export class Logger implements ILogger {
151
142
}
152
143
153
144
private async openLogFolder ( ) : Promise < void > {
154
- if ( this . logDirectoryCreated ) {
155
- // Open the folder in VS Code since there isn't an easy way to
156
- // open the folder in the platform's file browser
157
- await commands . executeCommand ( "openFolder" , this . logDirectoryPath , true ) ;
158
- } else {
159
- void this . writeAndShowError ( "Cannot open PowerShell log directory as it does not exist!" ) ;
160
- }
161
- }
162
-
163
- private static timestampMessage ( message : string , level : LogLevel ) : string {
164
- const now = new Date ( ) ;
165
- return `${ now . toLocaleDateString ( ) } ${ now . toLocaleTimeString ( ) } [${ LogLevel [ level ] . toUpperCase ( ) } ] - ${ message } ${ os . EOL } ` ;
145
+ await commands . executeCommand ( "openFolder" , this . logDirectoryPath , true ) ;
166
146
}
167
147
168
- // TODO: Should we await this function above?
169
148
private async writeLine ( message : string , level : LogLevel = LogLevel . Info ) : Promise < void > {
170
- switch ( level ) {
171
- case LogLevel . Trace : this . logChannel . trace ( message ) ; break ;
172
- case LogLevel . Debug : this . logChannel . debug ( message ) ; break ;
173
- case LogLevel . Info : this . logChannel . info ( message ) ; break ;
174
- case LogLevel . Warning : this . logChannel . warn ( message ) ; break ;
175
- case LogLevel . Error : this . logChannel . error ( message ) ; break ;
176
- default : this . logChannel . appendLine ( message ) ; break ;
177
- }
178
-
179
- if ( this . logLevel !== LogLevel . Off ) {
180
- // A simple lock because this function isn't re-entrant.
181
- while ( this . writingLog ) {
182
- await sleep ( 300 ) ;
149
+ return new Promise < void > ( ( resolve ) => {
150
+ switch ( level ) {
151
+ case LogLevel . Off : break ;
152
+ case LogLevel . Trace : this . logChannel . trace ( message ) ; break ;
153
+ case LogLevel . Debug : this . logChannel . debug ( message ) ; break ;
154
+ case LogLevel . Info : this . logChannel . info ( message ) ; break ;
155
+ case LogLevel . Warning : this . logChannel . warn ( message ) ; break ;
156
+ case LogLevel . Error : this . logChannel . error ( message ) ; break ;
157
+ default : this . logChannel . appendLine ( message ) ; break ;
183
158
}
184
- try {
185
- this . writingLog = true ;
186
- if ( ! this . logDirectoryCreated ) {
187
- this . writeVerbose ( `Creating log directory at: '${ this . logDirectoryPath } '` ) ;
188
- await workspace . fs . createDirectory ( this . logDirectoryPath ) ;
189
- this . logDirectoryCreated = true ;
190
- }
191
- let log = new Uint8Array ( ) ;
192
- if ( await checkIfFileExists ( this . logFilePath ) ) {
193
- log = await workspace . fs . readFile ( this . logFilePath ) ;
194
- }
195
-
196
- const timestampedMessage = Logger . timestampMessage ( message , level ) ;
197
- await workspace . fs . writeFile (
198
- this . logFilePath ,
199
- Buffer . concat ( [ log , Buffer . from ( timestampedMessage ) ] ) ) ;
200
- } catch ( err ) {
201
- console . log ( `Error writing to vscode-powershell log file: ${ err } ` ) ;
202
- } finally {
203
- this . writingLog = false ;
204
- }
205
- }
159
+ resolve ( ) ;
160
+ } ) ;
206
161
}
207
162
}
0 commit comments