Skip to content

Commit 3b0e800

Browse files
committed
util: make util.debuglog() consistent with doc
Previous realization produces some false positive and false negative results due to: * conflicts between unescaped user input and RegExp special characters; * conflicts between parsing with `\b` RegExp symbol and non alphanumeric characters in section names. The doc does not mention any such restrictions. PR-URL: #13841 Fixes: #13728 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 0ba74db commit 3b0e800

File tree

2 files changed

+31
-21
lines changed

2 files changed

+31
-21
lines changed

lib/util.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,13 @@ exports.deprecate = internalUtil.deprecate;
147147
var debugs = {};
148148
var debugEnviron;
149149
exports.debuglog = function(set) {
150-
if (debugEnviron === undefined)
151-
debugEnviron = process.env.NODE_DEBUG || '';
150+
if (debugEnviron === undefined) {
151+
debugEnviron = new Set(
152+
(process.env.NODE_DEBUG || '').split(',').map((s) => s.toUpperCase()));
153+
}
152154
set = set.toUpperCase();
153155
if (!debugs[set]) {
154-
if (new RegExp(`\\b${set}\\b`, 'i').test(debugEnviron)) {
156+
if (debugEnviron.has(set)) {
155157
var pid = process.pid;
156158
debugs[set] = function() {
157159
var msg = exports.format.apply(exports, arguments);

test/sequential/test-util-debug.js

+26-18
Original file line numberDiff line numberDiff line change
@@ -23,34 +23,42 @@
2323
const common = require('../common');
2424
const assert = require('assert');
2525

26-
if (process.argv[2] === 'child')
27-
child();
26+
const [, , modeArgv, sectionArgv] = process.argv;
27+
28+
if (modeArgv === 'child')
29+
child(sectionArgv);
2830
else
2931
parent();
3032

3133
function parent() {
32-
test('foo,tud,bar', true);
33-
test('foo,tud', true);
34-
test('tud,bar', true);
35-
test('tud', true);
36-
test('foo,bar', false);
37-
test('', false);
34+
test('foo,tud,bar', true, 'tud');
35+
test('foo,tud', true, 'tud');
36+
test('tud,bar', true, 'tud');
37+
test('tud', true, 'tud');
38+
test('foo,bar', false, 'tud');
39+
test('', false, 'tud');
40+
41+
test('###', true, '###');
42+
test('hi:)', true, 'hi:)');
43+
test('f$oo', true, 'f$oo');
44+
test('f$oo', false, 'f.oo');
45+
test('no-bar-at-all', false, 'bar');
3846
}
3947

40-
function test(environ, shouldWrite) {
48+
function test(environ, shouldWrite, section) {
4149
let expectErr = '';
42-
if (shouldWrite) {
43-
expectErr = 'TUD %PID%: this { is: \'a\' } /debugging/\n' +
44-
'TUD %PID%: number=1234 string=asdf obj={"foo":"bar"}\n';
45-
}
4650
const expectOut = 'ok\n';
4751

4852
const spawn = require('child_process').spawn;
49-
const child = spawn(process.execPath, [__filename, 'child'], {
53+
const child = spawn(process.execPath, [__filename, 'child', section], {
5054
env: Object.assign(process.env, { NODE_DEBUG: environ })
5155
});
5256

53-
expectErr = expectErr.split('%PID%').join(child.pid);
57+
if (shouldWrite) {
58+
expectErr =
59+
`${section.toUpperCase()} ${child.pid}: this { is: 'a' } /debugging/\n${
60+
section.toUpperCase()} ${child.pid}: num=1 str=a obj={"foo":"bar"}\n`;
61+
}
5462

5563
let err = '';
5664
child.stderr.setEncoding('utf8');
@@ -72,10 +80,10 @@ function test(environ, shouldWrite) {
7280
}
7381

7482

75-
function child() {
83+
function child(section) {
7684
const util = require('util');
77-
const debug = util.debuglog('tud');
85+
const debug = util.debuglog(section);
7886
debug('this', { is: 'a' }, /debugging/);
79-
debug('number=%d string=%s obj=%j', 1234, 'asdf', { foo: 'bar' });
87+
debug('num=%d str=%s obj=%j', 1, 'a', { foo: 'bar' });
8088
console.log('ok');
8189
}

0 commit comments

Comments
 (0)