Skip to content

Commit e697ed7

Browse files
javachefacebook-github-bot
authored andcommitted
Report native warnings to YellowBox
Reviewed By: fkgozali Differential Revision: D5553601 fbshipit-source-id: b80f019b11d02865a17a25cbff61edf65173cd84
1 parent 43ff9b4 commit e697ed7

File tree

2 files changed

+27
-17
lines changed

2 files changed

+27
-17
lines changed

Libraries/ReactNative/YellowBox.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ const EventEmitter = require('EventEmitter');
1616
const Platform = require('Platform');
1717
const React = require('React');
1818
const StyleSheet = require('StyleSheet');
19+
const RCTLog = require('RCTLog');
1920

2021
const infoLog = require('infoLog');
2122
const openFileInEditor = require('openFileInEditor');
2223
const parseErrorStack = require('parseErrorStack');
24+
const stringifySafe = require('stringifySafe');
2325
const symbolicateStackTrace = require('symbolicateStackTrace');
2426

2527
import type EmitterSubscription from 'EmitterSubscription';
@@ -74,18 +76,16 @@ if (__DEV__) {
7476

7577
(console: any).warn = function() {
7678
warn.apply(console, arguments);
77-
78-
if (typeof arguments[0] === 'string' &&
79-
arguments[0].startsWith('(ADVICE)')) {
80-
return;
81-
}
82-
8379
updateWarningMap.apply(null, arguments);
8480
};
8581

8682
if (Platform.isTesting) {
8783
(console: any).disableYellowBox = true;
8884
}
85+
86+
RCTLog.setWarningHandler((...args) => {
87+
updateWarningMap.apply(null, args);
88+
});
8989
}
9090

9191
/**
@@ -106,7 +106,6 @@ function updateWarningMap(format, ...args): void {
106106
if (console.disableYellowBox) {
107107
return;
108108
}
109-
const stringifySafe = require('stringifySafe');
110109

111110
format = String(format);
112111
const argCount = (format.match(/%s/g) || []).length;
@@ -115,6 +114,10 @@ function updateWarningMap(format, ...args): void {
115114
...args.slice(argCount).map(stringifySafe),
116115
].join(' ');
117116

117+
if (warning.startsWith('(ADVICE)')) {
118+
return;
119+
}
120+
118121
const warningInfo = _warningMap.get(warning);
119122
if (warningInfo) {
120123
warningInfo.count += 1;

Libraries/Utilities/RCTLog.js

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,35 @@ const levelsMap = {
2121
fatal: 'error',
2222
};
2323

24-
class RCTLog {
24+
let warningHandler: ?(Array<any> => void) = null;
25+
26+
const RCTLog = {
2527
// level one of log, info, warn, error, mustfix
26-
static logIfNoNativeHook(...args) {
28+
logIfNoNativeHook(level: string, ...args: Array<any>): void {
29+
// We already printed in the native console, so only log here if using a js debugger
2730
if (typeof global.nativeLoggingHook === 'undefined') {
28-
// We already printed in xcode, so only log here if using a js debugger
29-
RCTLog.logToConsole(...args);
31+
RCTLog.logToConsole(level, ...args);
32+
} else {
33+
// Report native warnings to YellowBox
34+
if (warningHandler && level === 'warn') {
35+
warningHandler(...args);
36+
}
3037
}
31-
32-
return true;
33-
}
38+
},
3439

3540
// Log to console regardless of nativeLoggingHook
36-
static logToConsole(level, ...args) {
41+
logToConsole(level: string, ...args: Array<any>): void {
3742
const logFn = levelsMap[level];
3843
invariant(
3944
logFn,
40-
'Level "' + level + '" not one of ' + Object.keys(levelsMap)
45+
'Level "' + level + '" not one of ' + Object.keys(levelsMap).toString()
4146
);
4247

4348
console[logFn](...args);
49+
},
4450

45-
return true;
51+
setWarningHandler(handler: typeof warningHandler): void {
52+
warningHandler = handler;
4653
}
4754
}
4855

0 commit comments

Comments
 (0)