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