1
1
// Copyright (c) Microsoft Corporation.
2
2
// Licensed under the MIT License.
3
3
4
+ "use strict" ;
5
+
4
6
import fs = require( "fs" ) ;
5
7
import os = require( "os" ) ;
6
- import path = require( "path" ) ;
7
8
import vscode = require( "vscode" ) ;
8
9
9
10
export enum LogLevel {
@@ -19,16 +20,15 @@ export enum LogLevel {
19
20
* This will allow for easy mocking of the logger during unit tests.
20
21
*/
21
22
export 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 ;
28
29
}
29
30
30
31
export class Logger implements ILogger {
31
-
32
32
public logBasePath : vscode . Uri ;
33
33
public logSessionPath : vscode . Uri ;
34
34
public MinimumLogLevel : LogLevel = LogLevel . Normal ;
@@ -40,54 +40,55 @@ export class Logger implements ILogger {
40
40
constructor ( logBasePath : vscode . Uri ) {
41
41
this . logChannel = vscode . window . createOutputChannel ( "PowerShell Extension Logs" ) ;
42
42
this . logBasePath = vscode . Uri . joinPath ( logBasePath , "logs" ) ;
43
-
44
43
this . commands = [
45
44
vscode . commands . registerCommand (
46
45
"PowerShell.ShowLogs" ,
47
46
( ) => { this . showLogPanel ( ) ; } ) ,
48
47
49
48
vscode . commands . registerCommand (
50
49
"PowerShell.OpenLogFolder" ,
51
- ( ) => { this . openLogFolder ( ) ; } ) ,
50
+ async ( ) => { await this . openLogFolder ( ) ; } ) ,
52
51
] ;
53
52
}
54
53
55
54
public dispose ( ) {
56
- this . commands . forEach ( ( command ) => { command . dispose ( ) ; } ) ;
57
55
this . logChannel . dispose ( ) ;
56
+ for ( const command of this . commands ) {
57
+ command . dispose ( ) ;
58
+ }
58
59
}
59
60
60
61
public getLogFilePath ( baseName : string ) : vscode . Uri {
61
62
return vscode . Uri . joinPath ( this . logSessionPath , `${ baseName } .log` ) ;
62
63
}
63
64
64
- public writeAtLevel ( logLevel : LogLevel , message : string , ...additionalMessages : string [ ] ) {
65
+ public writeAtLevel ( logLevel : LogLevel , message : string , ...additionalMessages : string [ ] ) : void {
65
66
if ( logLevel >= this . MinimumLogLevel ) {
66
67
this . writeLine ( message , logLevel ) ;
67
68
68
- additionalMessages . forEach ( ( line ) => {
69
- this . writeLine ( line , logLevel ) ;
70
- } ) ;
69
+ for ( const additionalMessage of additionalMessages ) {
70
+ this . writeLine ( additionalMessage , logLevel ) ;
71
+ } ;
71
72
}
72
73
}
73
74
74
- public write ( message : string , ...additionalMessages : string [ ] ) {
75
+ public write ( message : string , ...additionalMessages : string [ ] ) : void {
75
76
this . writeAtLevel ( LogLevel . Normal , message , ...additionalMessages ) ;
76
77
}
77
78
78
- public writeDiagnostic ( message : string , ...additionalMessages : string [ ] ) {
79
+ public writeDiagnostic ( message : string , ...additionalMessages : string [ ] ) : void {
79
80
this . writeAtLevel ( LogLevel . Diagnostic , message , ...additionalMessages ) ;
80
81
}
81
82
82
- public writeVerbose ( message : string , ...additionalMessages : string [ ] ) {
83
+ public writeVerbose ( message : string , ...additionalMessages : string [ ] ) : void {
83
84
this . writeAtLevel ( LogLevel . Verbose , message , ...additionalMessages ) ;
84
85
}
85
86
86
- public writeWarning ( message : string , ...additionalMessages : string [ ] ) {
87
+ public writeWarning ( message : string , ...additionalMessages : string [ ] ) : void {
87
88
this . writeAtLevel ( LogLevel . Warning , message , ...additionalMessages ) ;
88
89
}
89
90
90
- public writeAndShowWarning ( message : string , ...additionalMessages : string [ ] ) {
91
+ public writeAndShowWarning ( message : string , ...additionalMessages : string [ ] ) : void {
91
92
this . writeWarning ( message , ...additionalMessages ) ;
92
93
93
94
vscode . window . showWarningMessage ( message , "Show Logs" ) . then ( ( selection ) => {
@@ -97,23 +98,22 @@ export class Logger implements ILogger {
97
98
} ) ;
98
99
}
99
100
100
- public writeError ( message : string , ...additionalMessages : string [ ] ) {
101
+ public writeError ( message : string , ...additionalMessages : string [ ] ) : void {
101
102
this . writeAtLevel ( LogLevel . Error , message , ...additionalMessages ) ;
102
103
}
103
104
104
- public writeAndShowError ( message : string , ...additionalMessages : string [ ] ) {
105
+ public async writeAndShowError ( message : string , ...additionalMessages : string [ ] ) : Promise < void > {
105
106
this . writeError ( message , ...additionalMessages ) ;
106
107
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
+ }
112
112
}
113
113
114
114
public async writeAndShowErrorWithActions (
115
115
message : string ,
116
- actions : { prompt : string ; action : ( ) => Promise < void > } [ ] ) {
116
+ actions : { prompt : string ; action : ( ) => Promise < void > } [ ] ) : Promise < void > {
117
117
this . writeError ( message ) ;
118
118
119
119
const fullActions = [
@@ -134,7 +134,7 @@ export class Logger implements ILogger {
134
134
}
135
135
}
136
136
137
- public async startNewLog ( minimumLogLevel : string = "Normal" ) {
137
+ public async startNewLog ( minimumLogLevel : string = "Normal" ) : Promise < void > {
138
138
this . MinimumLogLevel = this . logLevelNameToValue ( minimumLogLevel . trim ( ) ) ;
139
139
140
140
this . logSessionPath =
@@ -158,19 +158,19 @@ export class Logger implements ILogger {
158
158
}
159
159
}
160
160
161
- private showLogPanel ( ) {
161
+ private showLogPanel ( ) : void {
162
162
this . logChannel . show ( ) ;
163
163
}
164
164
165
- private openLogFolder ( ) {
165
+ private async openLogFolder ( ) : Promise < void > {
166
166
if ( this . logSessionPath ) {
167
167
// Open the folder in VS Code since there isn't an easy way to
168
168
// 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 ) ;
170
170
}
171
171
}
172
172
173
- private writeLine ( message : string , level : LogLevel = LogLevel . Normal ) {
173
+ private writeLine ( message : string , level : LogLevel = LogLevel . Normal ) : void {
174
174
const now = new Date ( ) ;
175
175
const timestampedMessage =
176
176
`${ now . toLocaleDateString ( ) } ${ now . toLocaleTimeString ( ) } [${ LogLevel [ level ] . toUpperCase ( ) } ] - ${ message } ` ;
0 commit comments