From b7fd68e611875365c7d66b6077c52a8782bff736 Mon Sep 17 00:00:00 2001 From: Ramanpreet Nara Date: Fri, 13 Aug 2021 13:52:11 -0700 Subject: [PATCH] Introduce JReactSoftExceptionLogger to log SoftExceptions from C++ Summary: When the TurboModule system is enabled, C++ NativeModules shouldn't be used in production. We'll use this JReactSoftExceptionLogger to log soft exceptions from C++ NativeModules this scenario. Changelog: [Internal] Reviewed By: JoshuaGross Differential Revision: D30272694 fbshipit-source-id: 8dadcfe51bcbc353d438d1a403e74da5e2cb9546 --- .../bridge/ReactSoftExceptionLogger.java | 7 +++++ .../react/jni/JReactSoftExceptionLogger.cpp | 21 ++++++++++++++ .../jni/react/jni/JReactSoftExceptionLogger.h | 28 +++++++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 ReactAndroid/src/main/jni/react/jni/JReactSoftExceptionLogger.cpp create mode 100644 ReactAndroid/src/main/jni/react/jni/JReactSoftExceptionLogger.h diff --git a/ReactAndroid/src/main/java/com/facebook/react/bridge/ReactSoftExceptionLogger.java b/ReactAndroid/src/main/java/com/facebook/react/bridge/ReactSoftExceptionLogger.java index bf4f7348588..a33590a83d0 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/bridge/ReactSoftExceptionLogger.java +++ b/ReactAndroid/src/main/java/com/facebook/react/bridge/ReactSoftExceptionLogger.java @@ -50,4 +50,11 @@ public class ReactSoftExceptionLogger { FLog.e(category, "Unhandled SoftException", cause); } } + + @DoNotStrip + // For use from within the C++ JReactSoftExceptionLogger + private static void logNoThrowSoftExceptionWithMessage( + final String category, final String message) { + logSoftException(category, new ReactNoCrashSoftException(message)); + } } diff --git a/ReactAndroid/src/main/jni/react/jni/JReactSoftExceptionLogger.cpp b/ReactAndroid/src/main/jni/react/jni/JReactSoftExceptionLogger.cpp new file mode 100644 index 00000000000..788de6f99ab --- /dev/null +++ b/ReactAndroid/src/main/jni/react/jni/JReactSoftExceptionLogger.cpp @@ -0,0 +1,21 @@ +/* + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#include "JReactSoftExceptionLogger.h" + +using namespace facebook::react; + +void JReactSoftExceptionLogger::logNoThrowSoftExceptionWithMessage( + std::string tag, + std::string message) { + static const auto logNoThrowSoftExceptionWithMessage = + javaClassStatic() + ->getStaticMethod( + "logNoThrowSoftExceptionWithMessage"); + + return logNoThrowSoftExceptionWithMessage(javaClassStatic(), tag, message); +} diff --git a/ReactAndroid/src/main/jni/react/jni/JReactSoftExceptionLogger.h b/ReactAndroid/src/main/jni/react/jni/JReactSoftExceptionLogger.h new file mode 100644 index 00000000000..6a91ec709af --- /dev/null +++ b/ReactAndroid/src/main/jni/react/jni/JReactSoftExceptionLogger.h @@ -0,0 +1,28 @@ +/* + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#pragma once + +#include +#include + +namespace facebook { +namespace react { + +class JReactSoftExceptionLogger + : public jni::JavaClass { + public: + static constexpr const char *kJavaDescriptor = + "Lcom/facebook/react/bridge/ReactSoftExceptionLogger;"; + + static void logNoThrowSoftExceptionWithMessage( + std::string tag, + std::string message); +}; + +} // namespace react +} // namespace facebook