]>
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') { | |
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; | |
95 | ||
96 | if (!item._cobaltLog) { | |
97 | if (typeof item !== "object") { | |
98 | item = {message : item.toString() }; | |
99 | } else { | |
100 | item.message = Cobalt.stringify(item); | |
101 | } | |
102 | ||
103 | item._cobaltLog = true; | |
104 | item._from = co.from; | |
105 | item._level = item._level || level || 7; | |
106 | item._levelString = co._levelString(item._level); | |
107 | item._version = co.version; | |
108 | item._timestamp = co.now(); | |
109 | item._indentLevel = co.currentIndent; | |
110 | item._color = co.currentColor; | |
111 | } | |
112 | ||
113 | return item; | |
114 | }, | |
115 | ||
116 | buildSeparator : function (type) { | |
117 | var co = this; | |
118 | return { | |
119 | _cobaltLog : true, | |
120 | _separator : true, | |
121 | _version : co.version, | |
122 | _timestamp : co.now(), | |
123 | _separatorType : type, | |
124 | _indentLevel : co.currentIndent, | |
125 | _color : co.currentColor | |
126 | } | |
127 | }, | |
128 | ||
129 | _log : function (severity) { | |
130 | var co = this, | |
131 | logString, | |
132 | logObjectArray = [], | |
133 | i, j; | |
134 | ||
135 | for (i = 1; i < arguments.length; i++) { | |
136 | if (typeof arguments[i] === 'undefined') { | |
137 | logObjectArray.push(co.buildLog("undefined", severity)); | |
138 | } else { | |
139 | logObjectArray.push(co.buildLog(arguments[i], severity)); | |
140 | } | |
141 | } | |
142 | ||
143 | for (j = 0; j < co.loggers.length; j++) { | |
144 | co.loggers[j].log.apply(co.loggers[j], logObjectArray); | |
145 | } | |
146 | }, | |
147 | ||
148 | log : function () { | |
149 | this._log.apply(this, [7].concat([].splice.call(arguments, 0))); | |
150 | }, | |
151 | ||
152 | info : function () { | |
153 | this._log.apply(this, [6].concat([].splice.call(arguments, 0))); | |
154 | }, | |
155 | ||
156 | notice : function () { | |
157 | this._log.apply(this, [5].concat([].splice.call(arguments, 0))); | |
158 | }, | |
159 | ||
160 | warn : function () { | |
161 | this._log.apply(this, [4].concat([].splice.call(arguments, 0))); | |
162 | }, | |
163 | ||
164 | error : function () { | |
165 | this._log.apply(this, [3].concat([].splice.call(arguments, 0))); | |
166 | }, | |
167 | ||
168 | separator : function (type) { | |
169 | var co = this; | |
170 | ||
171 | co._log(7, co.buildSeparator(type)); | |
172 | }, | |
173 | ||
174 | space : function (lines) { | |
175 | var co = this, | |
176 | i; | |
177 | ||
178 | if (typeof lines === "undefined") { | |
179 | lines = 1; | |
180 | } | |
181 | ||
182 | for (i = 0; i < lines; i++) { | |
183 | co.log(' '); | |
184 | } | |
185 | ||
186 | return co; | |
187 | }, | |
188 | ||
189 | indent : function (callback) { | |
190 | var co = this; | |
191 | ||
192 | if (typeof callback === "function") { | |
193 | co.currentIndent = co.currentIndent + co.indentSize; | |
194 | callback(); | |
195 | co.currentIndent = co.currentIndent - co.indentSize; | |
196 | } else { | |
197 | co.currentIndent = co.currentIndent + co.indentSize; | |
198 | } | |
199 | ||
200 | return co; | |
201 | }, | |
202 | ||
203 | outdent : function (callback) { | |
204 | var co = this; | |
205 | ||
206 | if (typeof callback === "function") { | |
207 | co.currentIndent = co.currentIndent - co.indentSize; | |
208 | if (co.currentIndent < 0) { | |
209 | co.currentIndent = 0; | |
210 | } | |
211 | ||
212 | callback(); | |
213 | ||
214 | co.currentIndent = co.currentIndent + co.indentSize; | |
215 | } else { | |
216 | co.currentIndent = co.currentIndent - co.indentSize; | |
217 | if (co.currentIndent < 0) { | |
218 | co.currentIndent = 0; | |
219 | } | |
220 | } | |
221 | ||
222 | return co; | |
223 | }, | |
224 | ||
225 | color : function (color, callback) { | |
226 | var co = this, | |
227 | oldColor = co.currentColor; | |
228 | ||
229 | if (typeof callback === "function") { | |
230 | co.currentColor = color; | |
231 | callback(); | |
232 | co.currentColor = oldColor; | |
233 | } else { | |
234 | co.currentColor = color; | |
235 | } | |
236 | ||
237 | return co; | |
238 | }, | |
239 | ||
240 | // Returns the current time in microseconds. | |
241 | now : function () { | |
242 | if (typeof performance !== 'undefined') { | |
243 | return performance.timing.navigationStart + performance.now(); | |
244 | } | |
245 | ||
246 | if (typeof Microtime !== 'undefined') { | |
247 | return Microtime.nowDouble() * 1000; | |
248 | } | |
249 | ||
250 | return Date.now(); | |
251 | }, | |
252 | ||
253 | _levelString : function (level) { | |
254 | switch(level) { | |
255 | case 0: | |
256 | return "PANIC"; | |
257 | break; | |
258 | case 1: | |
259 | return "ALERT" | |
260 | break; | |
261 | case 2: | |
262 | return "CRIT" | |
263 | break; | |
264 | case 3: | |
265 | return "ERROR" | |
266 | break; | |
267 | case 4: | |
268 | return "WARN" | |
269 | break; | |
270 | case 5: | |
271 | return "NOTICE" | |
272 | break; | |
273 | case 6: | |
274 | return "INFO" | |
275 | break; | |
276 | case 7: | |
277 | return "DEBUG" | |
278 | break; | |
279 | } | |
280 | } | |
281 | } | |
282 | }); | |
283 | ||
284 | if (Cobalt.Console.__objectSpy) { | |
285 | Cobalt.Console.__objectSpy.destroy(); | |
286 | } | |
287 | ||
288 | if (typeof require === 'function') { | |
289 | global.Formatter = Cobalt.Formatter; | |
290 | global.Logger = Cobalt.Logger; | |
291 | global.Console = Cobalt.Console; | |
292 | global.now = Cobalt.now; | |
293 | global.stringify = Cobalt.stringify; | |
294 | } else { | |
295 | global.Cobalt = Cobalt; | |
296 | } | |
297 | ||
298 | }(typeof window !== 'undefined' ? window : (typeof module.exports !== 'undefined' ? module.exports : self))); |