summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRuben Bridgewater <ruben@bridgewater.de>2019-12-12 23:19:01 +0100
committerRuben Bridgewater <ruben@bridgewater.de>2019-12-25 11:09:16 +0100
commita03458396b35c455f01a7429456fd8f0b5d7a4f4 (patch)
tree0a513b4860263bf6d8d8bbcedeb0292c4fee8373
parentbe3c7aceba58f0a58ee4813a9894681d9c8ba56e (diff)
downloadnode-new-a03458396b35c455f01a7429456fd8f0b5d7a4f4.tar.gz
util: add colors to debuglog()
This adds colors to the passed through arguments in case the stream supports colors. The PID will also be highlighted. PR-URL: https://github.com/nodejs/node/pull/30930 Reviewed-By: James M Snell <jasnell@gmail.com>
-rw-r--r--lib/internal/util/debuglog.js8
-rw-r--r--test/sequential/test-util-debug.js38
2 files changed, 37 insertions, 9 deletions
diff --git a/lib/internal/util/debuglog.js b/lib/internal/util/debuglog.js
index 87af5de833..9834668307 100644
--- a/lib/internal/util/debuglog.js
+++ b/lib/internal/util/debuglog.js
@@ -1,6 +1,6 @@
'use strict';
-const { format } = require('internal/util/inspect');
+const { inspect, format, formatWithOptions } = require('internal/util/inspect');
// `debugs` is deliberately initialized to undefined so any call to
// debuglog() before initializeDebugEnv() is called will throw.
@@ -38,8 +38,10 @@ function debuglogImpl(set) {
const pid = process.pid;
emitWarningIfNeeded(set);
debugs[set] = function debug(...args) {
- const msg = format(...args);
- process.stderr.write(format('%s %d: %s\n', set, pid, msg));
+ const colors = process.stderr.hasColors && process.stderr.hasColors();
+ const msg = formatWithOptions({ colors }, ...args);
+ const coloredPID = inspect(pid, { colors });
+ process.stderr.write(format('%s %s: %s\n', set, coloredPID, msg));
};
} else {
debugs[set] = null;
diff --git a/test/sequential/test-util-debug.js b/test/sequential/test-util-debug.js
index c79f99ff28..f791eb8a8d 100644
--- a/test/sequential/test-util-debug.js
+++ b/test/sequential/test-util-debug.js
@@ -22,6 +22,7 @@
'use strict';
const common = require('../common');
const assert = require('assert');
+const util = require('util');
const [, , modeArgv, sectionArgv] = process.argv;
@@ -54,19 +55,36 @@ function parent() {
test('*-test', true, 'abc-test');
}
-function test(environ, shouldWrite, section) {
+function test(environ, shouldWrite, section, forceColors = false) {
let expectErr = '';
const expectOut = 'ok\n';
const spawn = require('child_process').spawn;
const child = spawn(process.execPath, [__filename, 'child', section], {
- env: Object.assign(process.env, { NODE_DEBUG: environ })
+ env: Object.assign(process.env, {
+ NODE_DEBUG: environ,
+ FORCE_COLOR: forceColors ? 'true' : 'false'
+ })
});
if (shouldWrite) {
- expectErr =
- `${section.toUpperCase()} ${child.pid}: this { is: 'a' } /debugging/\n${
- section.toUpperCase()} ${child.pid}: num=1 str=a obj={"foo":"bar"}\n`;
+ if (forceColors) {
+ const { colors, styles } = util.inspect;
+ const addCodes = (arr) => [`\x1B[${arr[0]}m`, `\x1B[${arr[1]}m`];
+ const num = addCodes(colors[styles.number]);
+ const str = addCodes(colors[styles.string]);
+ const regexp = addCodes(colors[styles.regexp]);
+ const start = `${section.toUpperCase()} ${num[0]}${child.pid}${num[1]}`;
+ const debugging = `${regexp[0]}/debugging/${regexp[1]}`;
+ expectErr =
+ `${start}: this { is: ${str[0]}'a'${str[1]} } ${debugging}\n` +
+ `${start}: num=1 str=a obj={"foo":"bar"}\n`;
+ } else {
+ const start = `${section.toUpperCase()} ${child.pid}`;
+ expectErr =
+ `${start}: this { is: 'a' } /debugging/\n` +
+ `${start}: num=1 str=a obj={"foo":"bar"}\n`;
+ }
}
let err = '';
@@ -85,12 +103,20 @@ function test(environ, shouldWrite, section) {
assert(!c);
assert.strictEqual(err, expectErr);
assert.strictEqual(out, expectOut);
+ // Run the test again, this time with colors enabled.
+ if (!forceColors) {
+ test(environ, shouldWrite, section, true);
+ }
}));
}
function child(section) {
- const util = require('util');
+ const tty = require('tty');
+ // Make sure we check for colors, no matter of the stream's default.
+ Object.defineProperty(process.stderr, 'hasColors', {
+ value: tty.WriteStream.prototype.hasColors
+ });
const debug = util.debuglog(section);
debug('this', { is: 'a' }, /debugging/);
debug('num=%d str=%s obj=%j', 1, 'a', { foo: 'bar' });