X-Git-Url: https://git.r.bdr.sh/rbdr/cologne/blobdiff_plain/71a808fe142f1c10f9e7de90877fb6e4ffc70158..c315b8c3c28373aeb28889cf4b09041d98cd5320:/lib/cobalt.js diff --git a/lib/cobalt.js b/lib/cobalt.js index 5ae39f2..d2d2c13 100644 --- a/lib/cobalt.js +++ b/lib/cobalt.js @@ -19,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 @@ -29,7 +27,7 @@ } Cobalt.now = function () { - if (typeof performance !== 'undefined') { + if (typeof performance !== 'undefined' && performance.timing) { return performance.timing.navigationStart + performance.now(); } @@ -40,6 +38,23 @@ 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", @@ -76,23 +91,19 @@ // Builds a Cobalt Log Object buildLog : function (item, level) { - var co = this, oldItem; + var co = this, oldItem, logObject = {}; if (!item._cobaltLog) { - if (typeof item !== "object") { - item = {message : item.toString() }; - } else { - item.message = JSON.stringify(item); - } - - item._cobaltLog = true; - 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; + logObject.message = item; + logObject._cobaltLog = true; + logObject._from = co.from; + logObject._level = item._level || level || 7; + logObject._levelString = co._levelString(item._level); + logObject._version = co.version; + logObject._timestamp = co.now(); + logObject._indentLevel = co.currentIndent; + logObject._color = co.currentColor; + return logObject; } return item; @@ -224,7 +235,7 @@ // Returns the current time in microseconds. now : function () { - if (typeof performance !== 'undefined') { + if (typeof performance !== 'undefined' && performance.timing) { return performance.timing.navigationStart + performance.now(); } @@ -274,6 +285,8 @@ global.Formatter = Cobalt.Formatter; global.Logger = Cobalt.Logger; global.Console = Cobalt.Console; + global.now = Cobalt.now; + global.stringify = Cobalt.stringify; } else { global.Cobalt = Cobalt; }