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: https://github.com/nodejs/node/pull/13841
Fixes: https://github.com/nodejs/node/issues/13728
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Vse Mozhet Byt 2017-06-21 16:37:42 +03:00
parent 0ba74dbcc6
commit 3b0e800f18
2 changed files with 31 additions and 21 deletions

View File

@ -147,11 +147,13 @@ exports.deprecate = internalUtil.deprecate;
var debugs = {}; var debugs = {};
var debugEnviron; var debugEnviron;
exports.debuglog = function(set) { exports.debuglog = function(set) {
if (debugEnviron === undefined) if (debugEnviron === undefined) {
debugEnviron = process.env.NODE_DEBUG || ''; debugEnviron = new Set(
(process.env.NODE_DEBUG || '').split(',').map((s) => s.toUpperCase()));
}
set = set.toUpperCase(); set = set.toUpperCase();
if (!debugs[set]) { if (!debugs[set]) {
if (new RegExp(`\\b${set}\\b`, 'i').test(debugEnviron)) { if (debugEnviron.has(set)) {
var pid = process.pid; var pid = process.pid;
debugs[set] = function() { debugs[set] = function() {
var msg = exports.format.apply(exports, arguments); var msg = exports.format.apply(exports, arguments);

View File

@ -23,34 +23,42 @@
const common = require('../common'); const common = require('../common');
const assert = require('assert'); const assert = require('assert');
if (process.argv[2] === 'child') const [, , modeArgv, sectionArgv] = process.argv;
child();
if (modeArgv === 'child')
child(sectionArgv);
else else
parent(); parent();
function parent() { function parent() {
test('foo,tud,bar', true); test('foo,tud,bar', true, 'tud');
test('foo,tud', true); test('foo,tud', true, 'tud');
test('tud,bar', true); test('tud,bar', true, 'tud');
test('tud', true); test('tud', true, 'tud');
test('foo,bar', false); test('foo,bar', false, 'tud');
test('', false); test('', false, 'tud');
test('###', true, '###');
test('hi:)', true, 'hi:)');
test('f$oo', true, 'f$oo');
test('f$oo', false, 'f.oo');
test('no-bar-at-all', false, 'bar');
} }
function test(environ, shouldWrite) { function test(environ, shouldWrite, section) {
let expectErr = ''; let expectErr = '';
if (shouldWrite) {
expectErr = 'TUD %PID%: this { is: \'a\' } /debugging/\n' +
'TUD %PID%: number=1234 string=asdf obj={"foo":"bar"}\n';
}
const expectOut = 'ok\n'; const expectOut = 'ok\n';
const spawn = require('child_process').spawn; const spawn = require('child_process').spawn;
const child = spawn(process.execPath, [__filename, 'child'], { const child = spawn(process.execPath, [__filename, 'child', section], {
env: Object.assign(process.env, { NODE_DEBUG: environ }) env: Object.assign(process.env, { NODE_DEBUG: environ })
}); });
expectErr = expectErr.split('%PID%').join(child.pid); if (shouldWrite) {
expectErr =
`${section.toUpperCase()} ${child.pid}: this { is: 'a' } /debugging/\n${
section.toUpperCase()} ${child.pid}: num=1 str=a obj={"foo":"bar"}\n`;
}
let err = ''; let err = '';
child.stderr.setEncoding('utf8'); child.stderr.setEncoding('utf8');
@ -72,10 +80,10 @@ function test(environ, shouldWrite) {
} }
function child() { function child(section) {
const util = require('util'); const util = require('util');
const debug = util.debuglog('tud'); const debug = util.debuglog(section);
debug('this', { is: 'a' }, /debugging/); debug('this', { is: 'a' }, /debugging/);
debug('number=%d string=%s obj=%j', 1234, 'asdf', { foo: 'bar' }); debug('num=%d str=%s obj=%j', 1, 'a', { foo: 'bar' });
console.log('ok'); console.log('ok');
} }