diff --git a/lib/configuration.js b/lib/configuration.js index f3187a3..2fbfacc 100644 --- a/lib/configuration.js +++ b/lib/configuration.js @@ -78,21 +78,47 @@ confProto.setOptions = function (options) { confProto.debug = false; -confProto.setDebugLevel = function (level) { - var that = this; +var _DEBUG_LEVEL_KEY = 'http2-cache.configuration._debugLevel', + _DEBUG_LEVEL_DEFAULT = false; + +confProto.setDebugLevel = function (debugLevel, persist) { + + debugLevel = typeof debugLevel === 'string' ? debugLevel : debugLevel === true ? 'info' : null; + + // Setter with persist but not 'auto' + if (debugLevel !== 'auto' && persist === true) { - level = typeof level === 'string' ? level : level === true ? 'info' : null; + _DEBUG_LEVEL_DEFAULT = debugLevel; + + // Use localStorage if available + if (typeof localStorage !== 'undefined') { + localStorage.setItem(_DEBUG_LEVEL_KEY, _DEBUG_LEVEL_DEFAULT); + } + + // Compute default value from store then const + } else if (debugLevel === 'auto') { + + // Use localStorage if available + if (typeof localStorage !== 'undefined') { + debugLevel = localStorage.getItem(_DEBUG_LEVEL_KEY, _DEBUG_LEVEL_DEFAULT); + } + + debugLevel = debugLevel || _DEBUG_LEVEL_DEFAULT; + } // Init debug/log - if (that._log && that._log.hasOwnProperty('debugLevel')) { - that._log.debugLevel = level; + if (this._log && this._log.hasOwnProperty('debugLevel')) { + this._log.debugLevel = debugLevel; } - + // Sync current options value - that.options.debug = level; + this.options.debug = debugLevel; // Sync and cast has boolean - that.debug = !!that.options.debug; + this.debug = !!debugLevel; + + // Chainable + return this; }; confProto.configuring = function () { @@ -141,7 +167,8 @@ confProto.getTransport = function (url) { if (uri.protocol === 'ws:' || uri.protocol === 'wss:') { activeTransportConnections[uri.href] = websocket(uri.href, "h2", { // TODO, maybe enable perMessageDeflate in production or on debug?? - perMessageDeflate: that.debug === false + // Do perMessageDeflate false only on browser and if debug cause if make nodeJS spec fail + perMessageDeflate: typeof process !== 'undefined' || that.debug === false }); } else if(uri.protocol === 'tcp:') { diff --git a/lib/http2-cache.js b/lib/http2-cache.js index 747bde4..91dad5a 100644 --- a/lib/http2-cache.js +++ b/lib/http2-cache.js @@ -11,7 +11,7 @@ enableXHROverH2 = require('./xhr.js').enableXHROverH2; enableXHROverH2(XMLHttpRequest, new Configuration({ - debug: false // true='info' or (info|debug|trace) + debug: 'auto' // true='info' or (auto|info|debug|trace) })); // To update debug level after injection: @@ -20,4 +20,7 @@ //- XMLHttpRequest.configuration.setDebugLevel('trace'); } + // To persit update debug level after injection: + //- XMLHttpRequest.configuration.setDebugLevel('debug', true); + }(this)); diff --git a/lib/logger.js b/lib/logger.js index 5206a6f..b958065 100644 --- a/lib/logger.js +++ b/lib/logger.js @@ -13,7 +13,8 @@ var defaultLogger = { }; var consoleLogger = { - debugLevel: 'info', // 'info|debug|trace' + debugLevel: 'log', // 'log|debug|trace' + log: console.log, fatal: console.error, error: console.error, warn : console.warning || console.info, @@ -37,24 +38,26 @@ var consoleLogger = { var args = (typeof ctx === 'string' ? [ctx, data] : arguments); debug.apply(console, args); } + return this; }, - // Trace only + // Displayed when debugLevel is trace only trace: function (data, ctx) { var debug = console.debug || console.info; if (consoleLogger.debugLevel === 'trace') { - consoleLogger.debug(data, ctx); + return consoleLogger.debug(data, ctx); } + return this; }, // Trace only child: function(msg) { if (consoleLogger.debugLevel === 'trace') { - console.info(msg); + return console.info(msg); } - return this; + return this; } }; module.exports = { - consoleLogger: consoleLogger, - defaultLogger: defaultLogger + consoleLogger: consoleLogger, + defaultLogger: defaultLogger }; \ No newline at end of file