11// Copyright (c) Microsoft Corporation.
22// Licensed under the MIT License.
33
4+ "use strict" ;
5+
46import fs = require( "fs" ) ;
57import os = require( "os" ) ;
6- import path = require( "path" ) ;
78import vscode = require( "vscode" ) ;
89
910export enum LogLevel {
@@ -19,16 +20,15 @@ export enum LogLevel {
1920 * This will allow for easy mocking of the logger during unit tests.
2021 */
2122export interface ILogger {
22- write ( message : string , ...additionalMessages : string [ ] ) ;
23- writeDiagnostic ( message : string , ...additionalMessages : string [ ] ) ;
24- writeVerbose ( message : string , ...additionalMessages : string [ ] ) ;
25- writeWarning ( message : string , ...additionalMessages : string [ ] ) ;
26- writeAndShowWarning ( message : string , ...additionalMessages : string [ ] ) ;
27- writeError ( message : string , ...additionalMessages : string [ ] ) ;
23+ write ( message : string , ...additionalMessages : string [ ] ) : void ;
24+ writeDiagnostic ( message : string , ...additionalMessages : string [ ] ) : void ;
25+ writeVerbose ( message : string , ...additionalMessages : string [ ] ) : void ;
26+ writeWarning ( message : string , ...additionalMessages : string [ ] ) : void ;
27+ writeAndShowWarning ( message : string , ...additionalMessages : string [ ] ) : void ;
28+ writeError ( message : string , ...additionalMessages : string [ ] ) : void ;
2829}
2930
3031export class Logger implements ILogger {
31-
3232 public logBasePath : vscode . Uri ;
3333 public logSessionPath : vscode . Uri ;
3434 public MinimumLogLevel : LogLevel = LogLevel . Normal ;
@@ -40,54 +40,55 @@ export class Logger implements ILogger {
4040 constructor ( logBasePath : vscode . Uri ) {
4141 this . logChannel = vscode . window . createOutputChannel ( "PowerShell Extension Logs" ) ;
4242 this . logBasePath = vscode . Uri . joinPath ( logBasePath , "logs" ) ;
43-
4443 this . commands = [
4544 vscode . commands . registerCommand (
4645 "PowerShell.ShowLogs" ,
4746 ( ) => { this . showLogPanel ( ) ; } ) ,
4847
4948 vscode . commands . registerCommand (
5049 "PowerShell.OpenLogFolder" ,
51- ( ) => { this . openLogFolder ( ) ; } ) ,
50+ async ( ) => { await this . openLogFolder ( ) ; } ) ,
5251 ] ;
5352 }
5453
5554 public dispose ( ) {
56- this . commands . forEach ( ( command ) => { command . dispose ( ) ; } ) ;
5755 this . logChannel . dispose ( ) ;
56+ for ( const command of this . commands ) {
57+ command . dispose ( ) ;
58+ }
5859 }
5960
6061 public getLogFilePath ( baseName : string ) : vscode . Uri {
6162 return vscode . Uri . joinPath ( this . logSessionPath , `${ baseName } .log` ) ;
6263 }
6364
64- public writeAtLevel ( logLevel : LogLevel , message : string , ...additionalMessages : string [ ] ) {
65+ public writeAtLevel ( logLevel : LogLevel , message : string , ...additionalMessages : string [ ] ) : void {
6566 if ( logLevel >= this . MinimumLogLevel ) {
6667 this . writeLine ( message , logLevel ) ;
6768
68- additionalMessages . forEach ( ( line ) => {
69- this . writeLine ( line , logLevel ) ;
70- } ) ;
69+ for ( const additionalMessage of additionalMessages ) {
70+ this . writeLine ( additionalMessage , logLevel ) ;
71+ } ;
7172 }
7273 }
7374
74- public write ( message : string , ...additionalMessages : string [ ] ) {
75+ public write ( message : string , ...additionalMessages : string [ ] ) : void {
7576 this . writeAtLevel ( LogLevel . Normal , message , ...additionalMessages ) ;
7677 }
7778
78- public writeDiagnostic ( message : string , ...additionalMessages : string [ ] ) {
79+ public writeDiagnostic ( message : string , ...additionalMessages : string [ ] ) : void {
7980 this . writeAtLevel ( LogLevel . Diagnostic , message , ...additionalMessages ) ;
8081 }
8182
82- public writeVerbose ( message : string , ...additionalMessages : string [ ] ) {
83+ public writeVerbose ( message : string , ...additionalMessages : string [ ] ) : void {
8384 this . writeAtLevel ( LogLevel . Verbose , message , ...additionalMessages ) ;
8485 }
8586
86- public writeWarning ( message : string , ...additionalMessages : string [ ] ) {
87+ public writeWarning ( message : string , ...additionalMessages : string [ ] ) : void {
8788 this . writeAtLevel ( LogLevel . Warning , message , ...additionalMessages ) ;
8889 }
8990
90- public writeAndShowWarning ( message : string , ...additionalMessages : string [ ] ) {
91+ public writeAndShowWarning ( message : string , ...additionalMessages : string [ ] ) : void {
9192 this . writeWarning ( message , ...additionalMessages ) ;
9293
9394 vscode . window . showWarningMessage ( message , "Show Logs" ) . then ( ( selection ) => {
@@ -97,23 +98,22 @@ export class Logger implements ILogger {
9798 } ) ;
9899 }
99100
100- public writeError ( message : string , ...additionalMessages : string [ ] ) {
101+ public writeError ( message : string , ...additionalMessages : string [ ] ) : void {
101102 this . writeAtLevel ( LogLevel . Error , message , ...additionalMessages ) ;
102103 }
103104
104- public writeAndShowError ( message : string , ...additionalMessages : string [ ] ) {
105+ public async writeAndShowError ( message : string , ...additionalMessages : string [ ] ) : Promise < void > {
105106 this . writeError ( message , ...additionalMessages ) ;
106107
107- vscode . window . showErrorMessage ( message , "Show Logs" ) . then ( ( selection ) => {
108- if ( selection !== undefined ) {
109- this . showLogPanel ( ) ;
110- }
111- } ) ;
108+ const choice = await vscode . window . showErrorMessage ( message , "Show Logs" ) ;
109+ if ( choice !== undefined ) {
110+ this . showLogPanel ( ) ;
111+ }
112112 }
113113
114114 public async writeAndShowErrorWithActions (
115115 message : string ,
116- actions : { prompt : string ; action : ( ) => Promise < void > } [ ] ) {
116+ actions : { prompt : string ; action : ( ) => Promise < void > } [ ] ) : Promise < void > {
117117 this . writeError ( message ) ;
118118
119119 const fullActions = [
@@ -134,7 +134,7 @@ export class Logger implements ILogger {
134134 }
135135 }
136136
137- public async startNewLog ( minimumLogLevel : string = "Normal" ) {
137+ public async startNewLog ( minimumLogLevel : string = "Normal" ) : Promise < void > {
138138 this . MinimumLogLevel = this . logLevelNameToValue ( minimumLogLevel . trim ( ) ) ;
139139
140140 this . logSessionPath =
@@ -158,19 +158,19 @@ export class Logger implements ILogger {
158158 }
159159 }
160160
161- private showLogPanel ( ) {
161+ private showLogPanel ( ) : void {
162162 this . logChannel . show ( ) ;
163163 }
164164
165- private openLogFolder ( ) {
165+ private async openLogFolder ( ) : Promise < void > {
166166 if ( this . logSessionPath ) {
167167 // Open the folder in VS Code since there isn't an easy way to
168168 // open the folder in the platform's file browser
169- vscode . commands . executeCommand ( "vscode.openFolder" , this . logSessionPath , true ) ;
169+ await vscode . commands . executeCommand ( "vscode.openFolder" , this . logSessionPath , true ) ;
170170 }
171171 }
172172
173- private writeLine ( message : string , level : LogLevel = LogLevel . Normal ) {
173+ private writeLine ( message : string , level : LogLevel = LogLevel . Normal ) : void {
174174 const now = new Date ( ) ;
175175 const timestampedMessage =
176176 `${ now . toLocaleDateString ( ) } ${ now . toLocaleTimeString ( ) } [${ LogLevel [ level ] . toUpperCase ( ) } ] - ${ message } ` ;
0 commit comments