]>
git.r.bdr.sh - rbdr/cologne/blob - lib/cobalt.js
d129714c98271e8ed75da9b9d7959147dc2bcd38
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
));
151 logObjectArray
.push(co
.buildLog(arguments
[i
], severity
));
155 for (j
= 0; j
< co
.loggers
.length
; j
++) {
156 co
.loggers
[j
].log
.apply(co
.loggers
[j
], logObjectArray
);
161 this._log
.apply(this, [6].concat(Array
.prototype.slice
.call(arguments
)));
164 debug : function () {
165 this._log
.apply(this, [7].concat(Array
.prototype.slice
.call(arguments
)));
169 this._log
.apply(this, [6].concat(Array
.prototype.slice
.call(arguments
)));
172 notice : function () {
173 this._log
.apply(this, [5].concat(Array
.prototype.slice
.call(arguments
)));
177 this._log
.apply(this, [4].concat(Array
.prototype.slice
.call(arguments
)));
180 error : function () {
181 this._log
.apply(this, [3].concat(Array
.prototype.slice
.call(arguments
)));
190 timeEnd : function () {
193 groupCollapsed : function () {
196 groupEnd : function () {
199 separator : function (type
) {
202 co
._log(7, co
.buildSeparator(type
));
205 space : function (lines
) {
209 if (typeof lines
=== "undefined") {
213 for (i
= 0; i
< lines
; i
++) {
220 indent : function (callback
) {
223 if (typeof callback
=== "function") {
224 co
.currentIndent
= co
.currentIndent
+ co
.indentSize
;
226 co
.currentIndent
= co
.currentIndent
- co
.indentSize
;
228 co
.currentIndent
= co
.currentIndent
+ co
.indentSize
;
234 outdent : function (callback
) {
237 if (typeof callback
=== "function") {
238 co
.currentIndent
= co
.currentIndent
- co
.indentSize
;
239 if (co
.currentIndent
< 0) {
240 co
.currentIndent
= 0;
245 co
.currentIndent
= co
.currentIndent
+ co
.indentSize
;
247 co
.currentIndent
= co
.currentIndent
- co
.indentSize
;
248 if (co
.currentIndent
< 0) {
249 co
.currentIndent
= 0;
256 color : function (color
, callback
) {
258 oldColor
= co
.currentColor
;
260 if (typeof callback
=== "function") {
261 co
.currentColor
= color
;
263 co
.currentColor
= oldColor
;
265 co
.currentColor
= color
;
271 // Returns the current time in microseconds.
273 if (typeof performance
!== 'undefined' && performance
.timing
) {
274 return performance
.timing
.navigationStart
+ performance
.now();
277 if (typeof Microtime
!== 'undefined') {
278 return Microtime
.nowDouble() * 1000;
284 _levelString : function (level
) {
315 if (Cobalt
.Console
.__objectSpy
) {
316 Cobalt
.Console
.__objectSpy
.destroy();