Skip to content

console: minor console refactoring #17707

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

Closed
wants to merge 6 commits 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
59 changes: 59 additions & 0 deletions benchmark/misc/arguments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
'use strict';

const { createBenchmark } = require('../common.js');
const { format } = require('util');

const methods = [
'restAndSpread',
'argumentsAndApply',
'restAndApply',
'predefined'
];

const bench = createBenchmark(main, {
method: methods,
n: [1e6]
});

function usingRestAndSpread(...args) {
format(...args);
}

function usingRestAndApply(...args) {
format.apply(null, args);
}

function usingArgumentsAndApply() {
format.apply(null, arguments);
}

function usingPredefined() {
format('part 1', 'part', 2, 'part 3', 'part', 4);
}

function main({ n, method, args }) {
var fn;
switch (method) {
// '' is a default case for tests
case '':
case 'restAndSpread':
fn = usingRestAndSpread;
break;
case 'restAndApply':
fn = usingRestAndApply;
break;
case 'argumentsAndApply':
fn = usingArgumentsAndApply;
break;
case 'predefined':
fn = usingPredefined;
break;
default:
throw new Error(`Unexpected method "${method}"`);
}

bench.start();
for (var i = 0; i < n; i++)
fn('part 1', 'part', 2, 'part 3', 'part', 4);
bench.end(n);
}
126 changes: 0 additions & 126 deletions benchmark/misc/console.js

This file was deleted.

5 changes: 0 additions & 5 deletions doc/api/console.md
Original file line number Diff line number Diff line change
Expand Up @@ -402,11 +402,6 @@ console.timeEnd('100-elements');
// prints 100-elements: 225.438ms
```

*Note*: As of Node.js v6.0.0, `console.timeEnd()` deletes the timer to avoid
leaking it. On older versions, the timer persisted. This allowed
`console.timeEnd()` to be called multiple times for the same label. This
functionality was unintended and is no longer supported.

### console.trace([message][, ...args])
<!-- YAML
added: v0.1.104
Expand Down
42 changes: 18 additions & 24 deletions lib/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ const kCounts = Symbol('counts');
// Track amount of indentation required via `console.group()`.
const kGroupIndent = Symbol('groupIndent');

let MAX_STACK_MESSAGE;

function Console(stdout, stderr, ignoreErrors = true) {
if (!(this instanceof Console)) {
return new Console(stdout, stderr, ignoreErrors);
Expand All @@ -50,7 +52,7 @@ function Console(stdout, stderr, ignoreErrors = true) {
Object.defineProperty(this, '_stdout', prop);
prop.value = stderr;
Object.defineProperty(this, '_stderr', prop);
prop.value = ignoreErrors;
prop.value = Boolean(ignoreErrors);
Object.defineProperty(this, '_ignoreErrors', prop);
prop.value = new Map();
Object.defineProperty(this, '_times', prop);
Expand Down Expand Up @@ -78,7 +80,7 @@ function createWriteErrorHandler(stream) {
// This conditional evaluates to true if and only if there was an error
// that was not already emitted (which happens when the _write callback
// is invoked asynchronously).
if (err && !stream._writableState.errorEmitted) {
if (err !== null && !stream._writableState.errorEmitted) {
// If there was an error, it will be emitted on `stream` as
// an `error` event. Adding a `once` listener will keep that error
// from becoming an uncaught exception, but since the handler is
Expand All @@ -100,7 +102,7 @@ function write(ignoreErrors, stream, string, errorhandler, groupIndent) {
}
string += '\n';

if (!ignoreErrors) return stream.write(string);
if (ignoreErrors === false) return stream.write(string);

// There may be an error occurring synchronously (e.g. for files or TTYs
// on POSIX systems) or asynchronously (e.g. pipes on POSIX systems), so
Expand All @@ -111,34 +113,37 @@ function write(ignoreErrors, stream, string, errorhandler, groupIndent) {

stream.write(string, errorhandler);
} catch (e) {
if (MAX_STACK_MESSAGE === undefined) {
try {
// eslint-disable-next-line no-unused-vars
function a() { a(); }
} catch (err) {
MAX_STACK_MESSAGE = err.message;
}
}
// console is a debugging utility, so it swallowing errors is not desirable
// even in edge cases such as low stack space.
if (e.message === 'Maximum call stack size exceeded')
if (e.message === MAX_STACK_MESSAGE && e.name === 'RangeError')
throw e;
// Sorry, there’s no proper way to pass along the error here.
} finally {
stream.removeListener('error', noop);
}
}


// As of v8 5.0.71.32, the combination of rest param, template string
// and .apply(null, args) benchmarks consistently faster than using
// the spread operator when calling util.format.
Console.prototype.log = function log(...args) {
write(this._ignoreErrors,
this._stdout,
// The performance of .apply and the spread operator seems on par in V8
// 6.3 but the spread operator, unlike .apply(), pushes the elements
// onto the stack. That is, it makes stack overflows more likely.
util.format.apply(null, args),
this._stdoutErrorHandler,
this[kGroupIndent]);
};


Console.prototype.debug = Console.prototype.log;


Console.prototype.info = Console.prototype.log;

Console.prototype.dirxml = Console.prototype.log;

Console.prototype.warn = function warn(...args) {
write(this._ignoreErrors,
Expand All @@ -147,11 +152,8 @@ Console.prototype.warn = function warn(...args) {
this._stderrErrorHandler,
this[kGroupIndent]);
};


Console.prototype.error = Console.prototype.warn;


Console.prototype.dir = function dir(object, options) {
options = Object.assign({ customInspect: false }, options);
write(this._ignoreErrors,
Expand All @@ -161,17 +163,12 @@ Console.prototype.dir = function dir(object, options) {
this[kGroupIndent]);
};


Console.prototype.dirxml = Console.prototype.log;


Console.prototype.time = function time(label = 'default') {
// Coerces everything other than Symbol to a string
label = `${label}`;
this._times.set(label, process.hrtime());
};


Console.prototype.timeEnd = function timeEnd(label = 'default') {
// Coerces everything other than Symbol to a string
label = `${label}`;
Expand All @@ -186,7 +183,6 @@ Console.prototype.timeEnd = function timeEnd(label = 'default') {
this._times.delete(label);
};


Console.prototype.trace = function trace(...args) {
const err = {
name: 'Trace',
Expand All @@ -196,7 +192,6 @@ Console.prototype.trace = function trace(...args) {
this.error(err.stack);
};


Console.prototype.assert = function assert(expression, ...args) {
if (!expression) {
require('assert').ok(false, util.format.apply(null, args));
Expand Down Expand Up @@ -246,7 +241,6 @@ Console.prototype.group = function group(...data) {
}
this[kGroupIndent] += ' ';
};

Console.prototype.groupCollapsed = Console.prototype.group;

Console.prototype.groupEnd = function groupEnd() {
Expand Down