]>
git.r.bdr.sh - rbdr/cologne/blob - lib/cobalt.js
4c6944d2617a64b9a009cf8748f19325f73c0cf7
1 // Load up dependencies
2 if (typeof require
=== 'function') {
4 var Microtime
= require('microtime');
7 Cobalt
= Module("Cobalt");
8 Module(Cobalt
, 'Logger')({});
9 Module(Cobalt
, 'Formatter')({});
11 // Load up loggers + formatters
12 if (typeof require
=== 'function') {
14 require('./formatters/token.js');
15 require('./formatters/simple.js');
18 require('./loggers/console.js');
19 require('./loggers/socket.js');
20 require('./loggers/file.js');
23 Cobalt
.now = function () {
24 if (typeof performance
!== 'undefined' && performance
.timing
) {
25 return performance
.timing
.navigationStart
+ performance
.now();
28 if (typeof Microtime
!== 'undefined') {
29 return Microtime
.nowDouble() * 1000;
35 // Stringify with circular dereference.
36 Cobalt
.stringify = function (object
) {
37 var cache
= [], stringified
;
38 stringified
= JSON
.stringify(object
, function (key
, value
) {
39 if (typeof value
=== 'object' && value
!== null) {
40 if (cache
.indexOf(value
) !== -1) {
52 Class(Cobalt
, 'Console')({
54 from : "Generic Cobalt Logger",
59 separatorLength : 120,
60 currentColor : "black",
62 // Initialize instance of cobalt console
63 // and extend configuration.
64 init : function (config
) {
69 for (property
in config
) {
70 co
[property
] = config
[property
];
75 addLogger : function (logger
) {
76 this.loggers
.push(logger
);
79 removeLogger : function (logger
) {
82 index
= this.loggers
.indexOf(logger
);
83 this.loggers
.splice(index
, 1);
86 // Builds a Cobalt Log Object
87 buildLog : function (item
, level
) {
88 var co
= this, oldItem
, logObject
= {};
90 if (typeof item
=== "undefined" || item
=== null || !item
._cobaltLog
) {
91 logObject
.message
= item
;
92 logObject
._cobaltLog
= true;
93 logObject
._from
= co
.from;
94 logObject
._level
= level
|| 6;
95 logObject
._levelString
= co
._levelString(logObject
._level
);
96 logObject
._version
= co
.version
;
97 logObject
._timestamp
= co
.now();
98 logObject
._indentLevel
= co
.currentIndent
;
99 logObject
._color
= co
.currentColor
;
100 logObject
._separator
= false;
104 if (item
._cobaltLog
) {
105 item
._level
= level
|| item
._level
|| 6;
106 item
._levelString
= co
._levelString(item
._level
);
112 extendLog : function (extendingObject
) {
113 var co
= this, logObject
,
116 logObject
= co
.buildLog(undefined, 6);
117 extendingObject
= extendingObject
|| {};
119 for (property
in extendingObject
) {
120 if (extendingObject
.hasOwnProperty(property
)) {
121 logObject
[property
] = extendingObject
[property
];
128 buildSeparator : function (type
) {
133 _version : co
.version
,
134 _timestamp : co
.now(),
135 _separatorType : type
,
136 _indentLevel : co
.currentIndent
,
137 _color : co
.currentColor
141 _log : function (severity
) {
147 for (i
= 1; i
< arguments
.length
; i
++) {
148 if (typeof arguments
[i
] === 'undefined') {
149 logObjectArray
.push(co
.buildLog("undefined", severity
));
150 } else if (arguments
[i
] === null) {
151 logObjectArray
.push(co
.buildLog("null", severity
));
153 logObjectArray
.push(co
.buildLog(arguments
[i
], severity
));
157 for (j
= 0; j
< co
.loggers
.length
; j
++) {
158 co
.loggers
[j
].log
.apply(co
.loggers
[j
], logObjectArray
);
163 this._log
.apply(this, [6].concat(Array
.prototype.slice
.call(arguments
)));
166 debug : function () {
167 this._log
.apply(this, [7].concat(Array
.prototype.slice
.call(arguments
)));
171 this._log
.apply(this, [6].concat(Array
.prototype.slice
.call(arguments
)));
174 notice : function () {
175 this._log
.apply(this, [5].concat(Array
.prototype.slice
.call(arguments
)));
179 this._log
.apply(this, [4].concat(Array
.prototype.slice
.call(arguments
)));
182 error : function () {
183 this._log
.apply(this, [3].concat(Array
.prototype.slice
.call(arguments
)));
192 timeEnd : function () {
195 groupCollapsed : function () {
198 groupEnd : function () {
201 separator : function (type
) {
204 co
._log(7, co
.buildSeparator(type
));
207 space : function (lines
) {
211 if (typeof lines
=== "undefined") {
215 for (i
= 0; i
< lines
; i
++) {
222 indent : function (callback
) {
225 if (typeof callback
=== "function") {
226 co
.currentIndent
= co
.currentIndent
+ co
.indentSize
;
228 co
.currentIndent
= co
.currentIndent
- co
.indentSize
;
230 co
.currentIndent
= co
.currentIndent
+ co
.indentSize
;
236 outdent : function (callback
) {
239 if (typeof callback
=== "function") {
240 co
.currentIndent
= co
.currentIndent
- co
.indentSize
;
241 if (co
.currentIndent
< 0) {
242 co
.currentIndent
= 0;
247 co
.currentIndent
= co
.currentIndent
+ co
.indentSize
;
249 co
.currentIndent
= co
.currentIndent
- co
.indentSize
;
250 if (co
.currentIndent
< 0) {
251 co
.currentIndent
= 0;
258 color : function (color
, callback
) {
260 oldColor
= co
.currentColor
;
262 if (typeof callback
=== "function") {
263 co
.currentColor
= color
;
265 co
.currentColor
= oldColor
;
267 co
.currentColor
= color
;
273 // Returns the current time in microseconds.
275 if (typeof performance
!== 'undefined' && performance
.timing
) {
276 return performance
.timing
.navigationStart
+ performance
.now();
279 if (typeof Microtime
!== 'undefined') {
280 return Microtime
.nowDouble() * 1000;
286 _levelString : function (level
) {
317 if (Cobalt
.Console
.__objectSpy
) {
318 Cobalt
.Console
.__objectSpy
.destroy();