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