Skip to content

Commit

Permalink
Report native warnings to YellowBox
Browse files Browse the repository at this point in the history
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 e697ed7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
17 changes: 10 additions & 7 deletions Libraries/ReactNative/YellowBox.js
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
});
}

/**
Expand All @@ -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;
Expand All @@ -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;
Expand Down
27 changes: 17 additions & 10 deletions Libraries/Utilities/RCTLog.js
Expand Up @@ -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;
}
}

Expand Down

0 comments on commit e697ed7

Please sign in to comment.