3 const util = require('util');
5 function Console(stdout, stderr) {
6 if (!(this instanceof Console)) {
7 return new Console(stdout, stderr);
9 if (!stdout || typeof stdout.write !== 'function') {
10 throw new TypeError('Console expects a writable stream instance');
21 Object.defineProperty(this, '_stdout', prop);
23 Object.defineProperty(this, '_stderr', prop);
24 prop.value = new Map();
25 Object.defineProperty(this, '_times', prop);
27 // bind the prototype functions to this Console instance
28 var keys = Object.keys(Console.prototype);
29 for (var v = 0; v < keys.length; v++) {
31 this[k] = this[k].bind(this);
35 Console.prototype.log = function() {
36 this._stdout.write(util.format.apply(this, arguments) + '\n');
40 Console.prototype.info = Console.prototype.log;
43 Console.prototype.warn = function() {
44 this._stderr.write(util.format.apply(this, arguments) + '\n');
48 Console.prototype.error = Console.prototype.warn;
51 Console.prototype.dir = function(object, options) {
52 this._stdout.write(util.inspect(object, util._extend({
58 Console.prototype.time = function(label) {
59 this._times.set(label, Date.now());
63 Console.prototype.timeEnd = function(label) {
64 var time = this._times.get(label);
66 throw new Error('No such label: ' + label);
68 var duration = Date.now() - time;
69 this.log('%s: %dms', label, duration);
73 Console.prototype.trace = function trace() {
74 // TODO probably can to do this better with V8's debug object once that is
76 var err = new Error();
78 err.message = util.format.apply(this, arguments);
79 Error.captureStackTrace(err, trace);
80 this.error(err.stack);
84 Console.prototype.assert = function(expression) {
86 var arr = Array.prototype.slice.call(arguments, 1);
87 require('assert').ok(false, util.format.apply(this, arr));
92 module.exports = new Console(process.stdout, process.stderr);
93 module.exports.Console = Console;