]>
Commit | Line | Data |
---|---|---|
db6bc3cb BB |
1 | // Load up dependencies |
2 | if (typeof require === 'function') { | |
3 | require('neon'); | |
4 | var Microtime = require('microtime'); | |
5 | } | |
6 | ||
7 | Cobalt = Module("Cobalt"); | |
8 | Module(Cobalt, 'Logger')({}); | |
9 | Module(Cobalt, 'Formatter')({}); | |
10 | ||
11 | // Load up loggers + formatters | |
12 | if (typeof require === 'function') { | |
13 | // Formatters | |
14 | require('./formatters/token.js'); | |
8ef6f979 | 15 | require('./formatters/simple.js'); |
db6bc3cb BB |
16 | |
17 | // Loggers | |
18 | require('./loggers/console.js'); | |
19 | require('./loggers/socket.js'); | |
8ef6f979 | 20 | require('./loggers/file.js'); |
db6bc3cb BB |
21 | } |
22 | ||
23 | Cobalt.now = function () { | |
24 | if (typeof performance !== 'undefined' && performance.timing) { | |
25 | return performance.timing.navigationStart + performance.now(); | |
bdedb5e5 BB |
26 | } |
27 | ||
db6bc3cb BB |
28 | if (typeof Microtime !== 'undefined') { |
29 | return Microtime.nowDouble() * 1000; | |
bdedb5e5 BB |
30 | } |
31 | ||
db6bc3cb BB |
32 | return Date.now(); |
33 | } | |
71a808fe | 34 | |
db6bc3cb BB |
35 | // Stringify with circular dereference. |
36 | Cobalt.stringify = function (object) { | |
37 | var cache = [], stringified; | |
38 | stringified = JSON.stringify(object, function (key, value) { | |
39 | if (typeof value === 'object' && value !== null) { | |
40 | if (cache.indexOf(value) !== -1) { | |
41 | return "[Circular]"; | |
42 | } | |
43 | cache.push(value); | |
71a808fe | 44 | } |
db6bc3cb BB |
45 | return value; |
46 | }); | |
47 | cache = null; | |
48 | ||
49 | return stringified; | |
50 | } | |
51 | ||
52 | Class(Cobalt, 'Console')({ | |
53 | prototype : { | |
54 | from : "Generic Cobalt Logger", | |
55 | version : "0.1.0", | |
56 | currentIndent : 0, | |
57 | indentSize : 2, | |
58 | loggers : [], | |
59 | separatorLength : 120, | |
60 | currentColor : "black", | |
61 | ||
62 | // Initialize instance of cobalt console | |
63 | // and extend configuration. | |
64 | init : function (config) { | |
65 | var co = this, | |
66 | property; | |
67 | ||
68 | if (config) { | |
69 | for (property in config) { | |
70 | co[property] = config[property]; | |
aa1e9cff | 71 | } |
aa1e9cff | 72 | } |
db6bc3cb BB |
73 | }, |
74 | ||
75 | addLogger : function (logger) { | |
76 | this.loggers.push(logger); | |
77 | }, | |
78 | ||
79 | removeLogger : function (logger) { | |
80 | var index; | |
81 | ||
82 | index = this.loggers.indexOf(logger); | |
83 | this.loggers.splice(index, 1); | |
84 | }, | |
85 | ||
86 | // Builds a Cobalt Log Object | |
87 | buildLog : function (item, level) { | |
88 | var co = this, oldItem, logObject = {}; | |
89 | ||
90 | if (typeof item === "undefined" || item === null || !item._cobaltLog) { | |
91 | logObject.message = item; | |
92 | logObject._cobaltLog = true; | |
93 | logObject._from = co.from; | |
94 | logObject._level = level || 6; | |
95 | logObject._levelString = co._levelString(logObject._level); | |
96 | logObject._version = co.version; | |
97 | logObject._timestamp = co.now(); | |
98 | logObject._indentLevel = co.currentIndent; | |
99 | logObject._color = co.currentColor; | |
100 | logObject._separator = false; | |
101 | return logObject; | |
102 | } | |
aa1e9cff | 103 | |
db6bc3cb BB |
104 | if (item._cobaltLog) { |
105 | item._level = level || item._level || 6; | |
106 | item._levelString = co._levelString(item._level); | |
107 | } | |
bdedb5e5 | 108 | |
db6bc3cb BB |
109 | return item; |
110 | }, | |
bdedb5e5 | 111 | |
db6bc3cb BB |
112 | extendLog : function (extendingObject) { |
113 | var co = this, logObject, | |
114 | property; | |
f6cd3c16 | 115 | |
db6bc3cb BB |
116 | logObject = co.buildLog(undefined, 6); |
117 | extendingObject = extendingObject || {}; | |
f6cd3c16 | 118 | |
db6bc3cb BB |
119 | for (property in extendingObject) { |
120 | if (extendingObject.hasOwnProperty(property)) { | |
121 | logObject[property] = extendingObject[property]; | |
f6cd3c16 | 122 | } |
db6bc3cb | 123 | } |
f6cd3c16 | 124 | |
db6bc3cb BB |
125 | return logObject; |
126 | }, | |
127 | ||
128 | buildSeparator : function (type) { | |
129 | var co = this; | |
130 | return { | |
131 | _cobaltLog : true, | |
132 | _separator : true, | |
133 | _version : co.version, | |
134 | _timestamp : co.now(), | |
135 | _separatorType : type, | |
136 | _indentLevel : co.currentIndent, | |
137 | _color : co.currentColor | |
138 | } | |
139 | }, | |
71a808fe | 140 | |
db6bc3cb BB |
141 | _log : function (severity) { |
142 | var co = this, | |
143 | logString, | |
144 | logObjectArray = [], | |
145 | i, j; | |
bdedb5e5 | 146 | |
db6bc3cb BB |
147 | for (i = 1; i < arguments.length; i++) { |
148 | if (typeof arguments[i] === 'undefined') { | |
149 | logObjectArray.push(co.buildLog("undefined", severity)); | |
deb2dffd BB |
150 | } else if (arguments[i] === null) { |
151 | logObjectArray.push(co.buildLog("null", severity)); | |
db6bc3cb BB |
152 | } else { |
153 | logObjectArray.push(co.buildLog(arguments[i], severity)); | |
154 | } | |
155 | } | |
bdedb5e5 | 156 | |
db6bc3cb BB |
157 | for (j = 0; j < co.loggers.length; j++) { |
158 | co.loggers[j].log.apply(co.loggers[j], logObjectArray); | |
159 | } | |
160 | }, | |
5c03d3b4 | 161 | |
db6bc3cb BB |
162 | log : function () { |
163 | this._log.apply(this, [6].concat(Array.prototype.slice.call(arguments))); | |
164 | }, | |
bdedb5e5 | 165 | |
db6bc3cb BB |
166 | debug : function () { |
167 | this._log.apply(this, [7].concat(Array.prototype.slice.call(arguments))); | |
168 | }, | |
bdedb5e5 | 169 | |
db6bc3cb BB |
170 | info : function () { |
171 | this._log.apply(this, [6].concat(Array.prototype.slice.call(arguments))); | |
172 | }, | |
bdedb5e5 | 173 | |
db6bc3cb BB |
174 | notice : function () { |
175 | this._log.apply(this, [5].concat(Array.prototype.slice.call(arguments))); | |
176 | }, | |
5c03d3b4 | 177 | |
db6bc3cb BB |
178 | warn : function () { |
179 | this._log.apply(this, [4].concat(Array.prototype.slice.call(arguments))); | |
180 | }, | |
5c03d3b4 | 181 | |
db6bc3cb BB |
182 | error : function () { |
183 | this._log.apply(this, [3].concat(Array.prototype.slice.call(arguments))); | |
184 | }, | |
5c03d3b4 | 185 | |
db6bc3cb BB |
186 | dir : function () { |
187 | }, | |
5c03d3b4 | 188 | |
db6bc3cb BB |
189 | time : function () { |
190 | }, | |
5c03d3b4 | 191 | |
db6bc3cb BB |
192 | timeEnd : function () { |
193 | }, | |
bdedb5e5 | 194 | |
db6bc3cb BB |
195 | groupCollapsed : function () { |
196 | }, | |
bdedb5e5 | 197 | |
db6bc3cb BB |
198 | groupEnd : function () { |
199 | }, | |
bdedb5e5 | 200 | |
db6bc3cb BB |
201 | separator : function (type) { |
202 | var co = this; | |
bdedb5e5 | 203 | |
db6bc3cb BB |
204 | co._log(7, co.buildSeparator(type)); |
205 | }, | |
bdedb5e5 | 206 | |
db6bc3cb BB |
207 | space : function (lines) { |
208 | var co = this, | |
209 | i; | |
bdedb5e5 | 210 | |
db6bc3cb BB |
211 | if (typeof lines === "undefined") { |
212 | lines = 1; | |
213 | } | |
bdedb5e5 | 214 | |
db6bc3cb BB |
215 | for (i = 0; i < lines; i++) { |
216 | co.log(' '); | |
217 | } | |
bdedb5e5 | 218 | |
db6bc3cb BB |
219 | return co; |
220 | }, | |
bdedb5e5 | 221 | |
db6bc3cb BB |
222 | indent : function (callback) { |
223 | var co = this; | |
bdedb5e5 | 224 | |
db6bc3cb BB |
225 | if (typeof callback === "function") { |
226 | co.currentIndent = co.currentIndent + co.indentSize; | |
227 | callback(); | |
228 | co.currentIndent = co.currentIndent - co.indentSize; | |
229 | } else { | |
230 | co.currentIndent = co.currentIndent + co.indentSize; | |
231 | } | |
bdedb5e5 | 232 | |
db6bc3cb BB |
233 | return co; |
234 | }, | |
bdedb5e5 | 235 | |
db6bc3cb BB |
236 | outdent : function (callback) { |
237 | var co = this; | |
bdedb5e5 | 238 | |
db6bc3cb BB |
239 | if (typeof callback === "function") { |
240 | co.currentIndent = co.currentIndent - co.indentSize; | |
241 | if (co.currentIndent < 0) { | |
242 | co.currentIndent = 0; | |
bdedb5e5 BB |
243 | } |
244 | ||
db6bc3cb | 245 | callback(); |
bdedb5e5 | 246 | |
db6bc3cb BB |
247 | co.currentIndent = co.currentIndent + co.indentSize; |
248 | } else { | |
249 | co.currentIndent = co.currentIndent - co.indentSize; | |
250 | if (co.currentIndent < 0) { | |
251 | co.currentIndent = 0; | |
bdedb5e5 | 252 | } |
db6bc3cb | 253 | } |
bdedb5e5 | 254 | |
db6bc3cb BB |
255 | return co; |
256 | }, | |
bdedb5e5 | 257 | |
db6bc3cb BB |
258 | color : function (color, callback) { |
259 | var co = this, | |
260 | oldColor = co.currentColor; | |
adc1a8da | 261 | |
db6bc3cb BB |
262 | if (typeof callback === "function") { |
263 | co.currentColor = color; | |
264 | callback(); | |
265 | co.currentColor = oldColor; | |
266 | } else { | |
267 | co.currentColor = color; | |
268 | } | |
adc1a8da | 269 | |
db6bc3cb BB |
270 | return co; |
271 | }, | |
272 | ||
273 | // Returns the current time in microseconds. | |
274 | now : function () { | |
275 | if (typeof performance !== 'undefined' && performance.timing) { | |
276 | return performance.timing.navigationStart + performance.now(); | |
bdedb5e5 | 277 | } |
bdedb5e5 | 278 | |
db6bc3cb BB |
279 | if (typeof Microtime !== 'undefined') { |
280 | return Microtime.nowDouble() * 1000; | |
281 | } | |
71a808fe | 282 | |
db6bc3cb BB |
283 | return Date.now(); |
284 | }, | |
285 | ||
286 | _levelString : function (level) { | |
287 | switch(level) { | |
288 | case 0: | |
289 | return "PANIC"; | |
290 | break; | |
291 | case 1: | |
292 | return "ALERT" | |
293 | break; | |
294 | case 2: | |
295 | return "CRIT" | |
296 | break; | |
297 | case 3: | |
298 | return "ERROR" | |
299 | break; | |
300 | case 4: | |
301 | return "WARN" | |
302 | break; | |
303 | case 5: | |
304 | return "NOTICE" | |
305 | break; | |
306 | case 6: | |
307 | return "INFO" | |
308 | break; | |
309 | case 7: | |
310 | return "DEBUG" | |
311 | break; | |
312 | } | |
313 | } | |
bdedb5e5 | 314 | } |
db6bc3cb | 315 | }); |
bdedb5e5 | 316 | |
db6bc3cb BB |
317 | if (Cobalt.Console.__objectSpy) { |
318 | Cobalt.Console.__objectSpy.destroy(); | |
319 | } |