Skip to content

Commit

Permalink
Update util.debuglog to Node 10.4.0.
Browse files Browse the repository at this point in the history
  • Loading branch information
goto-bus-stop committed May 1, 2019
1 parent f090e96 commit 6b82825
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 32 deletions.
74 changes: 46 additions & 28 deletions test/node/debug.js
Expand Up @@ -19,68 +19,86 @@
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

'use strict';
var assert = require('assert');
var util = require('../../');

if (process.argv[2] === 'child')
child();
var modeArgv = process.argv[2]
var sectionArgv = process.argv[3]

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');

test('test-abc', true, 'test-abc');
test('test-a', false, 'test-abc');
test('test-*', true, 'test-abc');
test('test-*c', true, 'test-abc');
test('test-*abc', true, 'test-abc');
test('abc-test', true, 'abc-test');
test('a*-test', true, 'abc-test');
test('*-test', true, 'abc-test');
}

function test(environ, shouldWrite) {
function test(environ, shouldWrite, section) {
var expectErr = '';
if (shouldWrite) {
expectErr = 'TUD %PID%: this { is: \'a\' } /debugging/\n' +
'TUD %PID%: number=1234 string=asdf obj={"foo":"bar"}\n';
}
var expectOut = 'ok\n';
var didTest = false;

var spawn = require('child_process').spawn;
var child = spawn(process.execPath, [__filename, 'child'], {
env: { NODE_DEBUG: environ }
var 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';
}

var err = '';
child.stderr.setEncoding('utf8');
child.stderr.on('data', function(c) {
child.stderr.on('data', function (c) {
err += c;
});

var out = '';
child.stdout.setEncoding('utf8');
child.stdout.on('data', function(c) {
child.stdout.on('data', function (c) {
out += c;
});

child.on('close', function(c) {
var didTest = false;
child.on('close', function (c) {
assert(!c);
assert.equal(err, expectErr);
assert.equal(out, expectOut);
assert.strictEqual(err, expectErr);
assert.strictEqual(out, expectOut);
didTest = true;
console.log('ok %j %j', environ, shouldWrite);
});

process.on('exit', function() {
process.on('exit', function () {
assert(didTest);
});
}


function child() {
var debug = util.debuglog('tud');
function child(section) {
var util = require('../../util');
var 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');
}
15 changes: 11 additions & 4 deletions util.js
Expand Up @@ -104,13 +104,20 @@ exports.deprecate = function(fn, msg) {


var debugs = {};
var debugEnviron;
var debugEnvRegex = /^$/;

if (process.env.NODE_DEBUG) {
var debugEnv = process.env.NODE_DEBUG;
debugEnv = debugEnv.replace(/[|\\{}()[\]^$+?.]/g, '\\$&')
.replace(/\*/g, '.*')
.replace(/,/g, '$|^')
.toUpperCase();
debugEnvRegex = new RegExp('^' + debugEnv + '$', 'i');
}
exports.debuglog = function(set) {
if (isUndefined(debugEnviron))
debugEnviron = process.env.NODE_DEBUG || '';
set = set.toUpperCase();
if (!debugs[set]) {
if (new RegExp('\\b' + set + '\\b', 'i').test(debugEnviron)) {
if (debugEnvRegex.test(set)) {
var pid = process.pid;
debugs[set] = function() {
var msg = exports.format.apply(exports, arguments);
Expand Down

0 comments on commit 6b82825

Please sign in to comment.