Skip to content

Commit

Permalink
util: make util.debuglog() consistent with doc
Browse files Browse the repository at this point in the history
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: nodejs#13841
Fixes: nodejs#13728
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
vsemozhetbyt committed Jun 23, 2017
1 parent 0ba74db commit 3b0e800
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 21 deletions.
8 changes: 5 additions & 3 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
44 changes: 26 additions & 18 deletions test/sequential/test-util-debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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');
}

0 comments on commit 3b0e800

Please sign in to comment.