Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util: access process states lazily in debuglog #27281

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,13 +44,12 @@ const {
} = require('internal/process/execution');

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

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)) {
Copy link
Member Author

@joyeecheung joyeecheung Apr 17, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that since process.env.NODE_DEBUG is saved above during pre-execution, even though the saved value is accessed lazily, it's still saved as soon as possible.

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