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 debuglog show up in --inspect logs #41884

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 0 additions & 2 deletions lib/internal/bootstrap/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,6 @@ function createGlobalConsole(consoleFromVM) {
const inspector = require('internal/util/inspector');
// This will be exposed by `require('inspector').console` later.
inspector.consoleFromVM = consoleFromVM;
// TODO(joyeecheung): postpone this until the first time inspector
// is activated.
inspector.wrapConsole(consoleFromNode, consoleFromVM);
const { setConsoleExtensionInstaller } = internalBinding('inspector');
// Setup inspector command line API.
Expand Down
15 changes: 8 additions & 7 deletions lib/internal/util/debuglog.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ const {
StringPrototypeToUpperCase,
} = primordials;

const { inspect, format, formatWithOptions } = require('internal/util/inspect');

// `debugImpls` and `testEnabled` are deliberately not initialized so any call
// to `debuglog()` before `initializeDebugEnv()` is called will throw.
let debugImpls;
Expand Down Expand Up @@ -51,11 +49,14 @@ function debuglogImpl(enabled, set) {
if (enabled) {
const pid = process.pid;
emitWarningIfNeeded(set);
debugImpls[set] = function debug(...args) {
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));
debugImpls[set] = function debug(maybeFmt, ...args) {
if (typeof maybeFmt === 'string') {
// eslint-disable-next-line
console.error(`%s %o: ${maybeFmt}`, set, pid, ...args);
} else {
// eslint-disable-next-line
console.error('%s %o:', set, pid, maybeFmt, ...args);
}
};
} else {
debugImpls[set] = noop;
Expand Down
37 changes: 30 additions & 7 deletions test/sequential/test-util-debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,23 +100,46 @@ function test(environ, shouldWrite, section, forceColors = false) {
});

child.on('close', common.mustCall((c) => {
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);
try {
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);
}
} catch (e) {
console.error('FAILED PERMUTATION:', { environ, shouldWrite, section, forceColors });
console.error('COMMAND:', Object.entries({
NODE_DEBUG: environ,
FORCE_COLOR: forceColors ? 'true' : ''
}).reduce(
(acc, [k, v]) => { acc.push(`${k}=${JSON.stringify(v)}`); return acc; },
[]
).join(' '), 'node', [__filename, 'child', JSON.stringify(section)].join(' '));
throw e;
}
}));
}


function child(section) {
const tty = require('tty');
// Make sure we check for colors, no matter of the stream's default.

const forceColors = process.env.FORCE_COLOR === 'true';
Object.defineProperty(process.stderr, 'hasColors', {
value: tty.WriteStream.prototype.hasColors
});
Object.defineProperty(process.stderr, 'isTTY', {
value: forceColors,
});
Object.defineProperty(process.stdout, 'hasColors', {
value: tty.WriteStream.prototype.hasColors
});
Object.defineProperty(process.stdout, 'isTTY', {
value: forceColors,
});

// eslint-disable-next-line no-restricted-syntax
const debug = util.debuglog(section, common.mustCall((cb) => {
assert.strictEqual(typeof cb, 'function');
Expand Down