[ReactNative] improve console logging a little bit

This commit is contained in:
Spencer Ahrens 2015-05-04 18:34:29 -07:00
parent 830646529a
commit 95a120b663
1 changed files with 18 additions and 9 deletions

View File

@ -35,25 +35,34 @@
function getNativeLogFunction(level) { function getNativeLogFunction(level) {
return function() { return function() {
var str = Array.prototype.map.call(arguments, function(arg) { var str = Array.prototype.map.call(arguments, function(arg) {
if (arg == null) { var ret;
return arg === null ? 'null' : 'undefined'; var type = typeof arg;
} else if (typeof arg === 'string') { if (arg === null) {
return '"' + arg + '"'; ret = 'null';
} else if (arg === undefined) {
ret = 'undefined';
} else if (type === 'string') {
ret = '"' + arg + '"';
} else if (type === 'function') {
try {
ret = arg.toString();
} catch (e) {
ret = '[function unknown]';
}
} else { } else {
// Perform a try catch, just in case the object has a circular // Perform a try catch, just in case the object has a circular
// reference or stringify throws for some other reason. // reference or stringify throws for some other reason.
try { try {
return JSON.stringify(arg); ret = JSON.stringify(arg);
} catch (e) { } catch (e) {
if (typeof arg.toString === 'function') { if (typeof arg.toString === 'function') {
try { try {
return arg.toString(); ret = arg.toString();
} catch (E) { } catch (E) {}
return 'unknown';
}
} }
} }
} }
return ret || '["' + type + '" failed to stringify]';
}).join(', '); }).join(', ');
global.nativeLoggingHook(str, level); global.nativeLoggingHook(str, level);
}; };