Report native warnings to YellowBox

Reviewed By: fkgozali

Differential Revision: D5553601

fbshipit-source-id: b80f019b11d02865a17a25cbff61edf65173cd84
This commit is contained in:
Pieter De Baets 2017-08-04 07:33:20 -07:00 committed by Facebook Github Bot
parent 43ff9b4252
commit e697ed75d1
2 changed files with 29 additions and 19 deletions

View File

@ -16,10 +16,12 @@ const EventEmitter = require('EventEmitter');
const Platform = require('Platform'); const Platform = require('Platform');
const React = require('React'); const React = require('React');
const StyleSheet = require('StyleSheet'); const StyleSheet = require('StyleSheet');
const RCTLog = require('RCTLog');
const infoLog = require('infoLog'); const infoLog = require('infoLog');
const openFileInEditor = require('openFileInEditor'); const openFileInEditor = require('openFileInEditor');
const parseErrorStack = require('parseErrorStack'); const parseErrorStack = require('parseErrorStack');
const stringifySafe = require('stringifySafe');
const symbolicateStackTrace = require('symbolicateStackTrace'); const symbolicateStackTrace = require('symbolicateStackTrace');
import type EmitterSubscription from 'EmitterSubscription'; import type EmitterSubscription from 'EmitterSubscription';
@ -74,18 +76,16 @@ if (__DEV__) {
(console: any).warn = function() { (console: any).warn = function() {
warn.apply(console, arguments); warn.apply(console, arguments);
if (typeof arguments[0] === 'string' &&
arguments[0].startsWith('(ADVICE)')) {
return;
}
updateWarningMap.apply(null, arguments); updateWarningMap.apply(null, arguments);
}; };
if (Platform.isTesting) { if (Platform.isTesting) {
(console: any).disableYellowBox = true; (console: any).disableYellowBox = true;
} }
RCTLog.setWarningHandler((...args) => {
updateWarningMap.apply(null, args);
});
} }
/** /**
@ -106,7 +106,6 @@ function updateWarningMap(format, ...args): void {
if (console.disableYellowBox) { if (console.disableYellowBox) {
return; return;
} }
const stringifySafe = require('stringifySafe');
format = String(format); format = String(format);
const argCount = (format.match(/%s/g) || []).length; const argCount = (format.match(/%s/g) || []).length;
@ -115,6 +114,10 @@ function updateWarningMap(format, ...args): void {
...args.slice(argCount).map(stringifySafe), ...args.slice(argCount).map(stringifySafe),
].join(' '); ].join(' ');
if (warning.startsWith('(ADVICE)')) {
return;
}
const warningInfo = _warningMap.get(warning); const warningInfo = _warningMap.get(warning);
if (warningInfo) { if (warningInfo) {
warningInfo.count += 1; warningInfo.count += 1;

View File

@ -21,28 +21,35 @@ const levelsMap = {
fatal: 'error', fatal: 'error',
}; };
class RCTLog { let warningHandler: ?(Array<any> => void) = null;
// 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);
}
return true; const RCTLog = {
} // level one of log, info, warn, error, mustfix
logIfNoNativeHook(level: string, ...args: Array<any>): 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 // Log to console regardless of nativeLoggingHook
static logToConsole(level, ...args) { logToConsole(level: string, ...args: Array<any>): void {
const logFn = levelsMap[level]; const logFn = levelsMap[level];
invariant( invariant(
logFn, logFn,
'Level "' + level + '" not one of ' + Object.keys(levelsMap) 'Level "' + level + '" not one of ' + Object.keys(levelsMap).toString()
); );
console[logFn](...args); console[logFn](...args);
},
return true; setWarningHandler(handler: typeof warningHandler): void {
warningHandler = handler;
} }
} }