summaryrefslogtreecommitdiff
path: root/lib/internal/util/debuglog.js
diff options
context:
space:
mode:
authorJoyee Cheung <joyeec9h3@gmail.com>2019-04-17 23:45:53 +0800
committerJoyee Cheung <joyeec9h3@gmail.com>2019-04-20 00:30:38 +0800
commit2e4ceb5747b0e5d1bccd79837de7731c4030dc48 (patch)
tree0088f5fb0fb5b4dd1eb80d7186f889dee4864f1f /lib/internal/util/debuglog.js
parent49ee0100059ef252c3dd2372f9b3807a60cd80f8 (diff)
downloadandroid-node-v8-2e4ceb5747b0e5d1bccd79837de7731c4030dc48.tar.gz
android-node-v8-2e4ceb5747b0e5d1bccd79837de7731c4030dc48.tar.bz2
android-node-v8-2e4ceb5747b0e5d1bccd79837de7731c4030dc48.zip
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>
Diffstat (limited to 'lib/internal/util/debuglog.js')
-rw-r--r--lib/internal/util/debuglog.js18
1 files changed, 17 insertions, 1 deletions
diff --git a/lib/internal/util/debuglog.js b/lib/internal/util/debuglog.js
index e92f3ad2bd..11de25b6b4 100644
--- a/lib/internal/util/debuglog.js
+++ b/lib/internal/util/debuglog.js
@@ -31,7 +31,7 @@ function emitWarningIfNeeded(set) {
}
}
-function debuglog(set) {
+function debuglogImpl(set) {
set = set.toUpperCase();
if (!debugs[set]) {
if (debugEnvRegex.test(set)) {
@@ -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