From bf5f3ace97e594230868f72141da509f489bbc8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Zasso?= Date: Mon, 11 Dec 2017 14:10:08 +0100 Subject: [PATCH 1/3] util: allow wildcards in NODE_DEBUG variable Fixes: https://github.com/nodejs/node/issues/17605 --- lib/util.js | 17 ++++++++++++++--- test/sequential/test-util-debug.js | 9 +++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/lib/util.js b/lib/util.js index 7eae9a99069d2c..2f69ddc19b63c5 100644 --- a/lib/util.js +++ b/lib/util.js @@ -233,14 +233,25 @@ function format(f) { var debugs = {}; var debugEnviron; +const regExpSpecialChars = /[|\\{}()[\]^$+?.]/g; + +function stringToRegExp(string) { + // Escape special chars except wildcard. + string = string.replace(regExpSpecialChars, '\\$&'); + // Transform wildcard to "match anything" + string = string.replace(/\*/g, '.*'); + return new RegExp(`^${string}$`); +} + function debuglog(set) { if (debugEnviron === undefined) { - debugEnviron = new Set( - (process.env.NODE_DEBUG || '').split(',').map((s) => s.toUpperCase())); + debugEnviron = Array.from(new Set( + (process.env.NODE_DEBUG || '').split(',').map((s) => s.toUpperCase()))); + debugEnviron = debugEnviron.map(stringToRegExp); } set = set.toUpperCase(); if (!debugs[set]) { - if (debugEnviron.has(set)) { + if (debugEnviron.some((name) => name.test(set))) { var pid = process.pid; debugs[set] = function() { var msg = exports.format.apply(exports, arguments); diff --git a/test/sequential/test-util-debug.js b/test/sequential/test-util-debug.js index 6e20ca49d81aea..c79f99ff283c28 100644 --- a/test/sequential/test-util-debug.js +++ b/test/sequential/test-util-debug.js @@ -43,6 +43,15 @@ function parent() { 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, section) { From a92714d73897d83481b06ce392ebd99afc9884d3 Mon Sep 17 00:00:00 2001 From: Tyler Date: Fri, 15 Dec 2017 10:39:25 +0800 Subject: [PATCH 2/3] util: refactor debuglog() from array regex to regex string --- lib/util.js | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/lib/util.js b/lib/util.js index 2f69ddc19b63c5..0a7af758f43cb3 100644 --- a/lib/util.js +++ b/lib/util.js @@ -230,28 +230,21 @@ function format(f) { return str; } -var debugs = {}; -var debugEnviron; - -const regExpSpecialChars = /[|\\{}()[\]^$+?.]/g; - -function stringToRegExp(string) { - // Escape special chars except wildcard. - string = string.replace(regExpSpecialChars, '\\$&'); - // Transform wildcard to "match anything" - string = string.replace(/\*/g, '.*'); - return new RegExp(`^${string}$`); +const debugs = {}; +let debugEnvRegex = /^$/; +if (process.env.NODE_DEBUG) { + let debugEnv = process.env.NODE_DEBUG; + debugEnv = debugEnv.replace(/[|\\{}()[\]^$+?.]/g, '\\$&') + .replace(/\*/g, '.*') + .replace(/,/g, '$|^') + .toUpperCase(); + debugEnvRegex = new RegExp(`^${debugEnv}$`, 'i'); } function debuglog(set) { - if (debugEnviron === undefined) { - debugEnviron = Array.from(new Set( - (process.env.NODE_DEBUG || '').split(',').map((s) => s.toUpperCase()))); - debugEnviron = debugEnviron.map(stringToRegExp); - } set = set.toUpperCase(); if (!debugs[set]) { - if (debugEnviron.some((name) => name.test(set))) { + if (debugEnvRegex.test(set)) { var pid = process.pid; debugs[set] = function() { var msg = exports.format.apply(exports, arguments); From 1496f3f3bd1aa952ed9ca82c1aeec6ab88af9f79 Mon Sep 17 00:00:00 2001 From: Tyler Date: Fri, 15 Dec 2017 10:39:29 +0800 Subject: [PATCH 3/3] doc: updated wildcard debuglog example on util --- doc/api/util.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/doc/api/util.md b/doc/api/util.md index 033224ba0cc16c..5502be784cd4b1 100644 --- a/doc/api/util.md +++ b/doc/api/util.md @@ -104,6 +104,19 @@ FOO 3245: hello from foo [123] where `3245` is the process id. If it is not run with that environment variable set, then it will not print anything. +The `section` supports wildcard also, for example: +```js +const util = require('util'); +const debuglog = util.debuglog('foo-bar'); + +debuglog('hi there, it\'s foo-bar [%d]', 2333); +``` + +if it is run with `NODE_DEBUG=foo*` in the environment, then it will output something like: +```txt +FOO-BAR 3257: hi there, it's foo-bar [2333] +``` + Multiple comma-separated `section` names may be specified in the `NODE_DEBUG` environment variable. For example: `NODE_DEBUG=fs,net,tls`.