Permalink
Browse files

Report native warnings to YellowBox

Reviewed By: fkgozali

Differential Revision: D5553601

fbshipit-source-id: b80f019b11d02865a17a25cbff61edf65173cd84
  • Loading branch information...
javache authored and facebook-github-bot committed Aug 4, 2017
1 parent 43ff9b4 commit e697ed75d14289c6d8e2ada448f7f24adbd1d29a
Showing with 27 additions and 17 deletions.
  1. +10 −7 Libraries/ReactNative/YellowBox.js
  2. +17 −10 Libraries/Utilities/RCTLog.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;
@@ -21,28 +21,35 @@ const levelsMap = {
fatal: 'error',
};
class RCTLog {
let warningHandler: ?(Array<any> => void) = null;
const RCTLog = {
// level one of log, info, warn, error, mustfix
static logIfNoNativeHook(...args) {
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') {
// We already printed in xcode, so only log here if using a js debugger
RCTLog.logToConsole(...args);
RCTLog.logToConsole(level, ...args);
} else {
// Report native warnings to YellowBox
if (warningHandler && level === 'warn') {
warningHandler(...args);
}
}
return true;
}
},
// Log to console regardless of nativeLoggingHook
static logToConsole(level, ...args) {
logToConsole(level: string, ...args: Array<any>): 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;
}
}

0 comments on commit e697ed7

Please sign in to comment.