@@ -131,6 +131,14 @@ namespace ts.server {
131131 constructor ( private readonly logFilename : string ,
132132 private readonly traceToConsole : boolean ,
133133 private readonly level : LogLevel ) {
134+ if ( this . logFilename ) {
135+ try {
136+ this . fd = fs . openSync ( this . logFilename , "w" ) ;
137+ }
138+ catch ( _ ) {
139+ // swallow the error and keep logging disabled if file cannot be opened
140+ }
141+ }
134142 }
135143
136144 static padStringRight ( str : string , padding : string ) {
@@ -175,11 +183,6 @@ namespace ts.server {
175183 }
176184
177185 msg ( s : string , type : Msg . Types = Msg . Err ) {
178- if ( this . fd < 0 ) {
179- if ( this . logFilename ) {
180- this . fd = fs . openSync ( this . logFilename , "w" ) ;
181- }
182- }
183186 if ( this . fd >= 0 || this . traceToConsole ) {
184187 s = s + "\n" ;
185188 const prefix = Logger . padStringRight ( type + " " + this . seq . toString ( ) , " " ) ;
@@ -410,6 +413,9 @@ namespace ts.server {
410413 }
411414
412415 function parseLoggingEnvironmentString ( logEnvStr : string ) : LogOptions {
416+ if ( ! logEnvStr ) {
417+ return { } ;
418+ }
413419 const logEnv : LogOptions = { logToFile : true } ;
414420 const args = logEnvStr . split ( " " ) ;
415421 const len = args . length - 1 ;
@@ -422,8 +428,8 @@ namespace ts.server {
422428 logEnv . file = stripQuotes ( value ) ;
423429 break ;
424430 case "-level" :
425- const level : LogLevel = ( < any > LogLevel ) [ value ] ;
426- logEnv . detailLevel = typeof level === "number" ? level : LogLevel . normal ;
431+ const level = getLogLevel ( value ) ;
432+ logEnv . detailLevel = level !== undefined ? level : LogLevel . normal ;
427433 break ;
428434 case "-traceToConsole" :
429435 logEnv . traceToConsole = value . toLowerCase ( ) === "true" ;
@@ -437,28 +443,32 @@ namespace ts.server {
437443 return logEnv ;
438444 }
439445
440- // TSS_LOG "{ level: "normal | verbose | terse", file?: string}"
441- function createLoggerFromEnv ( ) {
442- let fileName : string = undefined ;
443- let detailLevel = LogLevel . normal ;
444- let traceToConsole = false ;
445- const logEnvStr = process . env [ "TSS_LOG" ] ;
446- if ( logEnvStr ) {
447- const logEnv = parseLoggingEnvironmentString ( logEnvStr ) ;
448- if ( logEnv . logToFile ) {
449- if ( logEnv . file ) {
450- fileName = logEnv . file ;
451- }
452- else {
453- fileName = __dirname + "/.log" + process . pid . toString ( ) ;
446+ function getLogLevel ( level : string ) {
447+ if ( level ) {
448+ const l = level . toLowerCase ( ) ;
449+ for ( const name in LogLevel ) {
450+ if ( isNaN ( + name ) && l === name . toLowerCase ( ) ) {
451+ return < LogLevel > < any > LogLevel [ name ] ;
454452 }
455453 }
456- if ( logEnv . detailLevel ) {
457- detailLevel = logEnv . detailLevel ;
458- }
459- traceToConsole = logEnv . traceToConsole ;
460454 }
461- return new Logger ( fileName , traceToConsole , detailLevel ) ;
455+ return undefined ;
456+ }
457+
458+ // TSS_LOG "{ level: "normal | verbose | terse", file?: string}"
459+ function createLogger ( ) {
460+ const cmdLineLogFileName = findArgument ( "--logFile" ) ;
461+ const cmdLineVerbosity = getLogLevel ( findArgument ( "--logVerbosity" ) ) ;
462+ const envLogOptions = parseLoggingEnvironmentString ( process . env [ "TSS_LOG" ] ) ;
463+
464+ const logFileName = cmdLineLogFileName
465+ ? stripQuotes ( cmdLineLogFileName )
466+ : envLogOptions . logToFile
467+ ? envLogOptions . file || ( __dirname + "/.log" + process . pid . toString ( ) )
468+ : undefined ;
469+
470+ const logVerbosity = cmdLineVerbosity || envLogOptions . detailLevel ;
471+ return new Logger ( logFileName , envLogOptions . traceToConsole , logVerbosity )
462472 }
463473 // This places log file in the directory containing editorServices.js
464474 // TODO: check that this location is writable
@@ -555,7 +565,6 @@ namespace ts.server {
555565 // to increase the chunk size or decrease the interval
556566 // time dynamically to match the large reference set?
557567 const pollingWatchedFileSet = createPollingWatchedFileSet ( ) ;
558- const logger = createLoggerFromEnv ( ) ;
559568
560569 const pending : Buffer [ ] = [ ] ;
561570 let canWrite = true ;
@@ -607,6 +616,8 @@ namespace ts.server {
607616 return s . length > 2 && s . charCodeAt ( 0 ) === CharacterCodes . slash && s . charCodeAt ( 1 ) === CharacterCodes . slash ;
608617 }
609618
619+ const logger = createLogger ( ) ;
620+
610621 const sys = < ServerHost > ts . sys ;
611622 // use watchGuard process on Windows when node version is 4 or later
612623 const useWatchGuard = process . platform === "win32" && getNodeMajorVersion ( ) >= 4 ;
0 commit comments