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