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

console,doc: add inspector console object #15579

Closed
wants to merge 1 commit into from
Closed
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
20 changes: 20 additions & 0 deletions doc/api/console.md
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,26 @@ added: v0.1.100

The `console.warn()` function is an alias for [`console.error()`][].

### console.inspector
<!-- YAML
added: REPLACEME
-->

V8 contexts provide a `console` global object, but by default it is only useful
for sending console messages to attached inspectors. This is provided as the
`inspector` property of the global `console` object (but not of other instances
of `Console`).

For example, to send a log message, `'hello'` to an attached inspector console:

```js
console.inspector.log('hello');
// 'hello' appears in inspector console, but not in node's stdout
```

When an inspector is connected, logging methods on the global `console` object
will also send messages to the inspector console.

[`console.error()`]: #console_console_error_data_args
[`console.group()`]: #console_console_group_label
[`console.log()`]: #console_console_log_data_args
Expand Down
6 changes: 1 addition & 5 deletions lib/internal/bootstrap_node.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,11 +344,7 @@
wrappedConsole[key],
config);
}
for (const key of Object.keys(originalConsole)) {
if (wrappedConsole.hasOwnProperty(key))
continue;
wrappedConsole[key] = originalConsole[key];
}
wrappedConsole.inspector = originalConsole;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would just completely ignore the dummy inspector methods, i.e. do not expose them at all.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the inspector methods are not provided, users don't have a way of sending any of the messages that aren't also provided by Console.prototype to the inspector console.

I had originally thought of exposing something like require('inspector').console, but I'm not sure that's something I can set up at bootstrap_node.js time, so console.inspector seemed to be a good second option.

Copy link
Member

@TimothyGu TimothyGu Sep 24, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bengl I understand that implication. However, I feel it's ugly from a user experience standpoint to require users to use console.inspector ? console.inspector.table : console.table in polymorphic code: either it should work the way it does everywhere, or it shouldn't.

Also, these console methods only became exposed in 8.0.0, so there isn't much compatibility concern.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For certain functions that inherently do not deal with stdio, like console.markTimeline, console.profile, console.profileEnd, console.timeStamp, console.timeline, and console.timelineEnd, I think we should just keep them as they are and maybe document them. There isn't any work needed by Node.js' Console to "support" those methods because they are no-ops in the console anyway.

That leaves console.debug, console.dirxml, and console.table truly unimplemented Console methods.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TimothyGu Is that a "-1" opposition or more a "-0"?

}

function setupProcessFatal() {
Expand Down
14 changes: 14 additions & 0 deletions test/parallel/test-console.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const vm = require('vm');

assert.ok(process.stdout.writable);
assert.ok(process.stderr.writable);
Expand Down Expand Up @@ -187,3 +188,16 @@ common.hijackStderr(common.mustCall(function(data) {
assert.strictEqual(data.includes('no such label'), true);
});
}));

const pristineConsole = vm.runInNewContext('this.console');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you should skip if require('inspector').url() !== undefined?

for (const name in console.inspector) {

// No inspector-only functions are available on the global console
if (name in console) {
assert.notStrictEqual(console[name], console.inspector[name]);
}

// console.inspector should be the same as a pristine console object from a v8
// context.
assert(name in pristineConsole);
}