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