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