1 // Copyright Joyent, Inc. and other Node contributors.
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to permit
8 // persons to whom the Software is furnished to do so, subject to the
9 // following conditions:
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
22 var util = require('util');
24 function Console(stdout, stderr) {
25 if (!(this instanceof Console)) {
26 return new Console(stdout, stderr);
28 if (!stdout || !util.isFunction(stdout.write)) {
29 throw new TypeError('Console expects a writable stream instance');
40 Object.defineProperty(this, '_stdout', prop);
42 Object.defineProperty(this, '_stderr', prop);
44 Object.defineProperty(this, '_times', prop);
46 // bind the prototype functions to this Console instance
47 var keys = Object.keys(Console.prototype);
48 for (var v = 0; v < keys.length; v++) {
50 this[k] = this[k].bind(this);
54 Console.prototype.log = function() {
55 this._stdout.write(util.format.apply(this, arguments) + '\n');
59 Console.prototype.info = Console.prototype.log;
62 Console.prototype.warn = function() {
63 this._stderr.write(util.format.apply(this, arguments) + '\n');
67 Console.prototype.error = Console.prototype.warn;
70 Console.prototype.dir = function(object, options) {
71 this._stdout.write(util.inspect(object, util._extend({
77 Console.prototype.time = function(label) {
78 this._times[label] = Date.now();
82 Console.prototype.timeEnd = function(label) {
83 var time = this._times[label];
85 throw new Error('No such label: ' + label);
87 var duration = Date.now() - time;
88 this.log('%s: %dms', label, duration);
92 Console.prototype.trace = function() {
93 // TODO probably can to do this better with V8's debug object once that is
97 err.message = util.format.apply(this, arguments);
98 Error.captureStackTrace(err, arguments.callee);
99 this.error(err.stack);
103 Console.prototype.assert = function(expression) {
105 var arr = Array.prototype.slice.call(arguments, 1);
106 require('assert').ok(false, util.format.apply(this, arr));
111 module.exports = new Console(process.stdout, process.stderr);
112 module.exports.Console = Console;