X-Git-Url: https://git.r.bdr.sh/rbdr/cologne/blobdiff_plain/bdedb5e54032363de8afa322fadae0e2cb2132d0..941d09019e402ac565101fba6d824bd48504c90b:/lib/cobalt.js?ds=inline diff --git a/lib/cobalt.js b/lib/cobalt.js index 45f0e26..f3effcd 100644 --- a/lib/cobalt.js +++ b/lib/cobalt.js @@ -6,6 +6,7 @@ var Ne = require('neon'); Module = Ne.Module; Class = Ne.Class; + var Microtime = require('microtime'); } else { Module = global.Module; Class = global.Class; @@ -18,8 +19,6 @@ // Load up loggers + formatters if (typeof require === 'function') { // Formatters - Cobalt.Formatter.Simple = require('./formatters/simple.js').Simple; - Cobalt.Formatter.Ansi = require('./formatters/ansi.js').Ansi; Cobalt.Formatter.Token = require('./formatters/token.js').Token; // Loggers @@ -27,6 +26,35 @@ Cobalt.Logger.Socket = require('./loggers/socket.js').Socket; } + Cobalt.now = function () { + if (typeof performance !== 'undefined' && performance.timing) { + return performance.timing.navigationStart + performance.now(); + } + + if (typeof Microtime !== 'undefined') { + return Microtime.nowDouble() * 1000; + } + + return Date.now(); + } + + // Stringify with circular dereference. + Cobalt.stringify = function (object) { + var cache = [], stringified; + stringified = JSON.stringify(object, function (key, value) { + if (typeof value === 'object' && value !== null) { + if (cache.indexOf(value) !== -1) { + return "[Circular]"; + } + cache.push(value); + } + return value; + }); + cache = null; + + return stringified; + } + Cobalt.Console = Class(Cobalt, 'Console')({ prototype : { from : "Generic Cobalt Logger", @@ -63,23 +91,39 @@ // Builds a Cobalt Log Object buildLog : function (item, level) { - var co = this; + var co = this, oldItem, logObject = {}; + + if (typeof item === "undefined" || item === null || !item._cobaltLog) { + logObject.message = item; + logObject._cobaltLog = true; + logObject._from = co.from; + logObject._level = level || 6; + logObject._levelString = co._levelString(logObject._level); + logObject._version = co.version; + logObject._timestamp = co.now(); + logObject._indentLevel = co.currentIndent; + logObject._color = co.currentColor; + logObject._separator = false; + return logObject; + } - if (!item._cobaltLog) { - if (typeof item !== "object") { - item = { _cobaltLog : true, message : item.toString() }; - } + return item; + }, - item._from = co.from; - item._level = item._level || level || 7; - item._levelString = co._levelString(item._level); - item._version = co.version; - item._timestamp = co.now(); - item._indentLevel = co.currentIndent; - item._color = co.currentColor; + extendLog : function (extendingObject) { + var co = this, logObject, + property; + + logObject = co.buildLog(undefined, 6); + extendingObject = extendingObject || {}; + + for (property in extendingObject) { + if (extendingObject.hasOwnProperty(property)) { + logObject[property] = extendingObject[property]; + } } - return item; + return logObject; }, buildSeparator : function (type) { @@ -90,22 +134,28 @@ _version : co.version, _timestamp : co.now(), _separatorType : type, - _indentLevel : co.currentIndent + _indentLevel : co.currentIndent, + _color : co.currentColor } }, _log : function (severity) { var co = this, logString, + logObjectArray = [], i, j; for (i = 1; i < arguments.length; i++) { - logObject = co.buildLog(arguments[i], severity); - - for (j = 0; j < co.loggers.length; j++) { - co.loggers[j].log(logObject); + if (typeof arguments[i] === 'undefined') { + logObjectArray.push(co.buildLog("undefined", severity)); + } else { + logObjectArray.push(co.buildLog(arguments[i], severity)); } } + + for (j = 0; j < co.loggers.length; j++) { + co.loggers[j].log.apply(co.loggers[j], logObjectArray); + } }, log : function () { @@ -202,12 +252,12 @@ // Returns the current time in microseconds. now : function () { - if (typeof performance !== 'undefined') { + if (typeof performance !== 'undefined' && performance.timing) { return performance.timing.navigationStart + performance.now(); } - if (typeof process !== 'undefined') { - return process.hrtime(); + if (typeof Microtime !== 'undefined') { + return Microtime.nowDouble() * 1000; } return Date.now(); @@ -244,10 +294,16 @@ } }); + if (Cobalt.Console.__objectSpy) { + Cobalt.Console.__objectSpy.destroy(); + } + if (typeof require === 'function') { global.Formatter = Cobalt.Formatter; global.Logger = Cobalt.Logger; global.Console = Cobalt.Console; + global.now = Cobalt.now; + global.stringify = Cobalt.stringify; } else { global.Cobalt = Cobalt; }