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:
parent
0ba74dbcc6
commit
3b0e800f18
@ -147,11 +147,13 @@ exports.deprecate = internalUtil.deprecate;
|
||||
var debugs = {};
|
||||
var debugEnviron;
|
||||
exports.debuglog = function(set) {
|
||||
if (debugEnviron === undefined)
|
||||
debugEnviron = process.env.NODE_DEBUG || '';
|
||||
if (debugEnviron === undefined) {
|
||||
debugEnviron = new Set(
|
||||
(process.env.NODE_DEBUG || '').split(',').map((s) => s.toUpperCase()));
|
||||
}
|
||||
set = set.toUpperCase();
|
||||
if (!debugs[set]) {
|
||||
if (new RegExp(`\\b${set}\\b`, 'i').test(debugEnviron)) {
|
||||
if (debugEnviron.has(set)) {
|
||||
var pid = process.pid;
|
||||
debugs[set] = function() {
|
||||
var msg = exports.format.apply(exports, arguments);
|
||||
|
@ -23,34 +23,42 @@
|
||||
const common = require('../common');
|
||||
const assert = require('assert');
|
||||
|
||||
if (process.argv[2] === 'child')
|
||||
child();
|
||||
const [, , modeArgv, sectionArgv] = process.argv;
|
||||
|
||||
if (modeArgv === 'child')
|
||||
child(sectionArgv);
|
||||
else
|
||||
parent();
|
||||
|
||||
function parent() {
|
||||
test('foo,tud,bar', true);
|
||||
test('foo,tud', true);
|
||||
test('tud,bar', true);
|
||||
test('tud', true);
|
||||
test('foo,bar', false);
|
||||
test('', false);
|
||||
test('foo,tud,bar', true, 'tud');
|
||||
test('foo,tud', true, 'tud');
|
||||
test('tud,bar', true, 'tud');
|
||||
test('tud', true, 'tud');
|
||||
test('foo,bar', false, 'tud');
|
||||
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 = '';
|
||||
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 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 })
|
||||
});
|
||||
|
||||
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 = '';
|
||||
child.stderr.setEncoding('utf8');
|
||||
@ -72,10 +80,10 @@ function test(environ, shouldWrite) {
|
||||
}
|
||||
|
||||
|
||||
function child() {
|
||||
function child(section) {
|
||||
const util = require('util');
|
||||
const debug = util.debuglog('tud');
|
||||
const debug = util.debuglog(section);
|
||||
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');
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user