-
Notifications
You must be signed in to change notification settings - Fork 944
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
debug with chrome dev tool issue #483
Comments
This seems to be an encoding error not related to |
although It's working on linux console but not in chrome console. as you mention in readme
and my chrome support
then this should work. i think debug node with chrome dev tool relativly new. improve "debug" to work with this powerful tool |
Ya I see the problem when debugging a Node app with If you can come up with any clever ways to handle that then I'm all ears. |
Wrote a hack for you - had to sniff around a lot of C++. Should work in any version of Node with adequate API support, failing back silently to the current functionality. No dependencies. function formatInspector(...args) {
// TODO format the message for the web inspector
return `INSPECTOR: ${JSON.stringify(args)}`;
}
function formatNodeConsole(...args) {
// TODO format the message for the Node.js console
return `CONSOLE: ${require('util').inspect(args, {colors: true})}`;
}
function hookConsole(inspectFormatter, nodeFormatter) {
// Back up the current console configuration for
// failing back in the event of an error
const defaultConsole = console;
const defaultStdout = console._stdout;
const defaultStderr = console._stderr;
try {
// Pull in the config binding, which exposes internal
// configuration - namely the inspectorEnabled flag,
// which is true if either `--inspect` or `--inspect-brk`
// were passed.
// https://github.com/nodejs/node/blob/5e7c69716ecbb7a0ebceaed7cc1c6847f9bc5058/src/node_config.cc#L114-L116
const configBinding = process.binding('config');
if (!configBinding.debugOptions.inspectorEnabled) {
return console.log;
}
const inspectorConsole = global.console;
// Create a new console object using the configured stdout and stderr streams.
// If no errors occur, this is what the global `console` object becomes.
//
// This is because the console object has been wrapped in inspector mode
// to output to both the Console object's configured stdout/stderr
// as well as the web inspector. There is no other (known) way
// to get a handle to a stream for the web inspector's output,
// so instead we hijack the one the current inspector instance provides and
// 'mute' it from outputting to stdout/stderr. This gives us an effectively
// exclusive interface to the web inspector session.
const vanillaConsole = new (require('console')).Console(console._stdout, console._stderr);
// Try to remove the global console object.
// Simply re-assigning does not work as I don't think
// the descriptor allows for overwriting, but _does_
// allow deletion.
if (!(delete global.console)) {
return console.log;
}
// Replace the global console with the new vanilla console.
global.console = vanillaConsole;
// Create the black hole stream
const nullStream = new (require('stream').Writable)();
nullStream._write = () => {};
// Mute the wrapped Console object's stdout/stderr (see above comment)
inspectorConsole._stdout = nullStream;
inspectorConsole._stderr = nullStream;
// Create a wrapper to format/output to the node console
const nodeOutput = (...args) => console.log(nodeFormatter(...args));
// Create a wrapper to format/output to the web inspector console
const inspectorOutput = (...args) => inspectorConsole.log(inspectFormatter(...args));
// This pulls in the internal inspector binding that exposes the consoleCall function,
// which will call the first argument (a function) if the web inspector is /connected/
// and will always call the second function. The empty object is used for what appears
// to be a semaphore-like function, but we don't need any of that so an empty object is fine.
//
// https://github.com/nodejs/node/blob/60f2fa9a8b050563d2b7f3642a84ff192bd362a6/src/inspector_agent.cc#L335-L370
//
// The return value is a single function call that will call inspectorOutput() (above) if the inspector
// is connected and has an active session, and then it'll call nodeOutput() after, and will forward
// all of the arguments to those functions.
const inspectorBinding = process.binding('inspector');
return inspectorBinding.consoleCall.bind(inspectorConsole, nodeOutput, inspectorOutput, {});
} catch (err) {
// Try to restore the original console (should always work but this protects against
// strange configurations of the console descriptor).
try {
delete global.console;
} catch (err) {}
global.console = defaultConsole;
global.console._stdout = defaultStdout;
global.console._stderr = defaultStderr;
return console.log;
}
}
const debugWriter = hookConsole(formatInspector, formatNodeConsole); // Default is node console in this case
console.log('normal console log');
debugWriter('debug log message'); Only side effect is that it doesn't forward the vanilla |
This is very cool @Qix- this will make me want to try out |
@TooTallNate added comments explaining how it works. |
@TooTallNate did we ever merge my hack? |
I'll investigate adding this in the 5.x release. |
Holy shit. You have got to be kidding me. They really did it. Thanks for bringing this to my attention @ricardobeat! Okay well case closed then... 😅 |
@Qix- Hi, I have the log problem with chrome dev tool when I use this debug module. Initially, I could not get the log in chrome dev tool when I use this debug module. Then I use the I use the bind like below.
I use
It shows like below, in VS Code terminal. It shows like below, in Chrome DevTools. How to solve the displaying color of the label and the time, in Chrome Devtools? My version of the debug is Another question is about the message of the where is the log from. For example, in the above screenshot from Chrome DevTools. Thanks in advance! |
That's expected. There's a nasty hack to fix that, but you're looking at output from a terminal app in the developer tools. There's nothing simple we can do. |
2.6.8 |
4.3.4 |
debug @latest
i hit with follow with node
console look like
anyone can help me ?
The text was updated successfully, but these errors were encountered: