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:
Joyee Cheung 2019-04-17 23:45:53 +08:00
parent 49ee010005
commit 2e4ceb5747
No known key found for this signature in database
GPG key ID: 92B78A53C8303B8D
9 changed files with 25 additions and 51 deletions

View file

@ -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,