]>
git.r.bdr.sh - rbdr/cologne/blob - lib/cobalt.js
9554a04ad248b7f3ef06412448e8aa387de350b1
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') {
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
;
96 if (!item
._cobaltLog
) {
97 if (typeof item
!== "object") {
98 item
= {message : item
.toString() };
100 item
.message
= Cobalt
.stringify(item
);
103 item
._cobaltLog
= true;
104 item
._from
= co
.from;
105 item
._level
= item
._level
|| level
|| 7;
106 item
._levelString
= co
._levelString(item
._level
);
107 item
._version
= co
.version
;
108 item
._timestamp
= co
.now();
109 item
._indentLevel
= co
.currentIndent
;
110 item
._color
= co
.currentColor
;
116 buildSeparator : function (type
) {
121 _version : co
.version
,
122 _timestamp : co
.now(),
123 _separatorType : type
,
124 _indentLevel : co
.currentIndent
,
125 _color : co
.currentColor
129 _log : function (severity
) {
135 for (i
= 1; i
< arguments
.length
; i
++) {
136 if (typeof arguments
[i
] === 'undefined') {
137 logObjectArray
.push(co
.buildLog("undefined", severity
));
139 logObjectArray
.push(co
.buildLog(arguments
[i
], severity
));
143 for (j
= 0; j
< co
.loggers
.length
; j
++) {
144 co
.loggers
[j
].log
.apply(co
.loggers
[j
], logObjectArray
);
149 this._log
.apply(this, [7].concat([].splice
.call(arguments
, 0)));
153 this._log
.apply(this, [6].concat([].splice
.call(arguments
, 0)));
156 notice : function () {
157 this._log
.apply(this, [5].concat([].splice
.call(arguments
, 0)));
161 this._log
.apply(this, [4].concat([].splice
.call(arguments
, 0)));
164 error : function () {
165 this._log
.apply(this, [3].concat([].splice
.call(arguments
, 0)));
168 separator : function (type
) {
171 co
._log(7, co
.buildSeparator(type
));
174 space : function (lines
) {
178 if (typeof lines
=== "undefined") {
182 for (i
= 0; i
< lines
; i
++) {
189 indent : function (callback
) {
192 if (typeof callback
=== "function") {
193 co
.currentIndent
= co
.currentIndent
+ co
.indentSize
;
195 co
.currentIndent
= co
.currentIndent
- co
.indentSize
;
197 co
.currentIndent
= co
.currentIndent
+ co
.indentSize
;
203 outdent : function (callback
) {
206 if (typeof callback
=== "function") {
207 co
.currentIndent
= co
.currentIndent
- co
.indentSize
;
208 if (co
.currentIndent
< 0) {
209 co
.currentIndent
= 0;
214 co
.currentIndent
= co
.currentIndent
+ co
.indentSize
;
216 co
.currentIndent
= co
.currentIndent
- co
.indentSize
;
217 if (co
.currentIndent
< 0) {
218 co
.currentIndent
= 0;
225 color : function (color
, callback
) {
227 oldColor
= co
.currentColor
;
229 if (typeof callback
=== "function") {
230 co
.currentColor
= color
;
232 co
.currentColor
= oldColor
;
234 co
.currentColor
= color
;
240 // Returns the current time in microseconds.
242 if (typeof performance
!== 'undefined') {
243 return performance
.timing
.navigationStart
+ performance
.now();
246 if (typeof Microtime
!== 'undefined') {
247 return Microtime
.nowDouble() * 1000;
253 _levelString : function (level
) {
284 if (Cobalt
.Console
.__objectSpy
) {
285 Cobalt
.Console
.__objectSpy
.destroy();
288 if (typeof require
=== 'function') {
289 global
.Formatter
= Cobalt
.Formatter
;
290 global
.Logger
= Cobalt
.Logger
;
291 global
.Console
= Cobalt
.Console
;
292 global
.now
= Cobalt
.now
;
293 global
.stringify
= Cobalt
.stringify
;
295 global
.Cobalt
= Cobalt
;
298 }(typeof window
!== 'undefined' ? window : (typeof module
.exports
!== 'undefined' ? module
.exports : self
)));