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