Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util: make util.debuglog() consistent with doc #13841

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
}