Implement RCTWarn equivalent on Android

Summary:
## Overview
This diff is an RFC to port a logging feature from iOS to Android.

Changelog: [Internal]

## Motivation
On iOS we have the following log functions and behaviors available for logging native warnings and errors:

- **Warnings** (`RCTLogWarn`)
  - Log level 'warn' to console
  - Display warning in LogBox
- **Errors** (`RCTLogError`)
  - Log level 'error' to console
  - Display a native RedBox (needs converted to show a LogBox if available)
- **Logs**
  - We also have `RCTLog`, `RCTTrace`, `RCTAdvice`, `RCTInfo`, which just log to the console.

In Java, we have:
- **Warnings**
  - **None**, added in this diff
- **Errors** (`DevSupportManager.showNewJavaError`)
  - Log level 'error' to console with `FLog.e`
  - Display a native RedBox (needs converted to show a LogBox if available
- **Logs**
  - `ReactSoftException` (crashes the app??)
  - `ReactNoCrashSoftException` (only logs??)
  - Others?

## Details

This diff adds a method to pair with `RCTLogWarn`, `DevSupportManager.showNewJavaWarning`, which will log to the console and show a LogBox warning if LogBox is available.

## Concerns

I have a few concerns/questions about the state of logging on Android:
- Should/can we move all of the logging to it's own class, like how RCTLog works?
- Why does some logging happen on DevSupportManager and some in other classes?
- If we moved it all to it's own class, how could we access the reactContext to call the RCTLog JS module

Reviewed By: JoshuaGross

Differential Revision: D20056394

fbshipit-source-id: 32d57e300685e46da8039fc77cb22b4084acf81a
This commit is contained in:
Rick Hanlon
2020-04-01 16:45:54 -07:00
committed by Facebook GitHub Bot
parent ec0c65c4b2
commit 52b3105f65
5 changed files with 157 additions and 7 deletions
@@ -26,6 +26,7 @@ rn_android_library(
react_native_target("java/com/facebook/debug/tags:tags"),
react_native_target("java/com/facebook/react/bridge:bridge"),
react_native_target("java/com/facebook/react/common:common"),
react_native_target("java/com/facebook/react/util:util"),
react_native_target("java/com/facebook/react/common/network:network"),
react_native_target("java/com/facebook/react/devsupport:interfaces"),
react_native_target("java/com/facebook/react/module/annotations:annotations"),
@@ -11,13 +11,12 @@ import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.Nullable;
import com.facebook.common.logging.FLog;
import com.facebook.fbreact.specs.NativeLogBoxSpec;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.UiThreadUtil;
import com.facebook.react.common.ReactConstants;
import com.facebook.react.devsupport.interfaces.DevSupportManager;
import com.facebook.react.module.annotations.ReactModule;
import com.facebook.react.util.RNLog;
@ReactModule(name = LogBoxModule.NAME)
public class LogBoxModule extends NativeLogBoxSpec {
@@ -40,9 +39,7 @@ public class LogBoxModule extends NativeLogBoxSpec {
if (mReactRootView == null && mDevSupportManager != null) {
mReactRootView = mDevSupportManager.createRootView("LogBox");
if (mReactRootView == null) {
FLog.e(
ReactConstants.TAG,
"Unable to launch logbox because react was unable to create the root view");
RNLog.e("Unable to launch logbox because react was unable to create the root view");
}
}
}
@@ -64,8 +61,7 @@ public class LogBoxModule extends NativeLogBoxSpec {
if (mLogBoxDialog == null && mReactRootView != null) {
Activity context = getCurrentActivity();
if (context == null || context.isFinishing()) {
FLog.e(
ReactConstants.TAG,
RNLog.e(
"Unable to launch logbox because react activity "
+ "is not available, here is the error that logbox would've displayed: ");
return;