@@ -38,7 +38,7 @@ export interface LoggerOptions {
38
38
* Create a logger instance to write to file.
39
39
* @param options Logging options.
40
40
*/
41
- export function createLogger ( options : LoggerOptions ) : Logger {
41
+ export function createLogger ( options : LoggerOptions ) : ts . server . Logger {
42
42
let logLevel : ts . server . LogLevel ;
43
43
switch ( options . logVerbosity ) {
44
44
case 'requestTime' :
@@ -55,9 +55,7 @@ export function createLogger(options: LoggerOptions): Logger {
55
55
logLevel = ts . server . LogLevel . terse ;
56
56
break ;
57
57
}
58
- // If logFile is not provided then just trace to console.
59
- const traceToConsole = ! options . logFile ;
60
- return new Logger ( traceToConsole , logLevel , options . logFile ) ;
58
+ return new Logger ( logLevel , options . logFile ) ;
61
59
}
62
60
63
61
// TODO: Code below is from TypeScript's repository. Maybe create our own
@@ -72,17 +70,17 @@ function nowString() {
72
70
return `${ d . getHours ( ) } :${ d . getMinutes ( ) } :${ d . getSeconds ( ) } .${ d . getMilliseconds ( ) } ` ;
73
71
}
74
72
75
- export class Logger implements ts . server . Logger {
76
- private fd = - 1 ;
73
+ class Logger implements ts . server . Logger {
74
+ private readonly fd : number ;
77
75
private seq = 0 ;
78
76
private inGroup = false ;
79
77
private firstInGroup = true ;
80
78
81
79
constructor (
82
- private readonly traceToConsole : boolean ,
83
80
private readonly level : ts . server . LogLevel ,
84
81
private readonly logFilename ?: string ,
85
82
) {
83
+ this . fd = - 1 ;
86
84
if ( logFilename ) {
87
85
try {
88
86
const dir = path . dirname ( logFilename ) ;
@@ -101,7 +99,7 @@ export class Logger implements ts.server.Logger {
101
99
}
102
100
103
101
close ( ) {
104
- if ( this . fd >= 0 ) {
102
+ if ( this . loggingEnabled ( ) ) {
105
103
fs . close ( this . fd , noop ) ;
106
104
}
107
105
}
@@ -118,10 +116,6 @@ export class Logger implements ts.server.Logger {
118
116
this . msg ( s , ts . server . Msg . Info ) ;
119
117
}
120
118
121
- err ( s : string ) {
122
- this . msg ( s , ts . server . Msg . Err ) ;
123
- }
124
-
125
119
startGroup ( ) {
126
120
this . inGroup = true ;
127
121
this . firstInGroup = true ;
@@ -132,39 +126,29 @@ export class Logger implements ts.server.Logger {
132
126
}
133
127
134
128
loggingEnabled ( ) {
135
- return ! ! this . logFilename || this . traceToConsole ;
129
+ return this . fd >= 0 ;
136
130
}
137
131
138
132
hasLevel ( level : ts . server . LogLevel ) {
139
133
return this . loggingEnabled ( ) && this . level >= level ;
140
134
}
141
135
142
136
msg ( s : string , type : ts . server . Msg = ts . server . Msg . Err ) {
143
- if ( ! this . canWrite ) return ;
137
+ if ( ! this . loggingEnabled ( ) ) {
138
+ return ;
139
+ }
144
140
145
141
s = `[${ nowString ( ) } ] ${ s } \n` ;
146
142
if ( ! this . inGroup || this . firstInGroup ) {
147
143
const prefix = Logger . padStringRight ( type + ' ' + this . seq . toString ( ) , ' ' ) ;
148
144
s = prefix + s ;
149
145
}
150
- this . write ( s ) ;
151
- if ( ! this . inGroup ) {
152
- this . seq ++ ;
153
- }
154
- }
155
146
156
- private get canWrite ( ) {
157
- return this . fd >= 0 || this . traceToConsole ;
158
- }
147
+ const buf = Buffer . from ( s ) ;
148
+ fs . writeSync ( this . fd , buf , 0 , buf . length ) ;
159
149
160
- private write ( s : string ) {
161
- if ( this . fd >= 0 ) {
162
- const buf = Buffer . from ( s ) ;
163
- // tslint:disable-next-line no-null-keyword
164
- fs . writeSync ( this . fd , buf , 0 , buf . length , /*position*/ null ! ) ; // TODO: GH#18217
165
- }
166
- if ( this . traceToConsole ) {
167
- console . warn ( s ) ;
150
+ if ( ! this . inGroup ) {
151
+ this . seq ++ ;
168
152
}
169
153
}
170
154
}
0 commit comments