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