Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80606 views
1
/**
2
* Copyright (c) 2014, Facebook, Inc. All rights reserved.
3
*
4
* This source code is licensed under the BSD-style license found in the
5
* LICENSE file in the root directory of this source tree. An additional grant
6
* of patent rights can be found in the PATENTS file in the same directory.
7
*/
8
9
// Copyright Joyent, Inc. and other Node contributors.
10
//
11
// Permission is hereby granted, free of charge, to any person obtaining a
12
// copy of this software and associated documentation files (the
13
// "Software"), to deal in the Software without restriction, including
14
// without limitation the rights to use, copy, modify, merge, publish,
15
// distribute, sublicense, and/or sell copies of the Software, and to permit
16
// persons to whom the Software is furnished to do so, subject to the
17
// following conditions:
18
//
19
// The above copyright notice and this permission notice shall be included
20
// in all copies or substantial portions of the Software.
21
//
22
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
25
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
26
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
27
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
28
// USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30
/*jshint strict:false*/
31
32
var util = require('util');
33
34
function Console(messageQueue) {
35
if (!(this instanceof Console)) {
36
return new Console(messageQueue);
37
}
38
39
Object.defineProperty(this, '_messageQueue', {
40
value: messageQueue,
41
writable: true,
42
enumerable: false,
43
configurable: true
44
});
45
46
Object.defineProperty(this, '_times', {
47
value: {},
48
writable: true,
49
enumerable: false,
50
configurable: true
51
});
52
53
// bind the prototype functions to this Console instance
54
var keys = Object.keys(Console.prototype);
55
for (var v = 0; v < keys.length; v++) {
56
var k = keys[v];
57
this[k] = this[k].bind(this);
58
}
59
}
60
61
Console.prototype.log = function() {
62
this._messageQueue.push({
63
type: 'log',
64
data: util.format.apply(this, arguments) + '\n'
65
});
66
};
67
68
69
Console.prototype.info = Console.prototype.log;
70
71
72
Console.prototype.warn = function() {
73
this._messageQueue.push({
74
type: 'warn',
75
data: util.format.apply(this, arguments) + '\n'
76
});
77
};
78
79
80
Console.prototype.error = function() {
81
this._messageQueue.push({
82
type: 'error',
83
data: util.format.apply(this, arguments) + '\n'
84
});
85
};
86
87
88
Console.prototype.dir = function(object, options) {
89
this._messageQueue.push({
90
type: 'dir',
91
data: util.inspect(object, util._extend({
92
customInspect: false
93
}, options)) + '\n'
94
});
95
};
96
97
98
Console.prototype.time = function(label) {
99
this._times[label] = Date.now();
100
};
101
102
103
Console.prototype.timeEnd = function(label) {
104
var time = this._times[label];
105
if (!time) {
106
throw new Error('No such label: ' + label);
107
}
108
var duration = Date.now() - time;
109
this.log('%s: %dms', label, duration);
110
};
111
112
113
Console.prototype.trace = function() {
114
// TODO probably can to do this better with V8's debug object once that is
115
// exposed.
116
var err = new Error();
117
err.name = 'Trace';
118
err.message = util.format.apply(this, arguments);
119
/*jshint noarg:false*/
120
Error.captureStackTrace(err, arguments.callee);
121
this.error(err.stack);
122
};
123
124
125
Console.prototype.assert = function(expression) {
126
if (!expression) {
127
var arr = Array.prototype.slice.call(arguments, 1);
128
require('assert').ok(false, util.format.apply(this, arr));
129
}
130
};
131
132
module.exports = Console;
133
134