Keep the original console as `global.originalConsole`

Summary:
If a console exists, keep the original as `global.originalConsole` before overwriting `global.console` with a polyfill. This matches what we do for XHR, fetch, and some other libraries.
Closes https://github.com/facebook/react-native/pull/3322

Reviewed By: svcscm

Differential Revision: D2755873

Pulled By: androidtrunkagent

fb-gh-sync-id: 4c23f807b73b79cfa9fbbd4e2814d76eecabd596
This commit is contained in:
James Ide 2015-12-14 12:28:34 -08:00 committed by facebook-github-bot-4
parent ae4ea67fa6
commit 174c573c86
1 changed files with 14 additions and 7 deletions

View File

@ -366,8 +366,6 @@
};
function setupConsole(global) {
var originalConsole = global.console;
if (!global.nativeLoggingHook) {
return;
}
@ -461,7 +459,14 @@
global.nativeLoggingHook('\n' + table.join('\n'), LOG_LEVELS.info);
}
global.console = {
// Preserve the original `console` as `originalConsole`
var originalConsole = global.console;
var descriptor = Object.getOwnPropertyDescriptor(global, 'console');
if (descriptor) {
Object.defineProperty(global, 'originalConsole', descriptor);
}
var console = {
error: getNativeLogFunction(LOG_LEVELS.error),
info: getNativeLogFunction(LOG_LEVELS.info),
log: getNativeLogFunction(LOG_LEVELS.info),
@ -469,17 +474,19 @@
trace: getNativeLogFunction(LOG_LEVELS.trace),
table: consoleTablePolyfill
};
descriptor.value = console;
Object.defineProperty(global, 'console', descriptor);
// If available, also call the original `console` method since that is
// sometimes useful. Ex: on OS X, this will let you see rich output in
// the Safari Web Inspector console.
if (__DEV__ && originalConsole) {
Object.keys(global.console).forEach(methodName => {
var reactNativeMethod = global.console[methodName];
Object.keys(console).forEach(methodName => {
var reactNativeMethod = console[methodName];
if (originalConsole[methodName]) {
global.console[methodName] = function() {
console[methodName] = function() {
originalConsole[methodName](...arguments);
reactNativeMethod.apply(global.console, arguments);
reactNativeMethod.apply(console, arguments);
};
}
});