diff --git a/src/js/ripple/log.js b/src/js/ripple/log.js index c8469f3c43..5229352c4c 100644 --- a/src/js/ripple/log.js +++ b/src/js/ripple/log.js @@ -11,7 +11,7 @@ function Log(namespace) { } this._prefix = this._namespace.concat(['']).join(': '); -}; +} /** * Create a sub-logger. @@ -43,9 +43,9 @@ Log.prototype._setParent = function(parentLogger) { Log.makeLevel = function(level) { return function() { - var args = Array.prototype.slice.call(arguments); + var args = Array.prototype.slice.apply(arguments); args[0] = this._prefix + args[0]; - Log.engine.logObject.apply(Log, args); + Log.engine.logObject.apply(Log, [level].concat(args[0], [args.slice(2)])); }; }; @@ -54,6 +54,44 @@ Log.prototype.info = Log.makeLevel(2); Log.prototype.warn = Log.makeLevel(3); Log.prototype.error = Log.makeLevel(4); +/** + * @param {String} message + * @param {Array} details + * @return {Array} prepared log info + */ + +function getLogInfo(message, args) { + return [ + // Timestamp + '[' + new Date().toISOString() + ']', + message, + '--', + // Location + (new Error()).stack.split('\n')[4].replace(/^\s+/, ''), + '\n' + ].concat(args); +} + +/** + * @param {Number} log level + * @param {Array} log info + */ + +function logMessage(logLevel, args) { + switch (logLevel) { + case 1: + case 2: + console.log.apply(console, args); + break; + case 3: + console.warn.apply(console, args); + break; + case 4: + console.error.apply(console, args); + break; + } +} + /** * Basic logging connector. * @@ -61,20 +99,33 @@ Log.prototype.error = Log.makeLevel(4); * implementations. This is the logging engine used in Node.js. */ var BasicLogEngine = { - logObject: function logObject(msg) { - var args = Array.prototype.slice.call(arguments, 1); - + logObject: function logObject(level, message, args) { args = args.map(function(arg) { return JSON.stringify(arg, null, 2); }); - args.unshift(msg); - args.unshift('[' + new Date().toISOString() + ']'); - - console.log.apply(console, args); + logMessage(level, getLogInfo(message, args)); } }; +/** + * Log engine for browser consoles. + * + * Browsers tend to have better consoles that support nicely formatted + * JavaScript objects. This connector passes objects through to the logging + * function without any stringification. + */ +var InteractiveLogEngine = { + logObject: function(level, message, args) { + args = args.map(function(arg) { + return /MSIE/.test(navigator.userAgent) + ? JSON.stringify(arg, null, 2) + : arg; + }); + + logMessage(level, getLogInfo(message, args)); + } +}; /** * Null logging connector. * @@ -85,10 +136,12 @@ var NullLogEngine = { logObject: function() {} }; -Log.engine = NullLogEngine; - -if (console && console.log) { +if (typeof window !== 'undefined' && typeof console !== 'undefined') { + Log.engine = InteractiveLogEngine; +} else if (typeof console !== 'undefined' && console.log) { Log.engine = BasicLogEngine; +} else { + Log.engine = NullLogEngine; } /** diff --git a/src/js/ripple/log.web.js b/src/js/ripple/log.web.js deleted file mode 100644 index 2f0246adf9..0000000000 --- a/src/js/ripple/log.web.js +++ /dev/null @@ -1,31 +0,0 @@ -var exports = module.exports = require('./log.js'); - -/** - * Log engine for browser consoles. - * - * Browsers tend to have better consoles that support nicely formatted - * JavaScript objects. This connector passes objects through to the logging - * function without any stringification. - */ -var InteractiveLogEngine = { - logObject: function (msg, obj) { - var args = Array.prototype.slice.call(arguments, 1); - - args = args.map(function(arg) { - if (/MSIE/.test(navigator.userAgent)) { - return JSON.stringify(arg, null, 2); - } else { - return arg; - } - }); - - args.unshift(msg); - args.unshift('[' + new Date().toISOString() + ']'); - - console.log.apply(console, args); - } -}; - -if (window.console && window.console.log) { - exports.Log.engine = InteractiveLogEngine; -} diff --git a/src/js/ripple/remote.js b/src/js/ripple/remote.js index 8508842201..d4a7493d14 100644 --- a/src/js/ripple/remote.js +++ b/src/js/ripple/remote.js @@ -1119,7 +1119,7 @@ Remote.prototype.requestTransactionEntry = function(hash, ledgerHash, callback) ledgerHash = hash.ledger || hash.ledger_hash || hash.ledger_index; hash = hash.hash || hash.tx || hash.transaction; } else { - console.error('DEPRECATED: First argument to request constructor should be' + log.warn('DEPRECATED: First argument to request constructor should be' + ' an object containing request properties'); } @@ -1162,7 +1162,7 @@ Remote.prototype.requestTx = function(hash, callback) { if (typeof hash === 'string') { options = { hash: hash }; - console.error('DEPRECATED: First argument to request constructor should be' + log.warn('DEPRECATED: First argument to request constructor should be' + ' an object containing request properties'); } else { options = hash; @@ -1219,7 +1219,7 @@ Remote.accountRequest = function(type, options, callback) { limit = options.limit; marker = options.marker; } else { - console.error('DEPRECATED: First argument to request constructor should be' + log.warn('DEPRECATED: First argument to request constructor should be' + ' an object containing request properties'); } @@ -1548,7 +1548,7 @@ Remote.prototype.requestTxHistory = function(start, callback) { if (typeof start === 'object') { start = start.start; } else { - console.error('DEPRECATED: First argument to request constructor should be' + log.warn('DEPRECATED: First argument to request constructor should be' + ' an object containing request properties'); } @@ -1588,7 +1588,7 @@ Remote.prototype.requestBookOffers = function(gets, pays, taker, callback) { ledger = options.ledger; limit = options.limit; } else { - console.error('DEPRECATED: First argument to request constructor should be' + log.warn('DEPRECATED: First argument to request constructor should be' + ' an object containing request properties'); } @@ -1654,7 +1654,7 @@ Remote.prototype.requestWalletAccounts = function(seed, callback) { if (typeof seed === 'object') { seed = seed.seed; } else { - console.error('DEPRECATED: First argument to request constructor should be' + log.warn('DEPRECATED: First argument to request constructor should be' + ' an object containing request properties'); } @@ -1682,7 +1682,7 @@ Remote.prototype.requestSign = function(secret, tx_json, callback) { tx_json = secret.tx_json; secret = secret.secret; } else { - console.error('DEPRECATED: First argument to request constructor should be' + log.warn('DEPRECATED: First argument to request constructor should be' + ' an object containing request properties'); } @@ -1805,7 +1805,7 @@ Remote.accountRootRequest = function(type, responseFilter, account, ledger, call ledger = account.ledger; account = account.account; } else { - console.error('DEPRECATED: First argument to request constructor should be' + log.warn('DEPRECATED: First argument to request constructor should be' + ' an object containing request properties'); } @@ -1957,7 +1957,7 @@ Remote.prototype.createPathFind = function(src_account, dst_account, dst_amount, dst_account = options.dst_account; src_account = options.src_account; } else { - console.error('DEPRECATED: First argument to request constructor should be' + log.warn('DEPRECATED: First argument to request constructor should be' + ' an object containing request properties'); } @@ -1998,7 +1998,7 @@ Remote.prototype.createOrderBook = function(currency_gets, issuer_gets, currency issuer_gets = options.issuer_gets; currency_gets = options.currency_gets; } else { - console.error('DEPRECATED: First argument to request constructor should be' + log.warn('DEPRECATED: First argument to request constructor should be' + ' an object containing request properties'); } @@ -2081,7 +2081,7 @@ Remote.prototype.accountSeqCache = function(account, ledger, callback) { ledger = options.ledger; account = options.account; } else { - console.error('DEPRECATED: First argument to request constructor should be' + log.warn('DEPRECATED: First argument to request constructor should be' + ' an object containing request properties'); } @@ -2190,7 +2190,7 @@ Remote.prototype.requestRippleBalance = function(account, issuer, currency, ledg issuer = options.issuer; account = options.account; } else { - console.error('DEPRECATED: First argument to request constructor should be' + log.warn('DEPRECATED: First argument to request constructor should be' + ' an object containing request properties'); } @@ -2279,7 +2279,7 @@ Remote.prototype.requestRipplePathFind = function(src_account, dst_account, dst_ dst_account = options.dst_account; src_account = options.src_account; } else { - console.error('DEPRECATED: First argument to request constructor should be' + log.warn('DEPRECATED: First argument to request constructor should be' + ' an object containing request properties'); } @@ -2316,7 +2316,7 @@ Remote.prototype.requestPathFindCreate = function(src_account, dst_account, dst_ dst_account = options.dst_account; src_account = options.src_account; } else { - console.error('DEPRECATED: First argument to request constructor should be' + log.warn('DEPRECATED: First argument to request constructor should be' + ' an object containing request properties'); }