From 307f54832da04828fcddd4fbe1c2d7f2746bdb02 Mon Sep 17 00:00:00 2001 From: Sota Ogo Date: Tue, 10 Aug 2021 21:29:52 -0700 Subject: [PATCH] General Logging Util (stab) class for native errors (#31998) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/31998 Overall Context: We want to add a way to log errors (e.g. mustfix, warn, etc on the server with stack trace) without crashing the app (e.g. react_native_assert crashes the app). This diff: I am writing very simple logger functions which will get resolved at build time depending on the platforms/apps. Changelog: [internal] Reviewed By: JoshuaGross Differential Revision: D30174404 fbshipit-source-id: 2e5bc865dd8576c5a758c56e080a1e582a8c3ada --- .../react/renderer/components/view/Android.mk | 3 +- .../view/YogaLayoutableShadowNode.cpp | 4 ++- ReactCommon/react/utils/Android.mk | 4 ++- ReactCommon/react/utils/BUCK | 1 + ReactCommon/react/utils/ReactNativeLogger.cpp | 29 +++++++++++++++++++ ReactCommon/react/utils/ReactNativeLogger.h | 23 +++++++++++++++ 6 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 ReactCommon/react/utils/ReactNativeLogger.cpp create mode 100644 ReactCommon/react/utils/ReactNativeLogger.h diff --git a/ReactCommon/react/renderer/components/view/Android.mk b/ReactCommon/react/renderer/components/view/Android.mk index ae4a942c805..0bcac34a578 100644 --- a/ReactCommon/react/renderer/components/view/Android.mk +++ b/ReactCommon/react/renderer/components/view/Android.mk @@ -21,7 +21,7 @@ LOCAL_CFLAGS += -fexceptions -frtti -std=c++17 -Wall LOCAL_STATIC_LIBRARIES := -LOCAL_SHARED_LIBRARIES := libyoga glog libfolly_json libglog_init libreact_render_core libreact_render_debug libreact_render_graphics libreact_debug +LOCAL_SHARED_LIBRARIES := libyoga glog libfolly_json libglog_init libreact_render_core libreact_render_debug libreact_render_graphics libreact_debug libreact_utils include $(BUILD_SHARED_LIBRARY) @@ -31,5 +31,6 @@ $(call import-module,fbgloginit) $(call import-module,react/renderer/core) $(call import-module,react/renderer/debug) $(call import-module,react/renderer/graphics) +$(call import-module,react/utils) $(call import-module,yogajni) $(call import-module,react/debug) diff --git a/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.cpp b/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.cpp index a56e1d7ccd7..0872623964d 100644 --- a/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.cpp +++ b/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.cpp @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -226,7 +227,8 @@ void YogaLayoutableShadowNode::appendChild( ensureConsistency(); } else { - LOG(ERROR) << "Text strings must be rendered within a component."; + ReactNativeLogger::error( + "Text strings must be rendered within a component."); } } diff --git a/ReactCommon/react/utils/Android.mk b/ReactCommon/react/utils/Android.mk index 1f0334cde04..471b15e6cff 100644 --- a/ReactCommon/react/utils/Android.mk +++ b/ReactCommon/react/utils/Android.mk @@ -20,9 +20,11 @@ LOCAL_CFLAGS := \ LOCAL_CFLAGS += -fexceptions -frtti -std=c++17 -Wall LOCAL_STATIC_LIBRARIES := -LOCAL_SHARED_LIBRARIES := libreact_debug libreact_render_mapbuffer +LOCAL_SHARED_LIBRARIES := libreact_debug libreact_render_mapbuffer libglog libglog_init include $(BUILD_SHARED_LIBRARY) $(call import-module,react/debug) +$(call import-module,fbgloginit) +$(call import-module,glog) $(call import-module,react/renderer/mapbuffer) diff --git a/ReactCommon/react/utils/BUCK b/ReactCommon/react/utils/BUCK index 5faea9b9e86..3d4bfde9b0f 100644 --- a/ReactCommon/react/utils/BUCK +++ b/ReactCommon/react/utils/BUCK @@ -58,6 +58,7 @@ rn_xplat_cxx_library( tests = [], visibility = ["PUBLIC"], deps = [ + "//third-party/glog:glog", "//xplat/folly:container_evicting_cache_map", "//xplat/folly:headers_only", "//xplat/folly:memory", diff --git a/ReactCommon/react/utils/ReactNativeLogger.cpp b/ReactCommon/react/utils/ReactNativeLogger.cpp new file mode 100644 index 00000000000..78cbf6c24a3 --- /dev/null +++ b/ReactCommon/react/utils/ReactNativeLogger.cpp @@ -0,0 +1,29 @@ +/* + * 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 "ReactNativeLogger.h" +#include + +namespace facebook { +namespace react { +namespace ReactNativeLogger { + +void info(std::string const &text) { + LOG(INFO) << text; +} + +void warning(std::string const &text) { + LOG(WARNING) << text; +} + +void error(std::string const &text) { + LOG(ERROR) << text; +} + +} // namespace ReactNativeLogger +} // namespace react +} // namespace facebook diff --git a/ReactCommon/react/utils/ReactNativeLogger.h b/ReactCommon/react/utils/ReactNativeLogger.h new file mode 100644 index 00000000000..d448970c8b1 --- /dev/null +++ b/ReactCommon/react/utils/ReactNativeLogger.h @@ -0,0 +1,23 @@ +/* + * 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 { +namespace ReactNativeLogger { + +void info(std::string const &text); +void warning(std::string const &text); +void error(std::string const &text); + +} // namespace ReactNativeLogger +} // namespace react +} // namespace facebook