11/**
22 * @license
3- * Copyright (c) 2022, 2023 , Oracle and/or its affiliates.
3+ * Copyright (c) 2022, 2025 , Oracle and/or its affiliates.
44 * Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
55 */
66
@@ -20,38 +20,52 @@ const LoggingLevels = [
2020let _logFilename ;
2121let _loggingLevel ;
2222let _isHeadlessMode ;
23+ let _isStdoutErrorEpipe ;
2324
2425( function ( ) {
2526 _loggingLevel = 'info' ;
2627 _isHeadlessMode = false ;
28+ _isStdoutErrorEpipe = false ;
2729 const _error = console . error ;
2830 const _warning = console . warning ;
2931 const _debug = console . debug ;
3032 const _trace = console . trace ;
3133
3234 console . error = function ( line ) {
3335 fs . appendFileSync ( _logFilename , line + os . EOL ) ;
34- if ( ! _isHeadlessMode ) _error . apply ( console , arguments ) ;
36+ if ( _useConsoleLogging ( ) ) _error . apply ( console , arguments ) ;
3537 } ;
3638
3739 console . warning = function ( line ) {
3840 fs . appendFileSync ( _logFilename , line + os . EOL ) ;
39- if ( ! _isHeadlessMode ) _warning . apply ( console , arguments ) ;
41+ if ( _useConsoleLogging ( ) ) _warning . apply ( console , arguments ) ;
4042 } ;
4143
4244 console . debug = function ( line ) {
4345 fs . appendFileSync ( _logFilename , line + os . EOL ) ;
44- if ( ! _isHeadlessMode ) _debug . apply ( console , arguments ) ;
46+ if ( _useConsoleLogging ( ) ) _debug . apply ( console , arguments ) ;
4547 } ;
4648
4749 console . trace = function ( line ) {
4850 fs . appendFileSync ( _logFilename , line + os . EOL ) ;
49- if ( ! _isHeadlessMode ) _trace . apply ( console , arguments ) ;
51+ if ( _useConsoleLogging ( ) ) _trace . apply ( console , arguments ) ;
5052 } ;
53+
54+ // Handle error on stdout and also prevent a dialog box when using console logging functions...
55+ process . stdout . on ( 'error' , function ( error ) {
56+ if ( error . code == 'EPIPE' ) _isStdoutErrorEpipe = true ;
57+ const caller = _getCaller ( ( new Error ( 'StackLog' ) ) ) ;
58+ const line = `${ getLogEntryDateTime ( ) } ${ getLogEntryLevel ( 'error' ) } ${ caller } ${ error } ` ;
59+ fs . appendFileSync ( _logFilename , line + os . EOL ) ;
60+ } ) ;
5161} ) ( ) ;
5262
63+ function _useConsoleLogging ( ) {
64+ return ( ! _isHeadlessMode && ! _isStdoutErrorEpipe ) ;
65+ }
66+
5367function initializeLog ( options ) {
54- _logFilename = _rotateLogfile ( options . appPaths . userData , options . baseFilename ) ;
68+ _logFilename = _getLogFileName ( options . appPaths . userData , options . baseFilename ) ;
5569 if ( options . loggingLevel ) _loggingLevel = options . loggingLevel ;
5670 if ( options . isHeadlessMode ) _isHeadlessMode = options . isHeadlessMode ;
5771}
@@ -82,27 +96,28 @@ function getLogEntryLevel(level = 'info') {
8296 return levelSwitch ( level ) ;
8397}
8498
99+ function _getCaller ( stacklog ) {
100+ let caller = '' ;
101+ const stackParts = stacklog . stack . split ( '\n' ) ;
102+ if ( stackParts . length > 2 ) {
103+ const matched = stackParts [ 2 ] . match ( / ( [ ^ / ] + \. j s ) / ) ;
104+ caller = ( matched ? matched [ 0 ] : stackParts [ 2 ] ) ;
105+ }
106+ return caller ;
107+ }
108+
85109/**
86110 *
87111 * @param {string } [level='info']
88112 * @param {string } message
89113 */
90- function log ( level = 'info' , message ) {
114+ function log ( level = 'info' , message , infoUseTimeStamp = true ) {
91115 function isLoggableLevel ( level ) {
92116 return ( LoggingLevels . indexOf ( level ) <= LoggingLevels . indexOf ( _loggingLevel ) ) ;
93117 }
94118
95- function getCaller ( stacklog ) {
96- let caller = '' ;
97- const stackParts = stacklog . stack . split ( '\n' ) ;
98- if ( stackParts . length > 2 ) {
99- caller = stackParts [ 2 ] . match ( / ( [ ^ / ] + \. j s ) / ) [ 0 ] ;
100- }
101- return caller ;
102- }
103-
104119 if ( isLoggableLevel ( level ) ) {
105- const caller = getCaller ( ( new Error ( 'StackLog' ) ) ) ;
120+ const caller = _getCaller ( ( new Error ( 'StackLog' ) ) ) ;
106121 switch ( level ) {
107122 case 'error' :
108123 ( ( line ) => {
@@ -125,10 +140,14 @@ function log(level = 'info', message) {
125140 } ) ( `${ getLogEntryDateTime ( ) } ${ getLogEntryLevel ( level ) } ${ caller } ${ message } ` ) ;
126141 break ;
127142 default :
128- if ( ! _isHeadlessMode ) {
129- ( ( line ) => { console . log ( line ) ; } ) ( message ) ;
143+ let msg = message ;
144+ if ( infoUseTimeStamp ) {
145+ msg = `${ getLogEntryDateTime ( ) } ${ getLogEntryLevel ( 'info' ) } ${ caller } ${ message } ` ;
130146 }
131- ( ( line ) => { fs . appendFileSync ( _logFilename , line + os . EOL ) ; } ) ( message ) ;
147+ if ( _useConsoleLogging ( ) ) {
148+ ( ( line ) => { console . log ( line ) ; } ) ( msg ) ;
149+ }
150+ ( ( line ) => { fs . appendFileSync ( _logFilename , line + os . EOL ) ; } ) ( msg ) ;
132151 }
133152 }
134153}
@@ -160,6 +179,17 @@ function setOptions(options) {
160179 }
161180}
162181
182+ function rotateLog ( options ) {
183+ _logFilename = _rotateLogfile ( options . appPaths . userData , options . baseFilename ) ;
184+ if ( options . loggingLevel ) _loggingLevel = options . loggingLevel ;
185+ if ( options . isHeadlessMode ) _isHeadlessMode = options . isHeadlessMode ;
186+ }
187+
188+ function _getLogFileName ( userDataPath , baseFilename ) {
189+ const filename = `${ baseFilename } .log` ;
190+ return `${ userDataPath } /${ filename } ` ;
191+ }
192+
163193function _rotateLogfile ( userDataPath , baseFilename ) {
164194 function appendLogEntries ( file , rotateFilename ) {
165195 const w = fs . createWriteStream ( rotateFilename , { flags : 'a' } ) ;
@@ -174,8 +204,7 @@ function _rotateLogfile(userDataPath, baseFilename) {
174204 } ) ;
175205 }
176206
177- const filename = `${ baseFilename } .log` ;
178- const file = `${ userDataPath } /${ filename } ` ;
207+ const file = _getLogFileName ( userDataPath , baseFilename ) ;
179208
180209 const logDate = ( new Date ( ) ) . toISOString ( ) . slice ( 0 , 10 ) ;
181210 const rotateFilename = `${ userDataPath } /${ baseFilename } -${ logDate } .log` ;
@@ -196,5 +225,6 @@ module.exports = {
196225 getLogLevels,
197226 setLoggingLevel,
198227 getLoggingLevel,
199- setOptions
228+ setOptions,
229+ rotateLog
200230} ;
0 commit comments