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