Skip to content

Commit

Permalink
Add filename and line number to log, use log.warn() for deprecations
Browse files Browse the repository at this point in the history
  • Loading branch information
wltsmrz committed Mar 7, 2015
1 parent d3b2d3d commit 90329d3
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 58 deletions.
79 changes: 66 additions & 13 deletions src/js/ripple/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function Log(namespace) {
}

this._prefix = this._namespace.concat(['']).join(': ');
};
}

/**
* Create a sub-logger.
Expand Down Expand Up @@ -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)]));
};
};

Expand All @@ -54,27 +54,78 @@ 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.
*
* This engine has no formatting and works with the most basic of 'console.log'
* 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.
*
Expand All @@ -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;
}

/**
Expand Down
31 changes: 0 additions & 31 deletions src/js/ripple/log.web.js

This file was deleted.

28 changes: 14 additions & 14 deletions src/js/ripple/remote.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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');
}

Expand Down Expand Up @@ -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');
}

Expand Down Expand Up @@ -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');
}

Expand Down Expand Up @@ -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');
}

Expand Down Expand Up @@ -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');
}

Expand Down Expand Up @@ -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');
}

Expand Down Expand Up @@ -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');
}

Expand Down Expand Up @@ -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');
}

Expand Down Expand Up @@ -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');
}

Expand Down Expand Up @@ -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');
}

Expand Down Expand Up @@ -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');
}

Expand Down Expand Up @@ -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');
}

Expand Down

0 comments on commit 90329d3

Please sign in to comment.