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