2017-01-03 22:16:48 +01:00
|
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
|
//
|
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
|
// following conditions:
|
|
|
|
|
//
|
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
|
//
|
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
|
2014-11-22 16:59:48 +01:00
|
|
|
|
'use strict';
|
|
|
|
|
|
2017-02-13 17:28:51 +01:00
|
|
|
|
const errors = require('internal/errors');
|
2015-01-21 17:36:59 +01:00
|
|
|
|
const util = require('util');
|
2017-04-26 21:42:18 +02:00
|
|
|
|
const kCounts = Symbol('counts');
|
2010-11-30 20:18:02 +01:00
|
|
|
|
|
2017-08-18 07:41:14 +02:00
|
|
|
|
// Track amount of indentation required via `console.group()`.
|
|
|
|
|
const kGroupIndent = Symbol('groupIndent');
|
|
|
|
|
|
2016-11-22 18:21:43 +01:00
|
|
|
|
function Console(stdout, stderr, ignoreErrors = true) {
|
2012-08-24 22:12:30 +02:00
|
|
|
|
if (!(this instanceof Console)) {
|
2016-11-22 18:21:43 +01:00
|
|
|
|
return new Console(stdout, stderr, ignoreErrors);
|
2012-08-24 22:12:30 +02:00
|
|
|
|
}
|
2015-01-29 02:05:53 +01:00
|
|
|
|
if (!stdout || typeof stdout.write !== 'function') {
|
2017-02-13 17:28:51 +01:00
|
|
|
|
throw new errors.TypeError('ERR_CONSOLE_WRITABLE_STREAM', 'stdout');
|
2012-08-24 22:12:30 +02:00
|
|
|
|
}
|
|
|
|
|
if (!stderr) {
|
|
|
|
|
stderr = stdout;
|
2016-03-10 00:26:34 +01:00
|
|
|
|
} else if (typeof stderr.write !== 'function') {
|
2017-02-13 17:28:51 +01:00
|
|
|
|
throw new errors.TypeError('ERR_CONSOLE_WRITABLE_STREAM', 'stderr');
|
2012-08-24 22:12:30 +02:00
|
|
|
|
}
|
2016-03-10 00:26:34 +01:00
|
|
|
|
|
2012-08-24 22:12:30 +02:00
|
|
|
|
var prop = {
|
|
|
|
|
writable: true,
|
|
|
|
|
enumerable: false,
|
|
|
|
|
configurable: true
|
|
|
|
|
};
|
|
|
|
|
prop.value = stdout;
|
|
|
|
|
Object.defineProperty(this, '_stdout', prop);
|
|
|
|
|
prop.value = stderr;
|
|
|
|
|
Object.defineProperty(this, '_stderr', prop);
|
2016-11-22 18:21:43 +01:00
|
|
|
|
prop.value = ignoreErrors;
|
|
|
|
|
Object.defineProperty(this, '_ignoreErrors', prop);
|
2015-01-23 02:16:36 +01:00
|
|
|
|
prop.value = new Map();
|
2012-08-24 22:12:30 +02:00
|
|
|
|
Object.defineProperty(this, '_times', prop);
|
2016-11-22 18:21:43 +01:00
|
|
|
|
prop.value = createWriteErrorHandler(stdout);
|
|
|
|
|
Object.defineProperty(this, '_stdoutErrorHandler', prop);
|
|
|
|
|
prop.value = createWriteErrorHandler(stderr);
|
|
|
|
|
Object.defineProperty(this, '_stderrErrorHandler', prop);
|
2012-08-24 22:12:30 +02:00
|
|
|
|
|
2017-04-26 21:42:18 +02:00
|
|
|
|
this[kCounts] = new Map();
|
2017-08-24 05:36:54 +02:00
|
|
|
|
|
|
|
|
|
Object.defineProperty(this, kGroupIndent, { writable: true });
|
2017-08-18 07:41:14 +02:00
|
|
|
|
this[kGroupIndent] = '';
|
2017-04-26 21:42:18 +02:00
|
|
|
|
|
2012-08-24 22:12:30 +02:00
|
|
|
|
// bind the prototype functions to this Console instance
|
2014-08-14 05:15:24 +02:00
|
|
|
|
var keys = Object.keys(Console.prototype);
|
|
|
|
|
for (var v = 0; v < keys.length; v++) {
|
|
|
|
|
var k = keys[v];
|
2012-08-24 22:12:30 +02:00
|
|
|
|
this[k] = this[k].bind(this);
|
2014-08-14 05:15:24 +02:00
|
|
|
|
}
|
2012-08-24 22:12:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-22 18:21:43 +01:00
|
|
|
|
// Make a function that can serve as the callback passed to `stream.write()`.
|
|
|
|
|
function createWriteErrorHandler(stream) {
|
|
|
|
|
return (err) => {
|
|
|
|
|
// 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 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
|
|
|
|
|
// removed after the event, non-console.* writes won’t be affected.
|
2017-11-05 13:26:27 +01:00
|
|
|
|
// we are only adding noop if there is no one else listening for 'error'
|
|
|
|
|
if (stream.listenerCount('error') === 0) {
|
|
|
|
|
stream.on('error', noop);
|
|
|
|
|
}
|
2016-11-22 18:21:43 +01:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-24 05:36:54 +02:00
|
|
|
|
function write(ignoreErrors, stream, string, errorhandler, groupIndent) {
|
|
|
|
|
if (groupIndent.length !== 0) {
|
|
|
|
|
if (string.indexOf('\n') !== -1) {
|
|
|
|
|
string = string.replace(/\n/g, `\n${groupIndent}`);
|
|
|
|
|
}
|
|
|
|
|
string = groupIndent + string;
|
|
|
|
|
}
|
|
|
|
|
string += '\n';
|
|
|
|
|
|
2016-11-22 18:21:43 +01:00
|
|
|
|
if (!ignoreErrors) 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
|
|
|
|
|
// handle both situations.
|
|
|
|
|
try {
|
|
|
|
|
// Add and later remove a noop error handler to catch synchronous errors.
|
|
|
|
|
stream.once('error', noop);
|
|
|
|
|
|
|
|
|
|
stream.write(string, errorhandler);
|
|
|
|
|
} catch (e) {
|
2017-08-02 00:17:06 +02:00
|
|
|
|
// 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')
|
|
|
|
|
throw e;
|
2016-11-22 18:21:43 +01:00
|
|
|
|
// Sorry, there’s no proper way to pass along the error here.
|
|
|
|
|
} finally {
|
|
|
|
|
stream.removeListener('error', noop);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-15 22:13:49 +02:00
|
|
|
|
|
|
|
|
|
// 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.
|
2016-10-12 05:34:36 +02:00
|
|
|
|
Console.prototype.log = function log(...args) {
|
2016-11-22 18:21:43 +01:00
|
|
|
|
write(this._ignoreErrors,
|
|
|
|
|
this._stdout,
|
2017-08-24 05:36:54 +02:00
|
|
|
|
util.format.apply(null, args),
|
|
|
|
|
this._stdoutErrorHandler,
|
|
|
|
|
this[kGroupIndent]);
|
2010-11-30 20:18:02 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2012-08-24 22:12:30 +02:00
|
|
|
|
Console.prototype.info = Console.prototype.log;
|
2010-11-30 20:18:02 +01:00
|
|
|
|
|
|
|
|
|
|
2016-10-12 05:34:36 +02:00
|
|
|
|
Console.prototype.warn = function warn(...args) {
|
2016-11-22 18:21:43 +01:00
|
|
|
|
write(this._ignoreErrors,
|
|
|
|
|
this._stderr,
|
2017-08-24 05:36:54 +02:00
|
|
|
|
util.format.apply(null, args),
|
|
|
|
|
this._stderrErrorHandler,
|
|
|
|
|
this[kGroupIndent]);
|
2010-11-30 20:18:02 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2012-08-24 22:12:30 +02:00
|
|
|
|
Console.prototype.error = Console.prototype.warn;
|
2010-11-30 20:18:02 +01:00
|
|
|
|
|
|
|
|
|
|
2016-10-12 05:34:36 +02:00
|
|
|
|
Console.prototype.dir = function dir(object, options) {
|
2017-07-11 02:55:21 +02:00
|
|
|
|
options = Object.assign({ customInspect: false }, options);
|
2017-02-17 18:15:09 +01:00
|
|
|
|
write(this._ignoreErrors,
|
|
|
|
|
this._stdout,
|
2017-08-24 05:36:54 +02:00
|
|
|
|
util.inspect(object, options),
|
|
|
|
|
this._stdoutErrorHandler,
|
|
|
|
|
this[kGroupIndent]);
|
2010-11-30 20:18:02 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2017-08-05 20:53:32 +02:00
|
|
|
|
Console.prototype.time = function time(label = 'default') {
|
|
|
|
|
// Coerces everything other than Symbol to a string
|
|
|
|
|
label = `${label}`;
|
2015-10-29 20:22:12 +01:00
|
|
|
|
this._times.set(label, process.hrtime());
|
2010-11-30 20:18:02 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2017-08-05 20:53:32 +02:00
|
|
|
|
Console.prototype.timeEnd = function timeEnd(label = 'default') {
|
|
|
|
|
// Coerces everything other than Symbol to a string
|
|
|
|
|
label = `${label}`;
|
2016-04-21 18:02:27 +02:00
|
|
|
|
const time = this._times.get(label);
|
2012-04-29 15:17:16 +02:00
|
|
|
|
if (!time) {
|
2016-04-21 18:02:27 +02:00
|
|
|
|
process.emitWarning(`No such label '${label}' for console.timeEnd()`);
|
|
|
|
|
return;
|
2012-04-29 15:17:16 +02:00
|
|
|
|
}
|
2015-10-07 19:58:39 +02:00
|
|
|
|
const duration = process.hrtime(time);
|
|
|
|
|
const ms = duration[0] * 1000 + duration[1] / 1e6;
|
2015-10-29 20:22:12 +01:00
|
|
|
|
this.log('%s: %sms', label, ms.toFixed(3));
|
2015-10-28 13:56:34 +01:00
|
|
|
|
this._times.delete(label);
|
2010-11-30 20:18:02 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2016-04-15 22:13:49 +02:00
|
|
|
|
Console.prototype.trace = function trace(...args) {
|
2017-06-17 15:20:39 +02:00
|
|
|
|
const err = {
|
|
|
|
|
name: 'Trace',
|
|
|
|
|
message: util.format.apply(null, args)
|
|
|
|
|
};
|
2014-11-22 16:59:48 +01:00
|
|
|
|
Error.captureStackTrace(err, trace);
|
2012-08-24 22:12:30 +02:00
|
|
|
|
this.error(err.stack);
|
2010-11-30 20:18:02 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2016-10-12 05:34:36 +02:00
|
|
|
|
Console.prototype.assert = function assert(expression, ...args) {
|
2010-12-02 02:29:11 +01:00
|
|
|
|
if (!expression) {
|
2016-04-15 22:13:49 +02:00
|
|
|
|
require('assert').ok(false, util.format.apply(null, args));
|
2010-11-30 20:18:02 +01:00
|
|
|
|
}
|
|
|
|
|
};
|
2012-08-24 22:12:30 +02:00
|
|
|
|
|
2017-04-26 21:42:18 +02:00
|
|
|
|
// Defined by: https://console.spec.whatwg.org/#clear
|
|
|
|
|
Console.prototype.clear = function clear() {
|
|
|
|
|
// It only makes sense to clear if _stdout is a TTY.
|
|
|
|
|
// Otherwise, do nothing.
|
|
|
|
|
if (this._stdout.isTTY) {
|
|
|
|
|
// The require is here intentionally to avoid readline being
|
|
|
|
|
// required too early when console is first loaded.
|
|
|
|
|
const { cursorTo, clearScreenDown } = require('readline');
|
|
|
|
|
cursorTo(this._stdout, 0, 0);
|
|
|
|
|
clearScreenDown(this._stdout);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Defined by: https://console.spec.whatwg.org/#count
|
|
|
|
|
Console.prototype.count = function count(label = 'default') {
|
|
|
|
|
// Ensures that label is a string, and only things that can be
|
|
|
|
|
// coerced to strings. e.g. Symbol is not allowed
|
|
|
|
|
label = `${label}`;
|
|
|
|
|
const counts = this[kCounts];
|
|
|
|
|
let count = counts.get(label);
|
|
|
|
|
if (count === undefined)
|
|
|
|
|
count = 1;
|
|
|
|
|
else
|
|
|
|
|
count++;
|
|
|
|
|
counts.set(label, count);
|
|
|
|
|
this.log(`${label}: ${count}`);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Not yet defined by the https://console.spec.whatwg.org, but
|
|
|
|
|
// proposed to be added and currently implemented by Edge. Having
|
|
|
|
|
// the ability to reset counters is important to help prevent
|
|
|
|
|
// the counter from being a memory leak.
|
|
|
|
|
Console.prototype.countReset = function countReset(label = 'default') {
|
|
|
|
|
const counts = this[kCounts];
|
|
|
|
|
counts.delete(`${label}`);
|
|
|
|
|
};
|
2012-08-24 22:12:30 +02:00
|
|
|
|
|
2017-08-18 07:41:14 +02:00
|
|
|
|
Console.prototype.group = function group(...data) {
|
|
|
|
|
if (data.length > 0) {
|
|
|
|
|
this.log(...data);
|
|
|
|
|
}
|
|
|
|
|
this[kGroupIndent] += ' ';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Console.prototype.groupCollapsed = Console.prototype.group;
|
|
|
|
|
|
|
|
|
|
Console.prototype.groupEnd = function groupEnd() {
|
|
|
|
|
this[kGroupIndent] =
|
|
|
|
|
this[kGroupIndent].slice(0, this[kGroupIndent].length - 2);
|
|
|
|
|
};
|
|
|
|
|
|
2012-08-24 22:12:30 +02:00
|
|
|
|
module.exports = new Console(process.stdout, process.stderr);
|
|
|
|
|
module.exports.Console = Console;
|
2016-11-22 18:21:43 +01:00
|
|
|
|
|
|
|
|
|
function noop() {}
|