]>
git.r.bdr.sh - rbdr/cologne/blob - lib/cobalt.js
5ae39f2577490fcfd41cfdb2e5c7299b4e6e23da
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
.Simple
= require('./formatters/simple.js').Simple
;
23 Cobalt
.Formatter
.Ansi
= require('./formatters/ansi.js').Ansi
;
24 Cobalt
.Formatter
.Token
= require('./formatters/token.js').Token
;
27 Cobalt
.Logger
.JsConsole
= require('./loggers/console.js').JsConsole
;
28 Cobalt
.Logger
.Socket
= require('./loggers/socket.js').Socket
;
31 Cobalt
.now = function () {
32 if (typeof performance
!== 'undefined') {
33 return performance
.timing
.navigationStart
+ performance
.now();
36 if (typeof Microtime
!== 'undefined') {
37 return Microtime
.nowDouble() * 1000;
43 Cobalt
.Console
= Class(Cobalt
, 'Console')({
45 from : "Generic Cobalt Logger",
50 separatorLength : 120,
51 currentColor : "black",
53 // Initialize instance of cobalt console
54 // and extend configuration.
55 init : function (config
) {
60 for (property
in config
) {
61 co
[property
] = config
[property
];
66 addLogger : function (logger
) {
67 this.loggers
.push(logger
);
70 removeLogger : function (logger
) {
73 index
= this.loggers
.indexOf(logger
);
74 this.loggers
.splice(index
, 1);
77 // Builds a Cobalt Log Object
78 buildLog : function (item
, level
) {
79 var co
= this, oldItem
;
81 if (!item
._cobaltLog
) {
82 if (typeof item
!== "object") {
83 item
= {message : item
.toString() };
85 item
.message
= JSON
.stringify(item
);
88 item
._cobaltLog
= true;
90 item
._level
= item
._level
|| level
|| 7;
91 item
._levelString
= co
._levelString(item
._level
);
92 item
._version
= co
.version
;
93 item
._timestamp
= co
.now();
94 item
._indentLevel
= co
.currentIndent
;
95 item
._color
= co
.currentColor
;
101 buildSeparator : function (type
) {
106 _version : co
.version
,
107 _timestamp : co
.now(),
108 _separatorType : type
,
109 _indentLevel : co
.currentIndent
,
110 _color : co
.currentColor
114 _log : function (severity
) {
120 for (i
= 1; i
< arguments
.length
; i
++) {
121 if (typeof arguments
[i
] === 'undefined') {
122 logObjectArray
.push(co
.buildLog("undefined", severity
));
124 logObjectArray
.push(co
.buildLog(arguments
[i
], severity
));
128 for (j
= 0; j
< co
.loggers
.length
; j
++) {
129 co
.loggers
[j
].log
.apply(co
.loggers
[j
], logObjectArray
);
134 this._log
.apply(this, [7].concat([].splice
.call(arguments
, 0)));
138 this._log
.apply(this, [6].concat([].splice
.call(arguments
, 0)));
141 notice : function () {
142 this._log
.apply(this, [5].concat([].splice
.call(arguments
, 0)));
146 this._log
.apply(this, [4].concat([].splice
.call(arguments
, 0)));
149 error : function () {
150 this._log
.apply(this, [3].concat([].splice
.call(arguments
, 0)));
153 separator : function (type
) {
156 co
._log(7, co
.buildSeparator(type
));
159 space : function (lines
) {
163 if (typeof lines
=== "undefined") {
167 for (i
= 0; i
< lines
; i
++) {
174 indent : function (callback
) {
177 if (typeof callback
=== "function") {
178 co
.currentIndent
= co
.currentIndent
+ co
.indentSize
;
180 co
.currentIndent
= co
.currentIndent
- co
.indentSize
;
182 co
.currentIndent
= co
.currentIndent
+ co
.indentSize
;
188 outdent : function (callback
) {
191 if (typeof callback
=== "function") {
192 co
.currentIndent
= co
.currentIndent
- co
.indentSize
;
193 if (co
.currentIndent
< 0) {
194 co
.currentIndent
= 0;
199 co
.currentIndent
= co
.currentIndent
+ co
.indentSize
;
201 co
.currentIndent
= co
.currentIndent
- co
.indentSize
;
202 if (co
.currentIndent
< 0) {
203 co
.currentIndent
= 0;
210 color : function (color
, callback
) {
212 oldColor
= co
.currentColor
;
214 if (typeof callback
=== "function") {
215 co
.currentColor
= color
;
217 co
.currentColor
= oldColor
;
219 co
.currentColor
= color
;
225 // Returns the current time in microseconds.
227 if (typeof performance
!== 'undefined') {
228 return performance
.timing
.navigationStart
+ performance
.now();
231 if (typeof Microtime
!== 'undefined') {
232 return Microtime
.nowDouble() * 1000;
238 _levelString : function (level
) {
269 if (Cobalt
.Console
.__objectSpy
) {
270 Cobalt
.Console
.__objectSpy
.destroy();
273 if (typeof require
=== 'function') {
274 global
.Formatter
= Cobalt
.Formatter
;
275 global
.Logger
= Cobalt
.Logger
;
276 global
.Console
= Cobalt
.Console
;
278 global
.Cobalt
= Cobalt
;
281 }(typeof window
!== 'undefined' ? window : (typeof module
.exports
!== 'undefined' ? module
.exports : self
)));