Files
yoga/yoga/debug/Log.cpp
Nick Gerleman 70954bbda5 C++ style enums 5/N: LogLevel (#1387)
Summary:
Pull Request resolved: https://github.com/facebook/yoga/pull/1387

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

This converts usages of YGLogLevel to LogLevel

Reviewed By: rozele

Differential Revision: D49270695

fbshipit-source-id: 2ba5b4f2b0af93fef89dbbb2ce54c2f486670aac
2023-09-14 23:06:34 -07:00

108 lines
2.4 KiB
C++

/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <yoga/debug/Log.h>
#ifdef ANDROID
#include <android/log.h>
#endif
namespace facebook::yoga {
namespace {
void vlog(
const yoga::Config* config,
const yoga::Node* node,
LogLevel level,
const char* format,
va_list args) {
if (config == nullptr) {
getDefaultLogger()(nullptr, node, unscopedEnum(level), format, args);
} else {
config->log(node, level, format, args);
}
}
} // namespace
void log(LogLevel level, const char* format, ...) noexcept {
va_list args;
va_start(args, format);
vlog(nullptr, nullptr, level, format, args);
va_end(args);
}
void log(
const yoga::Node* node,
LogLevel level,
const char* format,
...) noexcept {
va_list args;
va_start(args, format);
vlog(
node == nullptr ? nullptr : node->getConfig(), node, level, format, args);
va_end(args);
}
void log(
const yoga::Config* config,
LogLevel level,
const char* format,
...) noexcept {
va_list args;
va_start(args, format);
vlog(config, nullptr, level, format, args);
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