From f1a0eb98ba65f3dedd9d48436f0d3226b4a81178 Mon Sep 17 00:00:00 2001 From: Nick Gerleman Date: Mon, 11 Sep 2023 17:33:59 -0700 Subject: [PATCH] Cleanup logger code Summary: Moves some messiness around conditionally using Android's logger to `Log.cpp`, isolated to within a single function. Differential Revision: D49131964 fbshipit-source-id: c6d4c260172b314276b8e239f6d3655f7bb32b3b --- yoga/Yoga.cpp | 82 ++-------------------------------------------- yoga/debug/Log.cpp | 49 +++++++++++++++++++++++++++ yoga/debug/Log.h | 4 +++ 3 files changed, 55 insertions(+), 80 deletions(-) diff --git a/yoga/Yoga.cpp b/yoga/Yoga.cpp index 0a50aa84a4..03b4bdf943 100644 --- a/yoga/Yoga.cpp +++ b/yoga/Yoga.cpp @@ -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 -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()); } } diff --git a/yoga/debug/Log.cpp b/yoga/debug/Log.cpp index 21d8c6075b..507214c793 100644 --- a/yoga/debug/Log.cpp +++ b/yoga/debug/Log.cpp @@ -7,6 +7,10 @@ #include +#ifdef ANDROID +#include +#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 diff --git a/yoga/debug/Log.h b/yoga/debug/Log.h index 47ed5d4118..54f10c4b04 100644 --- a/yoga/debug/Log.h +++ b/yoga/debug/Log.h @@ -7,6 +7,8 @@ #pragma once +#include + #include #include #include @@ -29,4 +31,6 @@ void log( const char* format, ...) noexcept; +YGLogger getDefaultLogger(); + } // namespace facebook::yoga