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