| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -136,6 +136,42 @@ let debuglog = util.debuglog('internals', (debug) => { | |||
| 136 | 136 | }); | |
| 137 | 137 | ``` | |
| 138 | 138 | ||
| 139 | + ### `debuglog().enabled` | ||
| 140 | + <!-- YAML | ||
| 141 | + added: REPLACEME | ||
| 142 | + --> | ||
| 143 | + | ||
| 144 | + * {boolean} | ||
| 145 | + | ||
| 146 | + The `util.debuglog().enabled` getter is used to create a test that can be used | ||
| 147 | + in conditionals based on the existence of the `NODE_DEBUG` environment variable. | ||
| 148 | + If the `section` name appears within the value of that environment variable, | ||
| 149 | + then the returned value will be `true`. If not, then the returned value will be | ||
| 150 | + `false`. | ||
| 151 | + | ||
| 152 | + ```js | ||
| 153 | + const util = require('util'); | ||
| 154 | + const enabled = util.debuglog('foo').enabled; | ||
| 155 | + if (enabled) { | ||
| 156 | + console.log('hello from foo [%d]', 123); | ||
| 157 | + } | ||
| 158 | + ``` | ||
| 159 | + | ||
| 160 | + If this program is run with `NODE_DEBUG=foo` in the environment, then it will | ||
| 161 | + output something like: | ||
| 162 | + | ||
| 163 | + ```console | ||
| 164 | + hello from foo [123] | ||
| 165 | + ``` | ||
| 166 | + | ||
| 167 | + ## `util.debug(section)` | ||
| 168 | + <!-- YAML | ||
| 169 | + added: REPLACEME | ||
| 170 | + --> | ||
| 171 | + | ||
| 172 | + Alias for `util.debuglog`. Usage allows for readability of that doesn't imply | ||
| 173 | + logging when only using `util.debuglog().enabled`. | ||
| 174 | + | ||
| 139 | 175 | ## `util.deprecate(fn, msg[, code])` | |
| 140 | 176 | <!-- YAML | |
| 141 | 177 | added: v0.8.0 | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,27 +1,34 @@ | |||
| 1 | 1 | 'use strict'; | |
| 2 | 2 | ||
| 3 | 3 | const { | |
| 4 | + FunctionPrototypeBind, | ||
| 5 | + ObjectCreate, | ||
| 6 | + ObjectDefineProperty, | ||
| 4 | 7 | RegExp, | |
| 8 | + RegExpPrototypeTest, | ||
| 9 | + StringPrototypeToUpperCase | ||
| 5 | 10 | } = primordials; | |
| 6 | 11 | ||
| 7 | 12 | const { inspect, format, formatWithOptions } = require('internal/util/inspect'); | |
| 8 | 13 | ||
| 9 | 14 | // `debugs` is deliberately initialized to undefined so any call to | |
| 10 | 15 | // debuglog() before initializeDebugEnv() is called will throw. | |
| 11 | - let debugs; | ||
| 16 | + let debugImpls; | ||
| 12 | 17 | ||
| 13 | 18 | let debugEnvRegex = /^$/; | |
| 19 | + let testEnabled; | ||
| 14 | 20 | ||
| 15 | 21 | // `debugEnv` is initial value of process.env.NODE_DEBUG | |
| 16 | 22 | function initializeDebugEnv(debugEnv) { | |
| 17 | - debugs = {}; | ||
| 23 | + debugImpls = ObjectCreate(null); | ||
| 18 | 24 | if (debugEnv) { | |
| 19 | 25 | debugEnv = debugEnv.replace(/[|\\{}()[\]^$+?.]/g, '\\$&') | |
| 20 | 26 | .replace(/\*/g, '.*') | |
| 21 | 27 | .replace(/,/g, '$|^') | |
| 22 | 28 | .toUpperCase(); | |
| 23 | 29 | debugEnvRegex = new RegExp(`^${debugEnv}$`, 'i'); | |
| 24 | 30 | } | |
| 31 | + testEnabled = FunctionPrototypeBind(RegExpPrototypeTest, null, debugEnvRegex); | ||
| 25 | 32 | } | |
| 26 | 33 | ||
| 27 | 34 | // Emits warning when user sets | |
@@ -37,41 +44,57 @@ function emitWarningIfNeeded(set) { | |||
| 37 | 44 | ||
| 38 | 45 | function noop() {} | |
| 39 | 46 | ||
| 40 | - function debuglogImpl(set) { | ||
| 41 | - set = set.toUpperCase(); | ||
| 42 | - if (debugs[set] === undefined) { | ||
| 43 | - if (debugEnvRegex.test(set)) { | ||
| 47 | + function debuglogImpl(enabled, set) { | ||
| 48 | + if (debugImpls[set] === undefined) { | ||
| 49 | + if (enabled) { | ||
| 44 | 50 | const pid = process.pid; | |
| 45 | 51 | emitWarningIfNeeded(set); | |
| 46 | - debugs[set] = function debug(...args) { | ||
| 52 | + debugImpls[set] = function debug(...args) { | ||
| 47 | 53 | const colors = process.stderr.hasColors && process.stderr.hasColors(); | |
| 48 | 54 | const msg = formatWithOptions({ colors }, ...args); | |
| 49 | 55 | const coloredPID = inspect(pid, { colors }); | |
| 50 | 56 | process.stderr.write(format('%s %s: %s\n', set, coloredPID, msg)); | |
| 51 | 57 | }; | |
| 52 | 58 | } else { | |
| 53 | - debugs[set] = noop; | ||
| 59 | + debugImpls[set] = noop; | ||
| 54 | 60 | } | |
| 55 | 61 | } | |
| 56 | - return debugs[set]; | ||
| 62 | + return debugImpls[set]; | ||
| 57 | 63 | } | |
| 58 | 64 | ||
| 59 | 65 | // debuglogImpl depends on process.pid and process.env.NODE_DEBUG, | |
| 60 | 66 | // so it needs to be called lazily in top scopes of internal modules | |
| 61 | 67 | // that may be loaded before these run time states are allowed to | |
| 62 | 68 | // be accessed. | |
| 63 | 69 | function debuglog(set, cb) { | |
| 70 | + function init() { | ||
| 71 | + set = StringPrototypeToUpperCase(set); | ||
| 72 | + enabled = testEnabled(set); | ||
| 73 | + } | ||
| 64 | 74 | let debug = (...args) => { | |
| 75 | + init(); | ||
| 65 | 76 | // Only invokes debuglogImpl() when the debug function is | |
| 66 | 77 | // called for the first time. | |
| 67 | - debug = debuglogImpl(set); | ||
| 78 | + debug = debuglogImpl(enabled, set); | ||
| 68 | 79 | if (typeof cb === 'function') | |
| 69 | 80 | cb(debug); | |
| 70 | 81 | debug(...args); | |
| 71 | 82 | }; | |
| 72 | - return (...args) => { | ||
| 73 | - debug(...args); | ||
| 83 | + let enabled; | ||
| 84 | + let test = () => { | ||
| 85 | + init(); | ||
| 86 | + test = () => enabled; | ||
| 87 | + return enabled; | ||
| 74 | 88 | }; | |
| 89 | + const logger = (...args) => debug(...args); | ||
| 90 | + ObjectDefineProperty(logger, 'enabled', { | ||
| 91 | + get() { | ||
| 92 | + return test(); | ||
| 93 | + }, | ||
| 94 | + configurable: true, | ||
| 95 | + enumerable: true | ||
| 96 | + }); | ||
| 97 | + return logger; | ||
| 75 | 98 | } | |
| 76 | 99 | ||
| 77 | 100 | module.exports = { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -239,6 +239,7 @@ module.exports = { | |||
| 239 | 239 | _exceptionWithHostPort: exceptionWithHostPort, | |
| 240 | 240 | _extend, | |
| 241 | 241 | callbackify, | |
| 242 | + debug: debuglog, | ||
| 242 | 243 | debuglog, | |
| 243 | 244 | deprecate, | |
| 244 | 245 | format, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -57,7 +57,7 @@ function parent() { | |||
| 57 | 57 | ||
| 58 | 58 | function test(environ, shouldWrite, section, forceColors = false) { | |
| 59 | 59 | let expectErr = ''; | |
| 60 | - const expectOut = 'ok\n'; | ||
| 60 | + const expectOut = shouldWrite ? 'enabled\n' : 'disabled\n'; | ||
| 61 | 61 | ||
| 62 | 62 | const spawn = require('child_process').spawn; | |
| 63 | 63 | const child = spawn(process.execPath, [__filename, 'child', section], { | |
@@ -123,5 +123,5 @@ function child(section) { | |||
| 123 | 123 | })); | |
| 124 | 124 | debug('this', { is: 'a' }, /debugging/); | |
| 125 | 125 | debug('num=%d str=%s obj=%j', 1, 'a', { foo: 'bar' }); | |
| 126 | - console.log('ok'); | ||
| 126 | + console.log(debug.enabled ? 'enabled' : 'disabled'); | ||
| 127 | 127 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments