Cleanup Android logger code (#1367)

Summary:
X-link: https://github.com/facebook/react-native/pull/39373

Pull Request resolved: https://github.com/facebook/yoga/pull/1367

Moves some messiness around conditionally using Android's logger to `Log.cpp`, isolated to within a single function.

Reviewed By: rshest

Differential Revision: D49131964

fbshipit-source-id: cdff8af1d4df6ae28f00eecfed1920c71eec24d0
This commit is contained in:
Nick Gerleman
2023-09-11 19:51:40 -07:00
committed by Facebook GitHub Bot
parent c35f8819ae
commit a003c09a4c
3 changed files with 55 additions and 80 deletions

View File

@@ -20,75 +20,6 @@
using namespace facebook;
using namespace facebook::yoga;
#ifdef ANDROID
static int YGAndroidLog(
const YGConfigConstRef config,
const YGNodeConstRef node,
YGLogLevel level,
const char* format,
va_list args);
#else
static int YGDefaultLog(
const YGConfigConstRef config,
const YGNodeConstRef node,
YGLogLevel level,
const char* format,
va_list args);
#endif
#ifdef ANDROID
#include <android/log.h>
static int YGAndroidLog(
const YGConfigConstRef /*config*/,
const YGNodeConstRef /*node*/,
YGLogLevel level,
const char* format,
va_list args) {
int androidLevel = YGLogLevelDebug;
switch (level) {
case YGLogLevelFatal:
androidLevel = ANDROID_LOG_FATAL;
break;
case YGLogLevelError:
androidLevel = ANDROID_LOG_ERROR;
break;
case YGLogLevelWarn:
androidLevel = ANDROID_LOG_WARN;
break;
case YGLogLevelInfo:
androidLevel = ANDROID_LOG_INFO;
break;
case YGLogLevelDebug:
androidLevel = ANDROID_LOG_DEBUG;
break;
case YGLogLevelVerbose:
androidLevel = ANDROID_LOG_VERBOSE;
break;
}
const int result = __android_log_vprint(androidLevel, "yoga", format, args);
return result;
}
#else
static int YGDefaultLog(
const YGConfigConstRef /*config*/,
const YGNodeConstRef /*node*/,
YGLogLevel level,
const char* format,
va_list args) {
switch (level) {
case YGLogLevelError:
case YGLogLevelFatal:
return vfprintf(stderr, format, args);
case YGLogLevelWarn:
case YGLogLevelInfo:
case YGLogLevelDebug:
case YGLogLevelVerbose:
default:
return vprintf(format, args);
}
}
#endif
YOGA_EXPORT bool YGFloatIsUndefined(const float value) {
return yoga::isUndefined(value);
}
@@ -260,12 +191,7 @@ YOGA_EXPORT void YGNodeReset(YGNodeRef node) {
}
YOGA_EXPORT YGConfigRef YGConfigNew(void) {
#ifdef ANDROID
const YGConfigRef config = new yoga::Config(YGAndroidLog);
#else
const YGConfigRef config = new yoga::Config(YGDefaultLog);
#endif
return config;
return new yoga::Config(getDefaultLogger());
}
YOGA_EXPORT void YGConfigFree(const YGConfigRef config) {
@@ -960,11 +886,7 @@ YOGA_EXPORT void YGConfigSetLogger(const YGConfigRef config, YGLogger logger) {
if (logger != nullptr) {
resolveRef(config)->setLogger(logger);
} else {
#ifdef ANDROID
resolveRef(config)->setLogger(&YGAndroidLog);
#else
resolveRef(config)->setLogger(&YGDefaultLog);
#endif
resolveRef(config)->setLogger(getDefaultLogger());
}
}

View File

@@ -7,6 +7,10 @@
#include <yoga/debug/Log.h>
#ifdef ANDROID
#include <android/log.h>
#endif
namespace facebook::yoga {
namespace {
@@ -62,4 +66,49 @@ void log(
va_end(args);
}
YGLogger getDefaultLogger() {
return [](const YGConfigConstRef /*config*/,
const YGNodeConstRef /*node*/,
YGLogLevel level,
const char* format,
va_list args) -> int {
#ifdef ANDROID
int androidLevel = YGLogLevelDebug;
switch (level) {
case YGLogLevelFatal:
androidLevel = ANDROID_LOG_FATAL;
break;
case YGLogLevelError:
androidLevel = ANDROID_LOG_ERROR;
break;
case YGLogLevelWarn:
androidLevel = ANDROID_LOG_WARN;
break;
case YGLogLevelInfo:
androidLevel = ANDROID_LOG_INFO;
break;
case YGLogLevelDebug:
androidLevel = ANDROID_LOG_DEBUG;
break;
case YGLogLevelVerbose:
androidLevel = ANDROID_LOG_VERBOSE;
break;
}
return __android_log_vprint(androidLevel, "yoga", format, args);
#else
switch (level) {
case YGLogLevelError:
case YGLogLevelFatal:
return vfprintf(stderr, format, args);
case YGLogLevelWarn:
case YGLogLevelInfo:
case YGLogLevelDebug:
case YGLogLevelVerbose:
default:
return vprintf(format, args);
}
#endif
};
}
} // namespace facebook::yoga

View File

@@ -7,6 +7,8 @@
#pragma once
#include <yoga/Yoga.h>
#include <yoga/YGEnums.h>
#include <yoga/node/Node.h>
#include <yoga/config/Config.h>
@@ -29,4 +31,6 @@ void log(
const char* format,
...) noexcept;
YGLogger getDefaultLogger();
} // namespace facebook::yoga