]>
Commit | Line | Data |
---|---|---|
a181aeae 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'); | |
2957910b | 15 | require('./formatters/simple.js'); |
a181aeae BB |
16 | |
17 | // Loggers | |
18 | require('./loggers/console.js'); | |
19 | require('./loggers/socket.js'); | |
2957910b | 20 | require('./loggers/file.js'); |
a181aeae BB |
21 | } |
22 | ||
23 | Cobalt.now = function () { | |
24 | if (typeof performance !== 'undefined' && performance.timing) { | |
25 | return performance.timing.navigationStart + performance.now(); | |
b69497ef BB |
26 | } |
27 | ||
a181aeae BB |
28 | if (typeof Microtime !== 'undefined') { |
29 | return Microtime.nowDouble() * 1000; | |
b69497ef BB |
30 | } |
31 | ||
a181aeae BB |
32 | return Date.now(); |
33 | } | |
c35e4543 | 34 | |
a181aeae 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); | |
c35e4543 | 44 | } |
a181aeae 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]; | |
25d73f5e | 71 | } |
25d73f5e | 72 | } |
a181aeae 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 | } | |
25d73f5e | 103 | |
a181aeae BB |
104 | if (item._cobaltLog) { |
105 | item._level = level || item._level || 6; | |
106 | item._levelString = co._levelString(item._level); | |
107 | } | |
b69497ef | 108 | |
a181aeae BB |
109 | return item; |
110 | }, | |
b69497ef | 111 | |
a181aeae BB |
112 | extendLog : function (extendingObject) { |
113 | var co = this, logObject, | |
114 | property; | |
2e8bfe95 | 115 | |
a181aeae BB |
116 | logObject = co.buildLog(undefined, 6); |
117 | extendingObject = extendingObject || {}; | |
2e8bfe95 | 118 | |
a181aeae BB |
119 | for (property in extendingObject) { |
120 | if (extendingObject.hasOwnProperty(property)) { | |
121 | logObject[property] = extendingObject[property]; | |
2e8bfe95 | 122 | } |
a181aeae | 123 | } |
2e8bfe95 | 124 | |
a181aeae 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 | }, | |
c35e4543 | 140 | |
a181aeae BB |
141 | _log : function (severity) { |
142 | var co = this, | |
143 | logString, | |
144 | logObjectArray = [], | |
145 | i, j; | |
b69497ef | 146 | |
a181aeae BB |
147 | for (i = 1; i < arguments.length; i++) { |
148 | if (typeof arguments[i] === 'undefined') { | |
149 | logObjectArray.push(co.buildLog("undefined", severity)); | |
150 | } else { | |
151 | logObjectArray.push(co.buildLog(arguments[i], severity)); | |
152 | } | |
153 | } | |
b69497ef | 154 | |
a181aeae BB |
155 | for (j = 0; j < co.loggers.length; j++) { |
156 | co.loggers[j].log.apply(co.loggers[j], logObjectArray); | |
157 | } | |
158 | }, | |
cf3671d7 | 159 | |
a181aeae BB |
160 | log : function () { |
161 | this._log.apply(this, [6].concat(Array.prototype.slice.call(arguments))); | |
162 | }, | |
b69497ef | 163 | |
a181aeae BB |
164 | debug : function () { |
165 | this._log.apply(this, [7].concat(Array.prototype.slice.call(arguments))); | |
166 | }, | |
b69497ef | 167 | |
a181aeae BB |
168 | info : function () { |
169 | this._log.apply(this, [6].concat(Array.prototype.slice.call(arguments))); | |
170 | }, | |
b69497ef | 171 | |
a181aeae BB |
172 | notice : function () { |
173 | this._log.apply(this, [5].concat(Array.prototype.slice.call(arguments))); | |
174 | }, | |
cf3671d7 | 175 | |
a181aeae BB |
176 | warn : function () { |
177 | this._log.apply(this, [4].concat(Array.prototype.slice.call(arguments))); | |
178 | }, | |
cf3671d7 | 179 | |
a181aeae BB |
180 | error : function () { |
181 | this._log.apply(this, [3].concat(Array.prototype.slice.call(arguments))); | |
182 | }, | |
cf3671d7 | 183 | |
a181aeae BB |
184 | dir : function () { |
185 | }, | |
cf3671d7 | 186 | |
a181aeae BB |
187 | time : function () { |
188 | }, | |
cf3671d7 | 189 | |
a181aeae BB |
190 | timeEnd : function () { |
191 | }, | |
b69497ef | 192 | |
a181aeae BB |
193 | groupCollapsed : function () { |
194 | }, | |
b69497ef | 195 | |
a181aeae BB |
196 | groupEnd : function () { |
197 | }, | |
b69497ef | 198 | |
a181aeae BB |
199 | separator : function (type) { |
200 | var co = this; | |
b69497ef | 201 | |
a181aeae BB |
202 | co._log(7, co.buildSeparator(type)); |
203 | }, | |
b69497ef | 204 | |
a181aeae BB |
205 | space : function (lines) { |
206 | var co = this, | |
207 | i; | |
b69497ef | 208 | |
a181aeae BB |
209 | if (typeof lines === "undefined") { |
210 | lines = 1; | |
211 | } | |
b69497ef | 212 | |
a181aeae BB |
213 | for (i = 0; i < lines; i++) { |
214 | co.log(' '); | |
215 | } | |
b69497ef | 216 | |
a181aeae BB |
217 | return co; |
218 | }, | |
b69497ef | 219 | |
a181aeae BB |
220 | indent : function (callback) { |
221 | var co = this; | |
b69497ef | 222 | |
a181aeae BB |
223 | if (typeof callback === "function") { |
224 | co.currentIndent = co.currentIndent + co.indentSize; | |
225 | callback(); | |
226 | co.currentIndent = co.currentIndent - co.indentSize; | |
227 | } else { | |
228 | co.currentIndent = co.currentIndent + co.indentSize; | |
229 | } | |
b69497ef | 230 | |
a181aeae BB |
231 | return co; |
232 | }, | |
b69497ef | 233 | |
a181aeae BB |
234 | outdent : function (callback) { |
235 | var co = this; | |
b69497ef | 236 | |
a181aeae BB |
237 | if (typeof callback === "function") { |
238 | co.currentIndent = co.currentIndent - co.indentSize; | |
239 | if (co.currentIndent < 0) { | |
240 | co.currentIndent = 0; | |
b69497ef BB |
241 | } |
242 | ||
a181aeae | 243 | callback(); |
b69497ef | 244 | |
a181aeae BB |
245 | co.currentIndent = co.currentIndent + co.indentSize; |
246 | } else { | |
247 | co.currentIndent = co.currentIndent - co.indentSize; | |
248 | if (co.currentIndent < 0) { | |
249 | co.currentIndent = 0; | |
b69497ef | 250 | } |
a181aeae | 251 | } |
b69497ef | 252 | |
a181aeae BB |
253 | return co; |
254 | }, | |
b69497ef | 255 | |
a181aeae BB |
256 | color : function (color, callback) { |
257 | var co = this, | |
258 | oldColor = co.currentColor; | |
daf011e6 | 259 | |
a181aeae BB |
260 | if (typeof callback === "function") { |
261 | co.currentColor = color; | |
262 | callback(); | |
263 | co.currentColor = oldColor; | |
264 | } else { | |
265 | co.currentColor = color; | |
266 | } | |
daf011e6 | 267 | |
a181aeae BB |
268 | return co; |
269 | }, | |
270 | ||
271 | // Returns the current time in microseconds. | |
272 | now : function () { | |
273 | if (typeof performance !== 'undefined' && performance.timing) { | |
274 | return performance.timing.navigationStart + performance.now(); | |
b69497ef | 275 | } |
b69497ef | 276 | |
a181aeae BB |
277 | if (typeof Microtime !== 'undefined') { |
278 | return Microtime.nowDouble() * 1000; | |
279 | } | |
c35e4543 | 280 | |
a181aeae BB |
281 | return Date.now(); |
282 | }, | |
283 | ||
284 | _levelString : function (level) { | |
285 | switch(level) { | |
286 | case 0: | |
287 | return "PANIC"; | |
288 | break; | |
289 | case 1: | |
290 | return "ALERT" | |
291 | break; | |
292 | case 2: | |
293 | return "CRIT" | |
294 | break; | |
295 | case 3: | |
296 | return "ERROR" | |
297 | break; | |
298 | case 4: | |
299 | return "WARN" | |
300 | break; | |
301 | case 5: | |
302 | return "NOTICE" | |
303 | break; | |
304 | case 6: | |
305 | return "INFO" | |
306 | break; | |
307 | case 7: | |
308 | return "DEBUG" | |
309 | break; | |
310 | } | |
311 | } | |
b69497ef | 312 | } |
a181aeae | 313 | }); |
b69497ef | 314 | |
a181aeae BB |
315 | if (Cobalt.Console.__objectSpy) { |
316 | Cobalt.Console.__objectSpy.destroy(); | |
317 | } |