Skip to content

Commit

Permalink
console,doc: add inspector console object
Browse files Browse the repository at this point in the history
Also, remove undocumented (and unusable without inspector) methods from
the global console object.

Rather than having the undocumented methods on the global console, these
methods are now available on the console.inspector object.

Fixes: nodejs#12675
  • Loading branch information
bengl committed Sep 27, 2017
1 parent 150b8f7 commit 2a08479
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
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;
}

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');
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);
}

0 comments on commit 2a08479

Please sign in to comment.