From 174c573c86eb88507f42727ce1ca01070aa3af84 Mon Sep 17 00:00:00 2001 From: James Ide Date: Mon, 14 Dec 2015 12:28:34 -0800 Subject: [PATCH] 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 --- .../src/Resolver/polyfills/console.js | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/react-packager/src/Resolver/polyfills/console.js b/react-packager/src/Resolver/polyfills/console.js index 5461602b..6aa48550 100644 --- a/react-packager/src/Resolver/polyfills/console.js +++ b/react-packager/src/Resolver/polyfills/console.js @@ -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); }; } });