11'use strict' ;
22
33const {
4+ FunctionPrototypeBind,
5+ ObjectCreate,
6+ ObjectDefineProperty,
47 RegExp,
8+ RegExpPrototypeTest,
9+ StringPrototypeToUpperCase
510} = primordials ;
611
712const { inspect, format, formatWithOptions } = require ( 'internal/util/inspect' ) ;
813
914// `debugs` is deliberately initialized to undefined so any call to
1015// debuglog() before initializeDebugEnv() is called will throw.
11- let debugs ;
16+ let debugImpls ;
1217
1318let debugEnvRegex = / ^ $ / ;
19+ let testEnabled ;
1420
1521// `debugEnv` is initial value of process.env.NODE_DEBUG
1622function initializeDebugEnv ( debugEnv ) {
17- debugs = { } ;
23+ debugImpls = ObjectCreate ( null ) ;
1824 if ( debugEnv ) {
1925 debugEnv = debugEnv . replace ( / [ | \\ { } ( ) [ \] ^ $ + ? . ] / g, '\\$&' )
2026 . replace ( / \* / g, '.*' )
2127 . replace ( / , / g, '$|^' )
2228 . toUpperCase ( ) ;
2329 debugEnvRegex = new RegExp ( `^${ debugEnv } $` , 'i' ) ;
2430 }
31+ testEnabled = FunctionPrototypeBind ( RegExpPrototypeTest , null , debugEnvRegex ) ;
2532}
2633
2734// Emits warning when user sets
@@ -37,41 +44,57 @@ function emitWarningIfNeeded(set) {
3744
3845function noop ( ) { }
3946
40- function debuglogImpl ( set ) {
41- set = set . toUpperCase ( ) ;
42- if ( debugs [ set ] === undefined ) {
43- if ( debugEnvRegex . test ( set ) ) {
47+ function debuglogImpl ( enabled , set ) {
48+ if ( debugImpls [ set ] === undefined ) {
49+ if ( enabled ) {
4450 const pid = process . pid ;
4551 emitWarningIfNeeded ( set ) ;
46- debugs [ set ] = function debug ( ...args ) {
52+ debugImpls [ set ] = function debug ( ...args ) {
4753 const colors = process . stderr . hasColors && process . stderr . hasColors ( ) ;
4854 const msg = formatWithOptions ( { colors } , ...args ) ;
4955 const coloredPID = inspect ( pid , { colors } ) ;
5056 process . stderr . write ( format ( '%s %s: %s\n' , set , coloredPID , msg ) ) ;
5157 } ;
5258 } else {
53- debugs [ set ] = noop ;
59+ debugImpls [ set ] = noop ;
5460 }
5561 }
56- return debugs [ set ] ;
62+ return debugImpls [ set ] ;
5763}
5864
5965// debuglogImpl depends on process.pid and process.env.NODE_DEBUG,
6066// so it needs to be called lazily in top scopes of internal modules
6167// that may be loaded before these run time states are allowed to
6268// be accessed.
6369function debuglog ( set , cb ) {
70+ function init ( ) {
71+ set = StringPrototypeToUpperCase ( set ) ;
72+ enabled = testEnabled ( set ) ;
73+ }
6474 let debug = ( ...args ) => {
75+ init ( ) ;
6576 // Only invokes debuglogImpl() when the debug function is
6677 // called for the first time.
67- debug = debuglogImpl ( set ) ;
78+ debug = debuglogImpl ( enabled , set ) ;
6879 if ( typeof cb === 'function' )
6980 cb ( debug ) ;
7081 debug ( ...args ) ;
7182 } ;
72- return ( ...args ) => {
73- debug ( ...args ) ;
83+ let enabled ;
84+ let test = ( ) => {
85+ init ( ) ;
86+ test = ( ) => enabled ;
87+ return enabled ;
7488 } ;
89+ const logger = ( ...args ) => debug ( ...args ) ;
90+ ObjectDefineProperty ( logger , 'enabled' , {
91+ get ( ) {
92+ return test ( ) ;
93+ } ,
94+ configurable : true ,
95+ enumerable : true
96+ } ) ;
97+ return logger ;
7598}
7699
77100module . exports = {
0 commit comments