util: access process states lazily in debuglog
`debuglog()` depends on `process.pid` and `process.env.NODE_DEBUG`, so it needs to be called lazily in top scopes of internal modules that may be loaded before these run time states are allowed to be accessed. This patch makes its implementation lazy by default, the process states are only accessed when the returned debug function is called for the first time. PR-URL: https://github.com/nodejs/node/pull/27281 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Richard Lau <riclau@uk.ibm.com> Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com>
This commit is contained in:
parent
49ee010005
commit
2e4ceb5747
@ -30,14 +30,7 @@ const EE = require('events');
|
|||||||
const Stream = require('stream');
|
const Stream = require('stream');
|
||||||
const { Buffer } = require('buffer');
|
const { Buffer } = require('buffer');
|
||||||
|
|
||||||
let debuglog;
|
const debug = require('internal/util/debuglog').debuglog('stream');
|
||||||
function debug(...args) {
|
|
||||||
if (!debuglog) {
|
|
||||||
debuglog = require('internal/util/debuglog').debuglog('stream');
|
|
||||||
}
|
|
||||||
debuglog(...args);
|
|
||||||
}
|
|
||||||
|
|
||||||
const BufferList = require('internal/streams/buffer_list');
|
const BufferList = require('internal/streams/buffer_list');
|
||||||
const destroyImpl = require('internal/streams/destroy');
|
const destroyImpl = require('internal/streams/destroy');
|
||||||
const { getHighWaterMark } = require('internal/streams/state');
|
const { getHighWaterMark } = require('internal/streams/state');
|
||||||
|
@ -44,6 +44,7 @@ const {
|
|||||||
} = require('internal/process/execution');
|
} = require('internal/process/execution');
|
||||||
|
|
||||||
const publicWorker = require('worker_threads');
|
const publicWorker = require('worker_threads');
|
||||||
|
const debug = require('internal/util/debuglog').debuglog('worker');
|
||||||
|
|
||||||
const assert = require('internal/assert');
|
const assert = require('internal/assert');
|
||||||
|
|
||||||
@ -51,8 +52,6 @@ patchProcessObject();
|
|||||||
setupInspectorHooks();
|
setupInspectorHooks();
|
||||||
setupDebugEnv();
|
setupDebugEnv();
|
||||||
|
|
||||||
const debug = require('internal/util/debuglog').debuglog('worker');
|
|
||||||
|
|
||||||
setupWarningHandler();
|
setupWarningHandler();
|
||||||
|
|
||||||
// Since worker threads cannot switch cwd, we do not need to
|
// Since worker threads cannot switch cwd, we do not need to
|
||||||
|
@ -166,14 +166,7 @@ Object.defineProperty(Module, 'wrapper', {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
let debuglog;
|
const debug = require('internal/util/debuglog').debuglog('module');
|
||||||
function debug(...args) {
|
|
||||||
if (!debuglog) {
|
|
||||||
debuglog = require('internal/util/debuglog').debuglog('module');
|
|
||||||
}
|
|
||||||
debuglog(...args);
|
|
||||||
}
|
|
||||||
|
|
||||||
Module._debug = deprecate(debug, 'Module._debug is deprecated.', 'DEP0077');
|
Module._debug = deprecate(debug, 'Module._debug is deprecated.', 'DEP0077');
|
||||||
|
|
||||||
// Given a module name, and a list of paths to test, returns the first
|
// Given a module name, and a list of paths to test, returns the first
|
||||||
|
@ -31,7 +31,7 @@ const kAfterAsyncWrite = Symbol('kAfterAsyncWrite');
|
|||||||
const kHandle = Symbol('kHandle');
|
const kHandle = Symbol('kHandle');
|
||||||
const kSession = Symbol('kSession');
|
const kSession = Symbol('kSession');
|
||||||
|
|
||||||
const debug = require('util').debuglog('stream');
|
const debug = require('internal/util/debuglog').debuglog('stream');
|
||||||
|
|
||||||
function handleWriteReq(req, data, encoding) {
|
function handleWriteReq(req, data, encoding) {
|
||||||
const { handle } = req;
|
const { handle } = req;
|
||||||
|
@ -107,13 +107,7 @@ const L = require('internal/linkedlist');
|
|||||||
const PriorityQueue = require('internal/priority_queue');
|
const PriorityQueue = require('internal/priority_queue');
|
||||||
|
|
||||||
const { inspect } = require('internal/util/inspect');
|
const { inspect } = require('internal/util/inspect');
|
||||||
let debuglog;
|
const debug = require('internal/util/debuglog').debuglog('timer');
|
||||||
function debug(...args) {
|
|
||||||
if (!debuglog) {
|
|
||||||
debuglog = require('internal/util/debuglog').debuglog('timer');
|
|
||||||
}
|
|
||||||
debuglog(...args);
|
|
||||||
}
|
|
||||||
|
|
||||||
// *Must* match Environment::ImmediateInfo::Fields in src/env.h.
|
// *Must* match Environment::ImmediateInfo::Fields in src/env.h.
|
||||||
const kCount = 0;
|
const kCount = 0;
|
||||||
|
@ -31,7 +31,7 @@ function emitWarningIfNeeded(set) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function debuglog(set) {
|
function debuglogImpl(set) {
|
||||||
set = set.toUpperCase();
|
set = set.toUpperCase();
|
||||||
if (!debugs[set]) {
|
if (!debugs[set]) {
|
||||||
if (debugEnvRegex.test(set)) {
|
if (debugEnvRegex.test(set)) {
|
||||||
@ -48,6 +48,22 @@ function debuglog(set) {
|
|||||||
return debugs[set];
|
return debugs[set];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// debuglogImpl depends on process.pid and process.env.NODE_DEBUG,
|
||||||
|
// so it needs to be called lazily in top scopes of internal modules
|
||||||
|
// that may be loaded before these run time states are allowed to
|
||||||
|
// be accessed.
|
||||||
|
function debuglog(set) {
|
||||||
|
let debug;
|
||||||
|
return function(...args) {
|
||||||
|
if (!debug) {
|
||||||
|
// Only invokes debuglogImpl() when the debug function is
|
||||||
|
// called for the first time.
|
||||||
|
debug = debuglogImpl(set);
|
||||||
|
}
|
||||||
|
debug(...args);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
debuglog,
|
debuglog,
|
||||||
initializeDebugEnv
|
initializeDebugEnv
|
||||||
|
@ -48,14 +48,7 @@ const kOnErrorMessage = Symbol('kOnErrorMessage');
|
|||||||
const kParentSideStdio = Symbol('kParentSideStdio');
|
const kParentSideStdio = Symbol('kParentSideStdio');
|
||||||
|
|
||||||
const SHARE_ENV = Symbol.for('nodejs.worker_threads.SHARE_ENV');
|
const SHARE_ENV = Symbol.for('nodejs.worker_threads.SHARE_ENV');
|
||||||
|
const debug = require('internal/util/debuglog').debuglog('worker');
|
||||||
let debuglog;
|
|
||||||
function debug(...args) {
|
|
||||||
if (!debuglog) {
|
|
||||||
debuglog = require('internal/util/debuglog').debuglog('worker');
|
|
||||||
}
|
|
||||||
debuglog(...args);
|
|
||||||
}
|
|
||||||
|
|
||||||
class Worker extends EventEmitter {
|
class Worker extends EventEmitter {
|
||||||
constructor(filename, options = {}) {
|
constructor(filename, options = {}) {
|
||||||
|
@ -21,14 +21,7 @@ const {
|
|||||||
const { Readable, Writable } = require('stream');
|
const { Readable, Writable } = require('stream');
|
||||||
const EventEmitter = require('events');
|
const EventEmitter = require('events');
|
||||||
const { inspect } = require('internal/util/inspect');
|
const { inspect } = require('internal/util/inspect');
|
||||||
|
const debug = require('internal/util/debuglog').debuglog('worker');
|
||||||
let debuglog;
|
|
||||||
function debug(...args) {
|
|
||||||
if (!debuglog) {
|
|
||||||
debuglog = require('internal/util/debuglog').debuglog('worker');
|
|
||||||
}
|
|
||||||
debuglog(...args);
|
|
||||||
}
|
|
||||||
|
|
||||||
const kIncrementsPortRef = Symbol('kIncrementsPortRef');
|
const kIncrementsPortRef = Symbol('kIncrementsPortRef');
|
||||||
const kName = Symbol('kName');
|
const kName = Symbol('kName');
|
||||||
|
@ -50,14 +50,7 @@ const {
|
|||||||
deprecate
|
deprecate
|
||||||
} = require('internal/util');
|
} = require('internal/util');
|
||||||
const { ERR_INVALID_CALLBACK } = require('internal/errors').codes;
|
const { ERR_INVALID_CALLBACK } = require('internal/errors').codes;
|
||||||
|
const debug = require('internal/util/debuglog').debuglog('timer');
|
||||||
let debuglog;
|
|
||||||
function debug(...args) {
|
|
||||||
if (!debuglog) {
|
|
||||||
debuglog = require('internal/util/debuglog').debuglog('timer');
|
|
||||||
}
|
|
||||||
debuglog(...args);
|
|
||||||
}
|
|
||||||
|
|
||||||
const {
|
const {
|
||||||
destroyHooksExist,
|
destroyHooksExist,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user