]>
git.r.bdr.sh - rbdr/cologne/blob - lib/cobalt.js
9519bedd0b0f6157abf7d5b0a80d6260ab21de53
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');
17 require('./loggers/console.js');
18 require('./loggers/socket.js');
21 Cobalt
.now = function () {
22 if (typeof performance
!== 'undefined' && performance
.timing
) {
23 return performance
.timing
.navigationStart
+ performance
.now();
26 if (typeof Microtime
!== 'undefined') {
27 return Microtime
.nowDouble() * 1000;
33 // Stringify with circular dereference.
34 Cobalt
.stringify = function (object
) {
35 var cache
= [], stringified
;
36 stringified
= JSON
.stringify(object
, function (key
, value
) {
37 if (typeof value
=== 'object' && value
!== null) {
38 if (cache
.indexOf(value
) !== -1) {
50 Class(Cobalt
, 'Console')({
52 from : "Generic Cobalt Logger",
57 separatorLength : 120,
58 currentColor : "black",
60 // Initialize instance of cobalt console
61 // and extend configuration.
62 init : function (config
) {
67 for (property
in config
) {
68 co
[property
] = config
[property
];
73 addLogger : function (logger
) {
74 this.loggers
.push(logger
);
77 removeLogger : function (logger
) {
80 index
= this.loggers
.indexOf(logger
);
81 this.loggers
.splice(index
, 1);
84 // Builds a Cobalt Log Object
85 buildLog : function (item
, level
) {
86 var co
= this, oldItem
, logObject
= {};
88 if (typeof item
=== "undefined" || item
=== null || !item
._cobaltLog
) {
89 logObject
.message
= item
;
90 logObject
._cobaltLog
= true;
91 logObject
._from
= co
.from;
92 logObject
._level
= level
|| 6;
93 logObject
._levelString
= co
._levelString(logObject
._level
);
94 logObject
._version
= co
.version
;
95 logObject
._timestamp
= co
.now();
96 logObject
._indentLevel
= co
.currentIndent
;
97 logObject
._color
= co
.currentColor
;
98 logObject
._separator
= false;
102 if (item
._cobaltLog
) {
103 item
._level
= level
|| item
._level
|| 6;
104 item
._levelString
= co
._levelString(item
._level
);
110 extendLog : function (extendingObject
) {
111 var co
= this, logObject
,
114 logObject
= co
.buildLog(undefined, 6);
115 extendingObject
= extendingObject
|| {};
117 for (property
in extendingObject
) {
118 if (extendingObject
.hasOwnProperty(property
)) {
119 logObject
[property
] = extendingObject
[property
];
126 buildSeparator : function (type
) {
131 _version : co
.version
,
132 _timestamp : co
.now(),
133 _separatorType : type
,
134 _indentLevel : co
.currentIndent
,
135 _color : co
.currentColor
139 _log : function (severity
) {
145 for (i
= 1; i
< arguments
.length
; i
++) {
146 if (typeof arguments
[i
] === 'undefined') {
147 logObjectArray
.push(co
.buildLog("undefined", severity
));
149 logObjectArray
.push(co
.buildLog(arguments
[i
], severity
));
153 for (j
= 0; j
< co
.loggers
.length
; j
++) {
154 co
.loggers
[j
].log
.apply(co
.loggers
[j
], logObjectArray
);
159 this._log
.apply(this, [6].concat(Array
.prototype.slice
.call(arguments
)));
162 debug : function () {
163 this._log
.apply(this, [7].concat(Array
.prototype.slice
.call(arguments
)));
167 this._log
.apply(this, [6].concat(Array
.prototype.slice
.call(arguments
)));
170 notice : function () {
171 this._log
.apply(this, [5].concat(Array
.prototype.slice
.call(arguments
)));
175 this._log
.apply(this, [4].concat(Array
.prototype.slice
.call(arguments
)));
178 error : function () {
179 this._log
.apply(this, [3].concat(Array
.prototype.slice
.call(arguments
)));
188 timeEnd : function () {
191 groupCollapsed : function () {
194 groupEnd : function () {
197 separator : function (type
) {
200 co
._log(7, co
.buildSeparator(type
));
203 space : function (lines
) {
207 if (typeof lines
=== "undefined") {
211 for (i
= 0; i
< lines
; i
++) {
218 indent : function (callback
) {
221 if (typeof callback
=== "function") {
222 co
.currentIndent
= co
.currentIndent
+ co
.indentSize
;
224 co
.currentIndent
= co
.currentIndent
- co
.indentSize
;
226 co
.currentIndent
= co
.currentIndent
+ co
.indentSize
;
232 outdent : function (callback
) {
235 if (typeof callback
=== "function") {
236 co
.currentIndent
= co
.currentIndent
- co
.indentSize
;
237 if (co
.currentIndent
< 0) {
238 co
.currentIndent
= 0;
243 co
.currentIndent
= co
.currentIndent
+ co
.indentSize
;
245 co
.currentIndent
= co
.currentIndent
- co
.indentSize
;
246 if (co
.currentIndent
< 0) {
247 co
.currentIndent
= 0;
254 color : function (color
, callback
) {
256 oldColor
= co
.currentColor
;
258 if (typeof callback
=== "function") {
259 co
.currentColor
= color
;
261 co
.currentColor
= oldColor
;
263 co
.currentColor
= color
;
269 // Returns the current time in microseconds.
271 if (typeof performance
!== 'undefined' && performance
.timing
) {
272 return performance
.timing
.navigationStart
+ performance
.now();
275 if (typeof Microtime
!== 'undefined') {
276 return Microtime
.nowDouble() * 1000;
282 _levelString : function (level
) {
313 if (Cobalt
.Console
.__objectSpy
) {
314 Cobalt
.Console
.__objectSpy
.destroy();