Skip to content

Commit

Permalink
util: access process states lazily in debuglog
Browse files Browse the repository at this point in the history
`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: #27281
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Richard Lau <[email protected]>
Reviewed-By: Yongsheng Zhang <[email protected]>
  • Loading branch information
joyeecheung committed Apr 19, 2019
1 parent 49ee010 commit 2e4ceb5
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 51 deletions.
9 changes: 1 addition & 8 deletions lib/_stream_readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,7 @@ const EE = require('events');
const Stream = require('stream');
const { Buffer } = require('buffer');

let debuglog;
function debug(...args) {
if (!debuglog) {
debuglog = require('internal/util/debuglog').debuglog('stream');
}
debuglog(...args);
}

const debug = require('internal/util/debuglog').debuglog('stream');
const BufferList = require('internal/streams/buffer_list');
const destroyImpl = require('internal/streams/destroy');
const { getHighWaterMark } = require('internal/streams/state');
Expand Down
3 changes: 1 addition & 2 deletions lib/internal/main/worker_thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,14 @@ const {
} = require('internal/process/execution');

const publicWorker = require('worker_threads');
const debug = require('internal/util/debuglog').debuglog('worker');

const assert = require('internal/assert');

patchProcessObject();
setupInspectorHooks();
setupDebugEnv();

const debug = require('internal/util/debuglog').debuglog('worker');

setupWarningHandler();

// Since worker threads cannot switch cwd, we do not need to
Expand Down
9 changes: 1 addition & 8 deletions lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,7 @@ Object.defineProperty(Module, 'wrapper', {
}
});

let debuglog;
function debug(...args) {
if (!debuglog) {
debuglog = require('internal/util/debuglog').debuglog('module');
}
debuglog(...args);
}

const debug = require('internal/util/debuglog').debuglog('module');
Module._debug = deprecate(debug, 'Module._debug is deprecated.', 'DEP0077');

// Given a module name, and a list of paths to test, returns the first
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/stream_base_commons.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const kAfterAsyncWrite = Symbol('kAfterAsyncWrite');
const kHandle = Symbol('kHandle');
const kSession = Symbol('kSession');

const debug = require('util').debuglog('stream');
const debug = require('internal/util/debuglog').debuglog('stream');

function handleWriteReq(req, data, encoding) {
const { handle } = req;
Expand Down
8 changes: 1 addition & 7 deletions lib/internal/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,7 @@ const L = require('internal/linkedlist');
const PriorityQueue = require('internal/priority_queue');

const { inspect } = require('internal/util/inspect');
let debuglog;
function debug(...args) {
if (!debuglog) {
debuglog = require('internal/util/debuglog').debuglog('timer');
}
debuglog(...args);
}
const debug = require('internal/util/debuglog').debuglog('timer');

// *Must* match Environment::ImmediateInfo::Fields in src/env.h.
const kCount = 0;
Expand Down
18 changes: 17 additions & 1 deletion lib/internal/util/debuglog.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function emitWarningIfNeeded(set) {
}
}

function debuglog(set) {
function debuglogImpl(set) {
set = set.toUpperCase();
if (!debugs[set]) {
if (debugEnvRegex.test(set)) {
Expand All @@ -48,6 +48,22 @@ function debuglog(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 = {
debuglog,
initializeDebugEnv
Expand Down
9 changes: 1 addition & 8 deletions lib/internal/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,7 @@ const kOnErrorMessage = Symbol('kOnErrorMessage');
const kParentSideStdio = Symbol('kParentSideStdio');

const SHARE_ENV = Symbol.for('nodejs.worker_threads.SHARE_ENV');

let debuglog;
function debug(...args) {
if (!debuglog) {
debuglog = require('internal/util/debuglog').debuglog('worker');
}
debuglog(...args);
}
const debug = require('internal/util/debuglog').debuglog('worker');

class Worker extends EventEmitter {
constructor(filename, options = {}) {
Expand Down
9 changes: 1 addition & 8 deletions lib/internal/worker/io.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,7 @@ const {
const { Readable, Writable } = require('stream');
const EventEmitter = require('events');
const { inspect } = require('internal/util/inspect');

let debuglog;
function debug(...args) {
if (!debuglog) {
debuglog = require('internal/util/debuglog').debuglog('worker');
}
debuglog(...args);
}
const debug = require('internal/util/debuglog').debuglog('worker');

const kIncrementsPortRef = Symbol('kIncrementsPortRef');
const kName = Symbol('kName');
Expand Down
9 changes: 1 addition & 8 deletions lib/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,7 @@ const {
deprecate
} = require('internal/util');
const { ERR_INVALID_CALLBACK } = require('internal/errors').codes;

let debuglog;
function debug(...args) {
if (!debuglog) {
debuglog = require('internal/util/debuglog').debuglog('timer');
}
debuglog(...args);
}
const debug = require('internal/util/debuglog').debuglog('timer');

const {
destroyHooksExist,
Expand Down

0 comments on commit 2e4ceb5

Please sign in to comment.