Skip to content

Commit

Permalink
Cleanup logger code
Browse files Browse the repository at this point in the history
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
  • Loading branch information
NickGerleman authored and facebook-github-bot committed Sep 12, 2023
1 parent 4952a5c commit f1a0eb9
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 80 deletions.
82 changes: 2 additions & 80 deletions yoga/Yoga.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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());
}
}

Expand Down
49 changes: 49 additions & 0 deletions yoga/debug/Log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

#include <yoga/debug/Log.h>

#ifdef ANDROID
#include <android/log.h>
#endif

namespace facebook::yoga {

namespace {
Expand Down Expand Up @@ -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
4 changes: 4 additions & 0 deletions yoga/debug/Log.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

#pragma once

#include <yoga/Yoga.h>

#include <yoga/YGEnums.h>
#include <yoga/node/Node.h>
#include <yoga/config/Config.h>
Expand All @@ -29,4 +31,6 @@ void log(
const char* format,
...) noexcept;

YGLogger getDefaultLogger();

} // namespace facebook::yoga

0 comments on commit f1a0eb9

Please sign in to comment.