]>
git.r.bdr.sh - rbdr/cologne/blob - lib/cobalt.js
9f7f6c08a0dfc6dcac8bb29b032de8c4c2b42786
4 // Load up dependencies
5 if (typeof require
=== 'function') {
6 var Ne
= require('neon');
9 var Microtime
= require('microtime');
11 Module
= global
.Module
;
16 Module(Cobalt
, 'Logger')({});
17 Module(Cobalt
, 'Formatter')({});
19 // Load up loggers + formatters
20 if (typeof require
=== 'function') {
22 Cobalt
.Formatter
.Token
= require('./formatters/token.js').Token
;
25 Cobalt
.Logger
.JsConsole
= require('./loggers/console.js').JsConsole
;
26 Cobalt
.Logger
.Socket
= require('./loggers/socket.js').Socket
;
29 Cobalt
.now = function () {
30 if (typeof performance
!== 'undefined' && performance
.timing
) {
31 return performance
.timing
.navigationStart
+ performance
.now();
34 if (typeof Microtime
!== 'undefined') {
35 return Microtime
.nowDouble() * 1000;
41 // Stringify with circular dereference.
42 Cobalt
.stringify = function (object
) {
43 var cache
= [], stringified
;
44 stringified
= JSON
.stringify(object
, function (key
, value
) {
45 if (typeof value
=== 'object' && value
!== null) {
46 if (cache
.indexOf(value
) !== -1) {
58 Cobalt
.Console
= Class(Cobalt
, 'Console')({
60 from : "Generic Cobalt Logger",
65 separatorLength : 120,
66 currentColor : "black",
68 // Initialize instance of cobalt console
69 // and extend configuration.
70 init : function (config
) {
75 for (property
in config
) {
76 co
[property
] = config
[property
];
81 addLogger : function (logger
) {
82 this.loggers
.push(logger
);
85 removeLogger : function (logger
) {
88 index
= this.loggers
.indexOf(logger
);
89 this.loggers
.splice(index
, 1);
92 // Builds a Cobalt Log Object
93 buildLog : function (item
, level
) {
94 var co
= this, oldItem
, logObject
= {};
96 if (typeof item
=== "undefined" || item
=== null || !item
._cobaltLog
) {
97 logObject
.message
= item
;
98 logObject
._cobaltLog
= true;
99 logObject
._from
= co
.from;
100 logObject
._level
= level
|| 6;
101 logObject
._levelString
= co
._levelString(logObject
._level
);
102 logObject
._version
= co
.version
;
103 logObject
._timestamp
= co
.now();
104 logObject
._indentLevel
= co
.currentIndent
;
105 logObject
._color
= co
.currentColor
;
106 logObject
._separator
= false;
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
));
152 logObjectArray
.push(co
.buildLog(arguments
[i
], severity
));
156 for (j
= 0; j
< co
.loggers
.length
; j
++) {
157 co
.loggers
[j
].log
.apply(co
.loggers
[j
], logObjectArray
);
162 this._log
.apply(this, [7].concat(Array
.prototype.slice
.call(arguments
)));
165 debug : function () {
166 this._log
.apply(this, [7].concat(Array
.prototype.slice
.call(arguments
)));
170 this._log
.apply(this, [6].concat(Array
.prototype.slice
.call(arguments
)));
173 notice : function () {
174 this._log
.apply(this, [5].concat(Array
.prototype.slice
.call(arguments
)));
178 this._log
.apply(this, [4].concat(Array
.prototype.slice
.call(arguments
)));
181 error : function () {
182 this._log
.apply(this, [3].concat(Array
.prototype.slice
.call(arguments
)));
191 timeEnd : function () {
194 groupCollapsed : function () {
197 groupEnd : function () {
200 separator : function (type
) {
203 co
._log(7, co
.buildSeparator(type
));
206 space : function (lines
) {
210 if (typeof lines
=== "undefined") {
214 for (i
= 0; i
< lines
; i
++) {
221 indent : function (callback
) {
224 if (typeof callback
=== "function") {
225 co
.currentIndent
= co
.currentIndent
+ co
.indentSize
;
227 co
.currentIndent
= co
.currentIndent
- co
.indentSize
;
229 co
.currentIndent
= co
.currentIndent
+ co
.indentSize
;
235 outdent : function (callback
) {
238 if (typeof callback
=== "function") {
239 co
.currentIndent
= co
.currentIndent
- co
.indentSize
;
240 if (co
.currentIndent
< 0) {
241 co
.currentIndent
= 0;
246 co
.currentIndent
= co
.currentIndent
+ co
.indentSize
;
248 co
.currentIndent
= co
.currentIndent
- co
.indentSize
;
249 if (co
.currentIndent
< 0) {
250 co
.currentIndent
= 0;
257 color : function (color
, callback
) {
259 oldColor
= co
.currentColor
;
261 if (typeof callback
=== "function") {
262 co
.currentColor
= color
;
264 co
.currentColor
= oldColor
;
266 co
.currentColor
= color
;
272 // Returns the current time in microseconds.
274 if (typeof performance
!== 'undefined' && performance
.timing
) {
275 return performance
.timing
.navigationStart
+ performance
.now();
278 if (typeof Microtime
!== 'undefined') {
279 return Microtime
.nowDouble() * 1000;
285 _levelString : function (level
) {
316 if (Cobalt
.Console
.__objectSpy
) {
317 Cobalt
.Console
.__objectSpy
.destroy();
320 if (typeof require
=== 'function') {
321 global
.Formatter
= Cobalt
.Formatter
;
322 global
.Logger
= Cobalt
.Logger
;
323 global
.Console
= Cobalt
.Console
;
324 global
.now
= Cobalt
.now
;
325 global
.stringify
= Cobalt
.stringify
;
327 global
.Cobalt
= Cobalt
;
330 }(typeof window
!== 'undefined' ? window : (typeof module
.exports
!== 'undefined' ? module
.exports : self
)));