1
1
'use strict' ;
2
2
3
+ const config = process . binding ( 'config' ) ;
3
4
const prefix = `(${ process . release . name } :${ process . pid } ) ` ;
4
5
5
6
exports . setup = setupProcessWarnings ;
6
7
8
+ var fs ;
9
+ var cachedFd ;
10
+ var acquiringFd = false ;
11
+ function nop ( ) { }
12
+
13
+ function lazyFs ( ) {
14
+ if ( ! fs )
15
+ fs = require ( 'fs' ) ;
16
+ return fs ;
17
+ }
18
+
19
+ function writeOut ( message ) {
20
+ if ( console && typeof console . error === 'function' )
21
+ return console . error ( message ) ;
22
+ process . _rawDebug ( message ) ;
23
+ }
24
+
25
+ function onClose ( fd ) {
26
+ return function ( ) {
27
+ lazyFs ( ) . close ( fd , nop ) ;
28
+ } ;
29
+ }
30
+
31
+ function onOpen ( cb ) {
32
+ return function ( err , fd ) {
33
+ acquiringFd = false ;
34
+ if ( fd !== undefined ) {
35
+ cachedFd = fd ;
36
+ process . on ( 'exit' , onClose ( fd ) ) ;
37
+ }
38
+ cb ( err , fd ) ;
39
+ process . emit ( '_node_warning_fd_acquired' , err , fd ) ;
40
+ } ;
41
+ }
42
+
43
+ function onAcquired ( message ) {
44
+ // make a best effort attempt at writing the message
45
+ // to the fd. Errors are ignored at this point.
46
+ return function ( err , fd ) {
47
+ if ( err )
48
+ return writeOut ( message ) ;
49
+ lazyFs ( ) . appendFile ( fd , `${ message } \n` , nop ) ;
50
+ } ;
51
+ }
52
+
53
+ function acquireFd ( cb ) {
54
+ if ( cachedFd === undefined && ! acquiringFd ) {
55
+ acquiringFd = true ;
56
+ lazyFs ( ) . open ( config . warningFile , 'a' , onOpen ( cb ) ) ;
57
+ } else if ( cachedFd !== undefined && ! acquiringFd ) {
58
+ cb ( null , cachedFd ) ;
59
+ } else {
60
+ process . once ( '_node_warning_fd_acquired' , cb ) ;
61
+ }
62
+ }
63
+
64
+ function output ( message ) {
65
+ if ( typeof config . warningFile === 'string' ) {
66
+ acquireFd ( onAcquired ( message ) ) ;
67
+ return ;
68
+ }
69
+ writeOut ( message ) ;
70
+ }
71
+
72
+ function doEmitWarning ( warning ) {
73
+ return function ( ) {
74
+ process . emit ( 'warning' , warning ) ;
75
+ } ;
76
+ }
77
+
7
78
function setupProcessWarnings ( ) {
8
79
if ( ! process . noProcessWarnings && process . env . NODE_NO_WARNINGS !== '1' ) {
9
80
process . on ( 'warning' , ( warning ) => {
@@ -14,19 +85,18 @@ function setupProcessWarnings() {
14
85
( isDeprecation && process . traceDeprecation ) ;
15
86
if ( trace && warning . stack ) {
16
87
if ( warning . code ) {
17
- console . error ( `${ prefix } [${ warning . code } ] ${ warning . stack } ` ) ;
88
+ output ( `${ prefix } [${ warning . code } ] ${ warning . stack } ` ) ;
18
89
} else {
19
- console . error ( `${ prefix } ${ warning . stack } ` ) ;
90
+ output ( `${ prefix } ${ warning . stack } ` ) ;
20
91
}
21
92
} else {
22
93
const toString =
23
94
typeof warning . toString === 'function' ?
24
95
warning . toString : Error . prototype . toString ;
25
96
if ( warning . code ) {
26
- console . error (
27
- `${ prefix } [${ warning . code } ] ${ toString . apply ( warning ) } ` ) ;
97
+ output ( `${ prefix } [${ warning . code } ] ${ toString . apply ( warning ) } ` ) ;
28
98
} else {
29
- console . error ( `${ prefix } ${ toString . apply ( warning ) } ` ) ;
99
+ output ( `${ prefix } ${ toString . apply ( warning ) } ` ) ;
30
100
}
31
101
}
32
102
} ) ;
@@ -63,6 +133,6 @@ function setupProcessWarnings() {
63
133
if ( process . throwDeprecation )
64
134
throw warning ;
65
135
}
66
- process . nextTick ( ( ) => process . emit ( 'warning' , warning ) ) ;
136
+ process . nextTick ( doEmitWarning ( warning ) ) ;
67
137
} ;
68
138
}
0 commit comments