2a09329595d31b0c8806f753303b99343d5435f5
[platform/upstream/nodejs.git] / lib / console.js
1 // Copyright Joyent, Inc. and other Node contributors.
2 //
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:
10 //
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
13 //
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.
21
22 var util = require('util');
23
24 function Console(stdout, stderr) {
25   if (!(this instanceof Console)) {
26     return new Console(stdout, stderr);
27   }
28   if (!stdout || !util.isFunction(stdout.write)) {
29     throw new TypeError('Console expects a writable stream instance');
30   }
31   if (!stderr) {
32     stderr = stdout;
33   }
34   var prop = {
35     writable: true,
36     enumerable: false,
37     configurable: true
38   };
39   prop.value = stdout;
40   Object.defineProperty(this, '_stdout', prop);
41   prop.value = stderr;
42   Object.defineProperty(this, '_stderr', prop);
43   prop.value = {};
44   Object.defineProperty(this, '_times', prop);
45
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++) {
49     var k = keys[v];
50     this[k] = this[k].bind(this);
51   }
52 }
53
54 Console.prototype.log = function() {
55   this._stdout.write(util.format.apply(this, arguments) + '\n');
56 };
57
58
59 Console.prototype.info = Console.prototype.log;
60
61
62 Console.prototype.warn = function() {
63   this._stderr.write(util.format.apply(this, arguments) + '\n');
64 };
65
66
67 Console.prototype.error = Console.prototype.warn;
68
69
70 Console.prototype.dir = function(object, options) {
71   this._stdout.write(util.inspect(object, util._extend({
72     customInspect: false
73   }, options)) + '\n');
74 };
75
76
77 Console.prototype.time = function(label) {
78   this._times[label] = Date.now();
79 };
80
81
82 Console.prototype.timeEnd = function(label) {
83   var time = this._times[label];
84   if (!time) {
85     throw new Error('No such label: ' + label);
86   }
87   var duration = Date.now() - time;
88   this.log('%s: %dms', label, duration);
89 };
90
91
92 Console.prototype.trace = function() {
93   // TODO probably can to do this better with V8's debug object once that is
94   // exposed.
95   var err = new Error;
96   err.name = 'Trace';
97   err.message = util.format.apply(this, arguments);
98   Error.captureStackTrace(err, arguments.callee);
99   this.error(err.stack);
100 };
101
102
103 Console.prototype.assert = function(expression) {
104   if (!expression) {
105     var arr = Array.prototype.slice.call(arguments, 1);
106     require('assert').ok(false, util.format.apply(this, arr));
107   }
108 };
109
110
111 module.exports = new Console(process.stdout, process.stderr);
112 module.exports.Console = Console;