11// Copyright (c) Microsoft Corporation.
22// Licensed under the MIT License.
33
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" ;
75
86/** Interface for logging operations. New features should use this interface for the "type" of logger.
97 * This will allow for easy mocking of the logger during unit tests.
@@ -28,23 +26,16 @@ export class Logger implements ILogger {
2826 private commands : Disposable [ ] ;
2927 // Log output channel handles all the verbosity management so we don't have to.
3028 private logChannel : LogOutputChannel ;
31- private logFilePath : Uri ; // The client's logs
32- private logDirectoryCreated = false ;
33- private writingLog = false ;
3429 public get logLevel ( ) : LogLevel { return this . logChannel . logLevel ; }
3530
36- constructor ( globalStorageUri : Uri , logChannel ?: LogOutputChannel ) {
31+ constructor ( logPath : Uri , logChannel ?: LogOutputChannel ) {
3732 this . logChannel = logChannel ?? window . createOutputChannel ( "PowerShell" , { log : true } ) ;
3833 // We have to override the scheme because it defaults to
3934 // '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 ;
4536
4637 // Early logging of the log paths for debugging.
47- if ( this . logLevel >= LogLevel . Trace ) {
38+ if ( this . logLevel > LogLevel . Off ) {
4839 this . logChannel . trace ( `Log directory: ${ this . logDirectoryPath . fsPath } ` ) ;
4940 }
5041
@@ -151,57 +142,21 @@ export class Logger implements ILogger {
151142 }
152143
153144 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 ) ;
166146 }
167147
168- // TODO: Should we await this function above?
169148 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 ;
183158 }
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+ } ) ;
206161 }
207162}
0 commit comments