Skip to content

Commit

Permalink
[ReactNative] improve console logging a little bit
Browse files Browse the repository at this point in the history
  • Loading branch information
sahrens committed May 5, 2015
1 parent 50de4a6 commit 66d2f60
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
11 changes: 9 additions & 2 deletions Libraries/Utilities/stringifySafe.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,19 @@
*/
function stringifySafe(arg: any): string {
var ret;
var type = typeof arg;
if (arg === undefined) {
ret = 'undefined';
} else if (arg === null) {
ret = 'null';
} else if (typeof arg === 'string') {
} else if (type === 'string') {
ret = '"' + arg + '"';
} else if (type === 'function') {
try {
ret = arg.toString();
} catch (e) {
ret = '[function unknown]';
}
} else {
// Perform a try catch, just in case the object has a circular
// reference or stringify throws for some other reason.
Expand All @@ -36,7 +43,7 @@ function stringifySafe(arg: any): string {
}
}
}
return ret || '["' + typeof arg + '" failed to stringify]';
return ret || '["' + type + '" failed to stringify]';
}

module.exports = stringifySafe;
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,34 @@
function getNativeLogFunction(level) {
return function() {
var str = Array.prototype.map.call(arguments, function(arg) {
if (arg == null) {
return arg === null ? 'null' : 'undefined';
} else if (typeof arg === 'string') {
return '"' + arg + '"';
var ret;
var type = typeof arg;
if (arg === null) {
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 {
// Perform a try catch, just in case the object has a circular
// reference or stringify throws for some other reason.
try {
return JSON.stringify(arg);
ret = JSON.stringify(arg);
} catch (e) {
if (typeof arg.toString === 'function') {
try {
return arg.toString();
} catch (E) {
return 'unknown';
}
ret = arg.toString();
} catch (E) {}
}
}
}
return ret || '["' + type + '" failed to stringify]';
}).join(', ');
global.nativeLoggingHook(str, level);
};
Expand Down

0 comments on commit 66d2f60

Please sign in to comment.