]>
git.r.bdr.sh - rbdr/cologne/blob - lib/cobalt.js
1 // Load up dependencies
2 if (typeof require
=== 'function') {
4 var colors
= require('colors');
5 var Microtime
= require('microtime');
8 Cobalt
= Module("Cobalt");
9 Module(Cobalt
, 'Logger')({});
10 Module(Cobalt
, 'Formatter')({});
12 // Load up loggers + formatters
13 if (typeof require
=== 'function') {
15 require('./formatters/token.js');
16 require('./formatters/simple.js');
19 require('./loggers/console.js');
20 require('./loggers/socket.js');
21 require('./loggers/file.js');
24 Cobalt
.now = function () {
25 if (typeof performance
!== 'undefined' && performance
.timing
) {
26 return performance
.timing
.navigationStart
+ performance
.now();
29 if (typeof Microtime
!== 'undefined') {
30 return Microtime
.nowDouble() * 1000;
36 // Stringify with circular dereference.
37 Cobalt
.stringify = function (object
) {
38 var cache
= [], stringified
;
39 stringified
= JSON
.stringify(object
, function (key
, value
) {
40 if (typeof value
=== 'object' && value
!== null) {
41 if (cache
.indexOf(value
) !== -1) {
53 Class(Cobalt
, 'Console')({
55 from : "Generic Cobalt Logger",
60 separatorLength : 120,
61 currentColor : "black",
63 // Initialize instance of cobalt console
64 // and extend configuration.
65 init : function (config
) {
70 for (property
in config
) {
71 co
[property
] = config
[property
];
76 addLogger : function (logger
) {
77 this.loggers
.push(logger
);
80 removeLogger : function (logger
) {
83 index
= this.loggers
.indexOf(logger
);
84 this.loggers
.splice(index
, 1);
87 // Builds a Cobalt Log Object
88 buildLog : function (item
, level
) {
89 var co
= this, oldItem
, logObject
= {};
91 if (typeof item
=== "undefined" || item
=== null || !item
._cobaltLog
) {
92 logObject
.message
= item
;
93 logObject
._cobaltLog
= true;
94 logObject
._from
= co
.from;
95 logObject
._level
= level
|| 6;
96 logObject
._levelString
= co
._levelString(logObject
._level
);
97 logObject
._version
= co
.version
;
98 logObject
._timestamp
= co
.now();
99 logObject
._indentLevel
= co
.currentIndent
;
100 logObject
._color
= co
.currentColor
;
101 logObject
._separator
= false;
105 if (item
._cobaltLog
) {
106 item
._level
= level
|| item
._level
|| 6;
107 item
._levelString
= co
._levelString(item
._level
);
113 extendLog : function (extendingObject
) {
114 var co
= this, logObject
,
117 logObject
= co
.buildLog(undefined, 6);
118 extendingObject
= extendingObject
|| {};
120 for (property
in extendingObject
) {
121 if (extendingObject
.hasOwnProperty(property
)) {
122 logObject
[property
] = extendingObject
[property
];
129 buildSeparator : function (type
) {
134 _version : co
.version
,
135 _timestamp : co
.now(),
136 _separatorType : type
,
137 _indentLevel : co
.currentIndent
,
138 _color : co
.currentColor
142 _log : function (severity
) {
148 for (i
= 1; i
< arguments
.length
; i
++) {
149 if (typeof arguments
[i
] === 'undefined') {
150 logObjectArray
.push(co
.buildLog("undefined", severity
));
151 } else if (arguments
[i
] === null) {
152 logObjectArray
.push(co
.buildLog("null", severity
));
154 logObjectArray
.push(co
.buildLog(arguments
[i
], severity
));
158 for (j
= 0; j
< co
.loggers
.length
; j
++) {
159 co
.loggers
[j
].log
.apply(co
.loggers
[j
], logObjectArray
);
164 this._log
.apply(this, [6].concat(Array
.prototype.slice
.call(arguments
)));
167 debug : function () {
168 this._log
.apply(this, [7].concat(Array
.prototype.slice
.call(arguments
)));
172 this._log
.apply(this, [6].concat(Array
.prototype.slice
.call(arguments
)));
175 notice : function () {
176 this._log
.apply(this, [5].concat(Array
.prototype.slice
.call(arguments
)));
180 this._log
.apply(this, [4].concat(Array
.prototype.slice
.call(arguments
)));
183 error : function () {
184 this._log
.apply(this, [3].concat(Array
.prototype.slice
.call(arguments
)));
193 timeEnd : function () {
196 groupCollapsed : function () {
199 groupEnd : function () {
202 separator : function (type
) {
205 co
._log(7, co
.buildSeparator(type
));
208 space : function (lines
) {
212 if (typeof lines
=== "undefined") {
216 for (i
= 0; i
< lines
; i
++) {
223 indent : function (callback
) {
226 if (typeof callback
=== "function") {
227 co
.currentIndent
= co
.currentIndent
+ co
.indentSize
;
229 co
.currentIndent
= co
.currentIndent
- co
.indentSize
;
231 co
.currentIndent
= co
.currentIndent
+ co
.indentSize
;
237 outdent : function (callback
) {
240 if (typeof callback
=== "function") {
241 co
.currentIndent
= co
.currentIndent
- co
.indentSize
;
242 if (co
.currentIndent
< 0) {
243 co
.currentIndent
= 0;
248 co
.currentIndent
= co
.currentIndent
+ co
.indentSize
;
250 co
.currentIndent
= co
.currentIndent
- co
.indentSize
;
251 if (co
.currentIndent
< 0) {
252 co
.currentIndent
= 0;
259 color : function (color
, callback
) {
261 oldColor
= co
.currentColor
;
263 if (typeof callback
=== "function") {
264 co
.currentColor
= color
;
266 co
.currentColor
= oldColor
;
268 co
.currentColor
= color
;
274 // Returns the current time in microseconds.
276 if (typeof performance
!== 'undefined' && performance
.timing
) {
277 return performance
.timing
.navigationStart
+ performance
.now();
280 if (typeof Microtime
!== 'undefined') {
281 return Microtime
.nowDouble() * 1000;
287 _levelString : function (level
) {
318 if (Cobalt
.Console
.__objectSpy
) {
319 Cobalt
.Console
.__objectSpy
.destroy();