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