diff --git a/Libraries/ReactNative/YellowBox.js b/Libraries/ReactNative/YellowBox.js index d6d2514b8..a21a01a8b 100644 --- a/Libraries/ReactNative/YellowBox.js +++ b/Libraries/ReactNative/YellowBox.js @@ -16,10 +16,12 @@ const EventEmitter = require('EventEmitter'); const Platform = require('Platform'); const React = require('React'); const StyleSheet = require('StyleSheet'); +const RCTLog = require('RCTLog'); const infoLog = require('infoLog'); const openFileInEditor = require('openFileInEditor'); const parseErrorStack = require('parseErrorStack'); +const stringifySafe = require('stringifySafe'); const symbolicateStackTrace = require('symbolicateStackTrace'); import type EmitterSubscription from 'EmitterSubscription'; @@ -74,18 +76,16 @@ if (__DEV__) { (console: any).warn = function() { warn.apply(console, arguments); - - if (typeof arguments[0] === 'string' && - arguments[0].startsWith('(ADVICE)')) { - return; - } - updateWarningMap.apply(null, arguments); }; if (Platform.isTesting) { (console: any).disableYellowBox = true; } + + RCTLog.setWarningHandler((...args) => { + updateWarningMap.apply(null, args); + }); } /** @@ -106,7 +106,6 @@ function updateWarningMap(format, ...args): void { if (console.disableYellowBox) { return; } - const stringifySafe = require('stringifySafe'); format = String(format); const argCount = (format.match(/%s/g) || []).length; @@ -115,6 +114,10 @@ function updateWarningMap(format, ...args): void { ...args.slice(argCount).map(stringifySafe), ].join(' '); + if (warning.startsWith('(ADVICE)')) { + return; + } + const warningInfo = _warningMap.get(warning); if (warningInfo) { warningInfo.count += 1; diff --git a/Libraries/Utilities/RCTLog.js b/Libraries/Utilities/RCTLog.js index 10f2857b7..6d09e8a92 100644 --- a/Libraries/Utilities/RCTLog.js +++ b/Libraries/Utilities/RCTLog.js @@ -21,28 +21,35 @@ const levelsMap = { fatal: 'error', }; -class RCTLog { - // level one of log, info, warn, error, mustfix - static logIfNoNativeHook(...args) { - if (typeof global.nativeLoggingHook === 'undefined') { - // We already printed in xcode, so only log here if using a js debugger - RCTLog.logToConsole(...args); - } +let warningHandler: ?(Array => void) = null; - return true; - } +const RCTLog = { + // level one of log, info, warn, error, mustfix + logIfNoNativeHook(level: string, ...args: Array): void { + // We already printed in the native console, so only log here if using a js debugger + if (typeof global.nativeLoggingHook === 'undefined') { + RCTLog.logToConsole(level, ...args); + } else { + // Report native warnings to YellowBox + if (warningHandler && level === 'warn') { + warningHandler(...args); + } + } + }, // Log to console regardless of nativeLoggingHook - static logToConsole(level, ...args) { + logToConsole(level: string, ...args: Array): void { const logFn = levelsMap[level]; invariant( logFn, - 'Level "' + level + '" not one of ' + Object.keys(levelsMap) + 'Level "' + level + '" not one of ' + Object.keys(levelsMap).toString() ); console[logFn](...args); + }, - return true; + setWarningHandler(handler: typeof warningHandler): void { + warningHandler = handler; } }