]> git.r.bdr.sh - rbdr/cologne/blob - lib/cobalt.js
d129714c98271e8ed75da9b9d7959147dc2bcd38
[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 {
151 logObjectArray.push(co.buildLog(arguments[i], severity));
152 }
153 }
154
155 for (j = 0; j < co.loggers.length; j++) {
156 co.loggers[j].log.apply(co.loggers[j], logObjectArray);
157 }
158 },
159
160 log : function () {
161 this._log.apply(this, [6].concat(Array.prototype.slice.call(arguments)));
162 },
163
164 debug : function () {
165 this._log.apply(this, [7].concat(Array.prototype.slice.call(arguments)));
166 },
167
168 info : function () {
169 this._log.apply(this, [6].concat(Array.prototype.slice.call(arguments)));
170 },
171
172 notice : function () {
173 this._log.apply(this, [5].concat(Array.prototype.slice.call(arguments)));
174 },
175
176 warn : function () {
177 this._log.apply(this, [4].concat(Array.prototype.slice.call(arguments)));
178 },
179
180 error : function () {
181 this._log.apply(this, [3].concat(Array.prototype.slice.call(arguments)));
182 },
183
184 dir : function () {
185 },
186
187 time : function () {
188 },
189
190 timeEnd : function () {
191 },
192
193 groupCollapsed : function () {
194 },
195
196 groupEnd : function () {
197 },
198
199 separator : function (type) {
200 var co = this;
201
202 co._log(7, co.buildSeparator(type));
203 },
204
205 space : function (lines) {
206 var co = this,
207 i;
208
209 if (typeof lines === "undefined") {
210 lines = 1;
211 }
212
213 for (i = 0; i < lines; i++) {
214 co.log(' ');
215 }
216
217 return co;
218 },
219
220 indent : function (callback) {
221 var co = this;
222
223 if (typeof callback === "function") {
224 co.currentIndent = co.currentIndent + co.indentSize;
225 callback();
226 co.currentIndent = co.currentIndent - co.indentSize;
227 } else {
228 co.currentIndent = co.currentIndent + co.indentSize;
229 }
230
231 return co;
232 },
233
234 outdent : function (callback) {
235 var co = this;
236
237 if (typeof callback === "function") {
238 co.currentIndent = co.currentIndent - co.indentSize;
239 if (co.currentIndent < 0) {
240 co.currentIndent = 0;
241 }
242
243 callback();
244
245 co.currentIndent = co.currentIndent + co.indentSize;
246 } else {
247 co.currentIndent = co.currentIndent - co.indentSize;
248 if (co.currentIndent < 0) {
249 co.currentIndent = 0;
250 }
251 }
252
253 return co;
254 },
255
256 color : function (color, callback) {
257 var co = this,
258 oldColor = co.currentColor;
259
260 if (typeof callback === "function") {
261 co.currentColor = color;
262 callback();
263 co.currentColor = oldColor;
264 } else {
265 co.currentColor = color;
266 }
267
268 return co;
269 },
270
271 // Returns the current time in microseconds.
272 now : function () {
273 if (typeof performance !== 'undefined' && performance.timing) {
274 return performance.timing.navigationStart + performance.now();
275 }
276
277 if (typeof Microtime !== 'undefined') {
278 return Microtime.nowDouble() * 1000;
279 }
280
281 return Date.now();
282 },
283
284 _levelString : function (level) {
285 switch(level) {
286 case 0:
287 return "PANIC";
288 break;
289 case 1:
290 return "ALERT"
291 break;
292 case 2:
293 return "CRIT"
294 break;
295 case 3:
296 return "ERROR"
297 break;
298 case 4:
299 return "WARN"
300 break;
301 case 5:
302 return "NOTICE"
303 break;
304 case 6:
305 return "INFO"
306 break;
307 case 7:
308 return "DEBUG"
309 break;
310 }
311 }
312 }
313 });
314
315 if (Cobalt.Console.__objectSpy) {
316 Cobalt.Console.__objectSpy.destroy();
317 }