2014-11-22 16:59:48 +01:00
|
|
|
'use strict';
|
|
|
|
|
2015-01-21 17:36:59 +01:00
|
|
|
const util = require('util');
|
2010-11-30 20:18:02 +01:00
|
|
|
|
2012-08-24 22:12:30 +02:00
|
|
|
function Console(stdout, stderr) {
|
|
|
|
if (!(this instanceof Console)) {
|
|
|
|
return new Console(stdout, stderr);
|
|
|
|
}
|
2015-01-29 02:05:53 +01:00
|
|
|
if (!stdout || typeof stdout.write !== 'function') {
|
2012-08-24 22:12:30 +02:00
|
|
|
throw new TypeError('Console expects a writable stream instance');
|
|
|
|
}
|
|
|
|
if (!stderr) {
|
|
|
|
stderr = stdout;
|
|
|
|
}
|
|
|
|
var prop = {
|
|
|
|
writable: true,
|
|
|
|
enumerable: false,
|
|
|
|
configurable: true
|
|
|
|
};
|
|
|
|
prop.value = stdout;
|
|
|
|
Object.defineProperty(this, '_stdout', prop);
|
|
|
|
prop.value = stderr;
|
|
|
|
Object.defineProperty(this, '_stderr', 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);
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
|
|
|
Console.prototype.log = function() {
|
|
|
|
this._stdout.write(util.format.apply(this, arguments) + '\n');
|
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
|
|
|
|
|
|
|
|
2012-08-24 22:12:30 +02:00
|
|
|
Console.prototype.warn = function() {
|
|
|
|
this._stderr.write(util.format.apply(this, arguments) + '\n');
|
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
|
|
|
|
|
|
|
|
2014-06-08 11:14:14 +02:00
|
|
|
Console.prototype.dir = function(object, options) {
|
2014-06-08 12:02:54 +02:00
|
|
|
this._stdout.write(util.inspect(object, util._extend({
|
|
|
|
customInspect: false
|
|
|
|
}, options)) + '\n');
|
2010-11-30 20:18:02 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-08-24 22:12:30 +02:00
|
|
|
Console.prototype.time = function(label) {
|
2015-01-23 02:16:36 +01:00
|
|
|
this._times.set(label, Date.now());
|
2010-11-30 20:18:02 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-08-24 22:12:30 +02:00
|
|
|
Console.prototype.timeEnd = function(label) {
|
2015-01-23 02:16:36 +01:00
|
|
|
var time = this._times.get(label);
|
2012-04-29 15:17:16 +02:00
|
|
|
if (!time) {
|
|
|
|
throw new Error('No such label: ' + label);
|
|
|
|
}
|
|
|
|
var duration = Date.now() - time;
|
2012-08-24 22:12:30 +02:00
|
|
|
this.log('%s: %dms', label, duration);
|
2010-11-30 20:18:02 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-11-22 16:59:48 +01:00
|
|
|
Console.prototype.trace = function trace() {
|
2010-11-30 20:18:02 +01:00
|
|
|
// TODO probably can to do this better with V8's debug object once that is
|
|
|
|
// exposed.
|
2015-04-28 19:46:14 +02:00
|
|
|
var err = new Error();
|
2010-11-30 20:18:02 +01:00
|
|
|
err.name = 'Trace';
|
2013-01-18 01:02:41 +01:00
|
|
|
err.message = util.format.apply(this, arguments);
|
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
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-08-24 22:12:30 +02:00
|
|
|
Console.prototype.assert = function(expression) {
|
2010-12-02 02:29:11 +01:00
|
|
|
if (!expression) {
|
2010-11-30 20:18:02 +01:00
|
|
|
var arr = Array.prototype.slice.call(arguments, 1);
|
2011-07-29 17:26:45 +02:00
|
|
|
require('assert').ok(false, util.format.apply(this, arr));
|
2010-11-30 20:18:02 +01:00
|
|
|
}
|
|
|
|
};
|
2012-08-24 22:12:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
module.exports = new Console(process.stdout, process.stderr);
|
|
|
|
module.exports.Console = Console;
|