]> git.r.bdr.sh - rbdr/cologne/blob - lib/cobalt.js
771c9c77e9ae8921953abbd7fab0bc8b1490317f
[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.Simple = require('./formatters/simple.js').Simple;
23 Cobalt.Formatter.Ansi = require('./formatters/ansi.js').Ansi;
24 Cobalt.Formatter.Token = require('./formatters/token.js').Token;
25
26 // Loggers
27 Cobalt.Logger.JsConsole = require('./loggers/console.js').JsConsole;
28 Cobalt.Logger.Socket = require('./loggers/socket.js').Socket;
29 }
30
31 Cobalt.Console = Class(Cobalt, 'Console')({
32 prototype : {
33 from : "Generic Cobalt Logger",
34 version : "0.1.0",
35 currentIndent : 0,
36 indentSize : 2,
37 loggers : [],
38 separatorLength : 120,
39 currentColor : "black",
40
41 // Initialize instance of cobalt console
42 // and extend configuration.
43 init : function (config) {
44 var co = this,
45 property;
46
47 if (config) {
48 for (property in config) {
49 co[property] = config[property];
50 }
51 }
52 },
53
54 addLogger : function (logger) {
55 this.loggers.push(logger);
56 },
57
58 removeLogger : function (logger) {
59 var index;
60
61 index = this.loggers.indexOf(logger);
62 this.loggers.splice(index, 1);
63 },
64
65 // Builds a Cobalt Log Object
66 buildLog : function (item, level) {
67 var co = this;
68
69 if (!item._cobaltLog) {
70 if (typeof item !== "object") {
71 item = { _cobaltLog : true, message : item.toString() };
72 }
73
74 item._from = co.from;
75 item._level = item._level || level || 7;
76 item._levelString = co._levelString(item._level);
77 item._version = co.version;
78 item._timestamp = co.now();
79 item._indentLevel = co.currentIndent;
80 item._color = co.currentColor;
81 }
82
83 return item;
84 },
85
86 buildSeparator : function (type) {
87 var co = this;
88 return {
89 _cobaltLog : true,
90 _separator : true,
91 _version : co.version,
92 _timestamp : co.now(),
93 _separatorType : type,
94 _indentLevel : co.currentIndent,
95 _color : co.currentColor
96 }
97 },
98
99 _log : function (severity) {
100 var co = this,
101 logString,
102 i, j;
103
104 for (i = 1; i < arguments.length; i++) {
105 logObject = co.buildLog(arguments[i], severity);
106
107 for (j = 0; j < co.loggers.length; j++) {
108 co.loggers[j].log(logObject);
109 }
110 }
111 },
112
113 log : function () {
114 this._log.apply(this, [7].concat([].splice.call(arguments, 0)));
115 },
116
117 info : function () {
118 this._log.apply(this, [6].concat([].splice.call(arguments, 0)));
119 },
120
121 notice : function () {
122 this._log.apply(this, [5].concat([].splice.call(arguments, 0)));
123 },
124
125 warn : function () {
126 this._log.apply(this, [4].concat([].splice.call(arguments, 0)));
127 },
128
129 error : function () {
130 this._log.apply(this, [3].concat([].splice.call(arguments, 0)));
131 },
132
133 separator : function (type) {
134 var co = this;
135
136 co._log(7, co.buildSeparator(type));
137 },
138
139 space : function (lines) {
140 var co = this,
141 i;
142
143 if (typeof lines === "undefined") {
144 lines = 1;
145 }
146
147 for (i = 0; i < lines; i++) {
148 co.log(' ');
149 }
150
151 return co;
152 },
153
154 indent : function (callback) {
155 var co = this;
156
157 if (typeof callback === "function") {
158 co.currentIndent = co.currentIndent + co.indentSize;
159 callback();
160 co.currentIndent = co.currentIndent - co.indentSize;
161 } else {
162 co.currentIndent = co.currentIndent + co.indentSize;
163 }
164
165 return co;
166 },
167
168 outdent : function (callback) {
169 var co = this;
170
171 if (typeof callback === "function") {
172 co.currentIndent = co.currentIndent - co.indentSize;
173 if (co.currentIndent < 0) {
174 co.currentIndent = 0;
175 }
176
177 callback();
178
179 co.currentIndent = co.currentIndent + co.indentSize;
180 } else {
181 co.currentIndent = co.currentIndent - co.indentSize;
182 if (co.currentIndent < 0) {
183 co.currentIndent = 0;
184 }
185 }
186
187 return co;
188 },
189
190 color : function (color, callback) {
191 var co = this,
192 oldColor = co.currentColor;
193
194 if (typeof callback === "function") {
195 co.currentColor = color;
196 callback();
197 co.currentColor = oldColor;
198 } else {
199 co.currentColor = color;
200 }
201
202 return co;
203 },
204
205 // Returns the current time in microseconds.
206 now : function () {
207 if (typeof performance !== 'undefined') {
208 return performance.timing.navigationStart + performance.now();
209 }
210
211 if (typeof Microtime !== 'undefined') {
212 return Microtime.nowDouble() * 1000;
213 }
214
215 return Date.now();
216 },
217
218 _levelString : function (level) {
219 switch(level) {
220 case 0:
221 return "PANIC";
222 break;
223 case 1:
224 return "ALERT"
225 break;
226 case 2:
227 return "CRIT"
228 break;
229 case 3:
230 return "ERROR"
231 break;
232 case 4:
233 return "WARN"
234 break;
235 case 5:
236 return "NOTICE"
237 break;
238 case 6:
239 return "INFO"
240 break;
241 case 7:
242 return "DEBUG"
243 break;
244 }
245 }
246 }
247 });
248
249 if (typeof require === 'function') {
250 global.Formatter = Cobalt.Formatter;
251 global.Logger = Cobalt.Logger;
252 global.Console = Cobalt.Console;
253 } else {
254 global.Cobalt = Cobalt;
255 }
256
257 }(typeof window !== 'undefined' ? window : (typeof module.exports !== 'undefined' ? module.exports : self)));