2014-11-22 15:59:48 +00:00
|
|
|
|
'use strict';
|
|
|
|
|
|
2015-01-21 16:36:59 +00:00
|
|
|
|
const util = require('util');
|
2010-11-30 19:18:02 +00:00
|
|
|
|
|
2016-11-22 17:21:43 +00:00
|
|
|
|
function Console(stdout, stderr, ignoreErrors = true) {
|
2012-08-24 20:12:30 +00:00
|
|
|
|
if (!(this instanceof Console)) {
|
2016-11-22 17:21:43 +00:00
|
|
|
|
return new Console(stdout, stderr, ignoreErrors);
|
2012-08-24 20:12:30 +00:00
|
|
|
|
}
|
2015-01-29 01:05:53 +00:00
|
|
|
|
if (!stdout || typeof stdout.write !== 'function') {
|
2012-08-24 20:12:30 +00:00
|
|
|
|
throw new TypeError('Console expects a writable stream instance');
|
|
|
|
|
}
|
|
|
|
|
if (!stderr) {
|
|
|
|
|
stderr = stdout;
|
2016-03-09 23:26:34 +00:00
|
|
|
|
} else if (typeof stderr.write !== 'function') {
|
|
|
|
|
throw new TypeError('Console expects writable stream instances');
|
2012-08-24 20:12:30 +00:00
|
|
|
|
}
|
2016-03-09 23:26:34 +00:00
|
|
|
|
|
2012-08-24 20:12:30 +00: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 17:21:43 +00:00
|
|
|
|
prop.value = ignoreErrors;
|
|
|
|
|
Object.defineProperty(this, '_ignoreErrors', prop);
|
2015-01-23 01:16:36 +00:00
|
|
|
|
prop.value = new Map();
|
2012-08-24 20:12:30 +00:00
|
|
|
|
Object.defineProperty(this, '_times', prop);
|
2016-11-22 17:21:43 +00:00
|
|
|
|
prop.value = createWriteErrorHandler(stdout);
|
|
|
|
|
Object.defineProperty(this, '_stdoutErrorHandler', prop);
|
|
|
|
|
prop.value = createWriteErrorHandler(stderr);
|
|
|
|
|
Object.defineProperty(this, '_stderrErrorHandler', prop);
|
2012-08-24 20:12:30 +00:00
|
|
|
|
|
|
|
|
|
// bind the prototype functions to this Console instance
|
2014-08-14 03:15:24 +00:00
|
|
|
|
var keys = Object.keys(Console.prototype);
|
|
|
|
|
for (var v = 0; v < keys.length; v++) {
|
|
|
|
|
var k = keys[v];
|
2012-08-24 20:12:30 +00:00
|
|
|
|
this[k] = this[k].bind(this);
|
2014-08-14 03:15:24 +00:00
|
|
|
|
}
|
2012-08-24 20:12:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-22 17:21:43 +00: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.
|
|
|
|
|
stream.once('error', noop);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function write(ignoreErrors, stream, string, errorhandler) {
|
|
|
|
|
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) {
|
|
|
|
|
// Sorry, there’s no proper way to pass along the error here.
|
|
|
|
|
} finally {
|
|
|
|
|
stream.removeListener('error', noop);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-15 20:13:49 +00: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 03:34:36 +00:00
|
|
|
|
Console.prototype.log = function log(...args) {
|
2016-11-22 17:21:43 +00:00
|
|
|
|
write(this._ignoreErrors,
|
|
|
|
|
this._stdout,
|
|
|
|
|
`${util.format.apply(null, args)}\n`,
|
|
|
|
|
this._stdoutErrorHandler);
|
2010-11-30 19:18:02 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2012-08-24 20:12:30 +00:00
|
|
|
|
Console.prototype.info = Console.prototype.log;
|
2010-11-30 19:18:02 +00:00
|
|
|
|
|
|
|
|
|
|
2016-10-12 03:34:36 +00:00
|
|
|
|
Console.prototype.warn = function warn(...args) {
|
2016-11-22 17:21:43 +00:00
|
|
|
|
write(this._ignoreErrors,
|
|
|
|
|
this._stderr,
|
|
|
|
|
`${util.format.apply(null, args)}\n`,
|
|
|
|
|
this._stderrErrorHandler);
|
2010-11-30 19:18:02 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2012-08-24 20:12:30 +00:00
|
|
|
|
Console.prototype.error = Console.prototype.warn;
|
2010-11-30 19:18:02 +00:00
|
|
|
|
|
|
|
|
|
|
2016-10-12 03:34:36 +00:00
|
|
|
|
Console.prototype.dir = function dir(object, options) {
|
2016-04-15 20:13:49 +00:00
|
|
|
|
options = Object.assign({customInspect: false}, options);
|
2017-02-17 17:15:09 +00:00
|
|
|
|
write(this._ignoreErrors,
|
|
|
|
|
this._stdout,
|
|
|
|
|
`${util.inspect(object, options)}\n`,
|
|
|
|
|
this._stdoutErrorHandler);
|
2010-11-30 19:18:02 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2016-10-12 03:34:36 +00:00
|
|
|
|
Console.prototype.time = function time(label) {
|
2015-10-29 19:22:12 +00:00
|
|
|
|
this._times.set(label, process.hrtime());
|
2010-11-30 19:18:02 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2016-10-12 03:34:36 +00:00
|
|
|
|
Console.prototype.timeEnd = function timeEnd(label) {
|
2016-04-21 16:02:27 +00:00
|
|
|
|
const time = this._times.get(label);
|
2012-04-29 13:17:16 +00:00
|
|
|
|
if (!time) {
|
2016-04-21 16:02:27 +00:00
|
|
|
|
process.emitWarning(`No such label '${label}' for console.timeEnd()`);
|
|
|
|
|
return;
|
2012-04-29 13:17:16 +00:00
|
|
|
|
}
|
2015-10-07 17:58:39 +00:00
|
|
|
|
const duration = process.hrtime(time);
|
|
|
|
|
const ms = duration[0] * 1000 + duration[1] / 1e6;
|
2015-10-29 19:22:12 +00:00
|
|
|
|
this.log('%s: %sms', label, ms.toFixed(3));
|
2015-10-28 12:56:34 +00:00
|
|
|
|
this._times.delete(label);
|
2010-11-30 19:18:02 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2016-04-15 20:13:49 +00:00
|
|
|
|
Console.prototype.trace = function trace(...args) {
|
2010-11-30 19:18:02 +00:00
|
|
|
|
// TODO probably can to do this better with V8's debug object once that is
|
|
|
|
|
// exposed.
|
2015-04-28 17:46:14 +00:00
|
|
|
|
var err = new Error();
|
2010-11-30 19:18:02 +00:00
|
|
|
|
err.name = 'Trace';
|
2016-04-15 20:13:49 +00:00
|
|
|
|
err.message = util.format.apply(null, args);
|
2014-11-22 15:59:48 +00:00
|
|
|
|
Error.captureStackTrace(err, trace);
|
2012-08-24 20:12:30 +00:00
|
|
|
|
this.error(err.stack);
|
2010-11-30 19:18:02 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2016-10-12 03:34:36 +00:00
|
|
|
|
Console.prototype.assert = function assert(expression, ...args) {
|
2010-12-02 01:29:11 +00:00
|
|
|
|
if (!expression) {
|
2016-04-15 20:13:49 +00:00
|
|
|
|
require('assert').ok(false, util.format.apply(null, args));
|
2010-11-30 19:18:02 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
2012-08-24 20:12:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = new Console(process.stdout, process.stderr);
|
|
|
|
|
module.exports.Console = Console;
|
2016-11-22 17:21:43 +00:00
|
|
|
|
|
|
|
|
|
function noop() {}
|