2014-11-22 16:59:48 +01:00
|
|
|
'use strict';
|
|
|
|
|
2015-01-21 11:36:59 -05:00
|
|
|
const util = require('util');
|
2010-11-30 11:18:02 -08:00
|
|
|
|
2012-08-24 13:12:30 -07:00
|
|
|
function Console(stdout, stderr) {
|
|
|
|
if (!(this instanceof Console)) {
|
|
|
|
return new Console(stdout, stderr);
|
|
|
|
}
|
2015-01-28 20:05:53 -05:00
|
|
|
if (!stdout || typeof stdout.write !== 'function') {
|
2012-08-24 13:12:30 -07: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-22 20:16:36 -05:00
|
|
|
prop.value = new Map();
|
2012-08-24 13:12:30 -07:00
|
|
|
Object.defineProperty(this, '_times', prop);
|
|
|
|
|
|
|
|
// bind the prototype functions to this Console instance
|
2014-08-14 11:15:24 +08:00
|
|
|
var keys = Object.keys(Console.prototype);
|
|
|
|
for (var v = 0; v < keys.length; v++) {
|
|
|
|
var k = keys[v];
|
2012-08-24 13:12:30 -07:00
|
|
|
this[k] = this[k].bind(this);
|
2014-08-14 11:15:24 +08:00
|
|
|
}
|
2012-08-24 13:12:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
Console.prototype.log = function() {
|
|
|
|
this._stdout.write(util.format.apply(this, arguments) + '\n');
|
2010-11-30 11:18:02 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-08-24 13:12:30 -07:00
|
|
|
Console.prototype.info = Console.prototype.log;
|
2010-11-30 11:18:02 -08:00
|
|
|
|
|
|
|
|
2012-08-24 13:12:30 -07:00
|
|
|
Console.prototype.warn = function() {
|
|
|
|
this._stderr.write(util.format.apply(this, arguments) + '\n');
|
2010-11-30 11:18:02 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-08-24 13:12:30 -07:00
|
|
|
Console.prototype.error = Console.prototype.warn;
|
2010-11-30 11:18:02 -08:00
|
|
|
|
|
|
|
|
2014-06-08 12:14:14 +03:00
|
|
|
Console.prototype.dir = function(object, options) {
|
2014-06-08 13:02:54 +03:00
|
|
|
this._stdout.write(util.inspect(object, util._extend({
|
|
|
|
customInspect: false
|
|
|
|
}, options)) + '\n');
|
2010-11-30 11:18:02 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-08-24 13:12:30 -07:00
|
|
|
Console.prototype.time = function(label) {
|
2015-01-22 20:16:36 -05:00
|
|
|
this._times.set(label, Date.now());
|
2010-11-30 11:18:02 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-08-24 13:12:30 -07:00
|
|
|
Console.prototype.timeEnd = function(label) {
|
2015-01-22 20:16:36 -05: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 13:12:30 -07:00
|
|
|
this.log('%s: %dms', label, duration);
|
2010-11-30 11:18:02 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-11-22 16:59:48 +01:00
|
|
|
Console.prototype.trace = function trace() {
|
2010-11-30 11:18:02 -08:00
|
|
|
// TODO probably can to do this better with V8's debug object once that is
|
|
|
|
// exposed.
|
|
|
|
var err = new Error;
|
|
|
|
err.name = 'Trace';
|
2013-01-17 16:02:41 -08:00
|
|
|
err.message = util.format.apply(this, arguments);
|
2014-11-22 16:59:48 +01:00
|
|
|
Error.captureStackTrace(err, trace);
|
2012-08-24 13:12:30 -07:00
|
|
|
this.error(err.stack);
|
2010-11-30 11:18:02 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-08-24 13:12:30 -07:00
|
|
|
Console.prototype.assert = function(expression) {
|
2010-12-01 17:29:11 -08:00
|
|
|
if (!expression) {
|
2010-11-30 11:18:02 -08: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 11:18:02 -08:00
|
|
|
}
|
|
|
|
};
|
2012-08-24 13:12:30 -07:00
|
|
|
|
|
|
|
|
|
|
|
module.exports = new Console(process.stdout, process.stderr);
|
|
|
|
module.exports.Console = Console;
|