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