Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
console: allow options object as constructor arg
  • Loading branch information
addaleax committed Apr 12, 2018
commit ec7741fa20d62907a8c617b974f9238610fa7493
17 changes: 11 additions & 6 deletions doc/api/console.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,22 @@ const { Console } = console;
```

### new Console(stdout[, stderr][, ignoreErrors])
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@vsemozhetbyt The doctool doesn’t seem to be able to handle this anymore, since 9a6dd07?

Copy link
Copy Markdown
Contributor

@vsemozhetbyt vsemozhetbyt Apr 9, 2018

Choose a reason for hiding this comment

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

Do you mean two consecutive headings? I cannot say why it should not. We have many of them in the bufer.md and they seem to be handled properly. Do you encounter issues with tests or doc building?

Copy link
Copy Markdown
Contributor

@vsemozhetbyt vsemozhetbyt Apr 9, 2018

Choose a reason for hiding this comment

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

Oh, sorry, now I've tested this. It seems the consecutive headings we already have contain similar parameters and they do not differ from the list parameters below. Before 9a6dd07, these differences were checked, but they did not throw, just reported to stderr. It seems we should either split the section or change the parser strictness, which may not be good for detecting differences between signatures and parameter lists.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

BTW, consecutive headings are parsed (and were parsed) as clones: the section bodies are copied and checked identically: see here and here. This is not changed in 9a6dd07, I've just make the differences throw.

Copy link
Copy Markdown
Contributor

@vsemozhetbyt vsemozhetbyt Apr 9, 2018

Copy link
Copy Markdown
Contributor

@vsemozhetbyt vsemozhetbyt Apr 12, 2018

Choose a reason for hiding this comment

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

So two headings with a similar name but different signatures do not confuse the JSON parser as far as the second signature is in sync with the parameter list below. However, it seems this is still the only case in our docs for now. And it can be a bit confusing: the first signature parameters are not openly listed (they are similar to object properties/options fields, but this is a bit non-formal way). Maybe we can split them, add an own minimal parameter list to the first signature and a link to the common description in the second section? It is a vague thing for me and it is not a strong opinion, so feel free to ignore) We still have a valid JSON (just a bit overflooded with parameter list clones) and the doc is still pretty readable in this way.

### new Console(options)
<!-- YAML
changes:
- version: v8.0.0
pr-url: https://github.com/nodejs/node/pull/9744
description: The `ignoreErrors` option was introduced.
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/19372
description: The `Console` constructor now supports an `options` argument.
-->

* `stdout` {stream.Writable}
* `stderr` {stream.Writable}
* `ignoreErrors` {boolean} Ignore errors when writing to the underlying streams.
Defaults to `true`.
* `options` {Object}
* `stdout` {stream.Writable}
* `stderr` {stream.Writable}
* `ignoreErrors` {boolean} Ignore errors when writing to the underlying
streams. **Default:** `true`.

Creates a new `Console` with one or two writable stream instances. `stdout` is a
writable stream to print log or info output. `stderr` is used for warning or
Expand All @@ -99,7 +104,7 @@ error output. If `stderr` is not provided, `stdout` is used for `stderr`.
const output = fs.createWriteStream('./stdout.log');
const errorOutput = fs.createWriteStream('./stderr.log');
// custom simple logger
const logger = new Console(output, errorOutput);
const logger = new Console({ stdout: output, stderr: errorOutput });
// use it like console
const count = 5;
logger.log('count: %d', count);
Expand All @@ -110,7 +115,7 @@ The global `console` is a special `Console` whose output is sent to
[`process.stdout`][] and [`process.stderr`][]. It is equivalent to calling:

```js
new Console(process.stdout, process.stderr);
new Console({ stdout: process.stdout, stderr: process.stderr });
```

### console.assert(value[, ...message])
Expand Down
27 changes: 21 additions & 6 deletions lib/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,28 @@ const {
// Track amount of indentation required via `console.group()`.
const kGroupIndent = Symbol('groupIndent');

function Console(stdout, stderr, ignoreErrors = true) {
function Console(options /* or: stdout, stderr, ignoreErrors = true */) {
if (!(this instanceof Console)) {
return new Console(stdout, stderr, ignoreErrors);
return new Console(...arguments);
}

let stdout, stderr, ignoreErrors;
if (options && typeof options.write !== 'function') {
({
stdout,
stderr = stdout,
ignoreErrors = true
} = options);
} else {
stdout = options;
stderr = arguments[1];
ignoreErrors = arguments[2] === undefined ? true : arguments[2];
}

if (!stdout || typeof stdout.write !== 'function') {
throw new ERR_CONSOLE_WRITABLE_STREAM('stdout');
}
if (!stderr) {
stderr = stdout;
} else if (typeof stderr.write !== 'function') {
if (!stderr || typeof stderr.write !== 'function') {
throw new ERR_CONSOLE_WRITABLE_STREAM('stderr');
}

Expand Down Expand Up @@ -369,7 +381,10 @@ Console.prototype.table = function(tabularData, properties) {
return final(keys, values);
};

module.exports = new Console(process.stdout, process.stderr);
module.exports = new Console({
stdout: process.stdout,
stderr: process.stderr
});
module.exports.Console = Console;

function noop() {}