Skip to content

Commit

Permalink
Add a way to bind log function to the unified react native logger.
Browse files Browse the repository at this point in the history
Summary:
In this diff:
1. Convert the ReactNativeLogger to c function for the future compatibility.
2. Bind the log function from Catalyst app
3. Update the call site

Changelog: [internal]

Reviewed By: JoshuaGross

Differential Revision: D30271863

fbshipit-source-id: 4c0ea704cf19f53468a3b72631353959ea999884
  • Loading branch information
sota000 authored and facebook-github-bot committed Aug 16, 2021
1 parent 3e5998e commit c317a70
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 54 deletions.
1 change: 1 addition & 0 deletions ReactCommon/react/renderer/components/view/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ rn_xplat_cxx_library(
react_native_xplat_target("react/renderer/core:core"),
react_native_xplat_target("react/renderer/debug:debug"),
react_native_xplat_target("react/renderer/graphics:graphics"),
react_native_xplat_target("react/utils:utils"),
],
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include <react/renderer/core/LayoutContext.h>
#include <react/renderer/debug/DebugStringConvertibleItem.h>
#include <react/renderer/debug/SystraceSection.h>
#include <react/utils/ReactNativeLogger.h>
#include <react/utils/react_native_log.h>
#include <yoga/Yoga.h>
#include <algorithm>
#include <limits>
Expand Down Expand Up @@ -227,7 +227,7 @@ void YogaLayoutableShadowNode::appendChild(

ensureConsistency();
} else {
ReactNativeLogger::error(
react_native_log_error(
"Text strings must be rendered within a <Text> component.");
}
}
Expand Down
29 changes: 0 additions & 29 deletions ReactCommon/react/utils/ReactNativeLogger.cpp

This file was deleted.

23 changes: 0 additions & 23 deletions ReactCommon/react/utils/ReactNativeLogger.h

This file was deleted.

52 changes: 52 additions & 0 deletions ReactCommon/react/utils/react_native_log.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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 "react_native_log.h"
#include <glog/logging.h>

static reactnativelogfunctype _reactnativelogfunc = NULL;

void set_react_native_logfunc(reactnativelogfunctype newlogfunc) {
_reactnativelogfunc = newlogfunc;
}
void react_native_log_info(const char *message) {
_react_native_log(ReactNativeLogLevelInfo, message);
}
void react_native_log_warn(const char *message) {
_react_native_log(ReactNativeLogLevelWarning, message);
}
void react_native_log_error(const char *message) {
_react_native_log(ReactNativeLogLevelError, message);
}
void react_native_log_fatal(const char *message) {
_react_native_log(ReactNativeLogLevelFatal, message);
}

void _react_native_log(ReactNativeLogLevel level, const char *message) {
if (_reactnativelogfunc == NULL) {
_react_native_log_default(level, message);
} else {
_reactnativelogfunc(level, message);
}
}

void _react_native_log_default(ReactNativeLogLevel level, const char *message) {
switch (level) {
case ReactNativeLogLevelInfo:
LOG(INFO) << message;
break;
case ReactNativeLogLevelWarning:
LOG(WARNING) << message;
break;
case ReactNativeLogLevelError:
LOG(ERROR) << message;
break;
case ReactNativeLogLevelFatal:
LOG(FATAL) << message;
break;
}
}
33 changes: 33 additions & 0 deletions ReactCommon/react/utils/react_native_log.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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

enum ReactNativeLogLevel {
ReactNativeLogLevelInfo = 1,
ReactNativeLogLevelWarning = 2,
ReactNativeLogLevelError = 3,
ReactNativeLogLevelFatal = 4
};

typedef void (*reactnativelogfunctype)(ReactNativeLogLevel, const char *);

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
void set_react_native_logfunc(reactnativelogfunctype newlogfunc);

void react_native_log_info(const char *text);
void react_native_log_warn(const char *text);
void react_native_log_error(const char *text);
void react_native_log_fatal(const char *text);

void _react_native_log(ReactNativeLogLevel level, const char *text);
void _react_native_log_default(ReactNativeLogLevel level, const char *text);
#ifdef __cplusplus
}
#endif // __cpusplus

0 comments on commit c317a70

Please sign in to comment.