diff --git a/java/jni/YGJNI.cpp b/java/jni/YGJNI.cpp index 915876b5..b08342c7 100644 --- a/java/jni/YGJNI.cpp +++ b/java/jni/YGJNI.cpp @@ -7,10 +7,12 @@ #include #include #include +#include #include using namespace facebook::jni; using namespace std; +using facebook::yoga::detail::Log; struct JYogaNode : public JavaClass { static constexpr auto kJavaDescriptor = "Lcom/facebook/yoga/YogaNode;"; @@ -51,7 +53,7 @@ static void YGTransferLayoutOutputsRecursive(YGNodeRef root) { } auto obj = YGNodeJobject(root)->lockLocal(); if (!obj) { - YGLog( + Log::log( root, YGLogLevelError, "Java YGNode was GCed during layout calculation\n"); @@ -154,7 +156,7 @@ static void YGPrint(YGNodeRef node) { if (auto obj = YGNodeJobject(node)->lockLocal()) { cout << obj->toString() << endl; } else { - YGLog( + Log::log( node, YGLogLevelError, "Java YGNode was GCed during layout calculation\n"); @@ -240,7 +242,7 @@ static YGSize YGJNIMeasureFunc( return YGSize{*measuredWidth, *measuredHeight}; } else { - YGLog( + Log::log( node, YGLogLevelError, "Java YGNode was GCed during layout calculation\n"); diff --git a/yoga/Yoga.cpp b/yoga/Yoga.cpp index 3f65092b..3b3208af 100644 --- a/yoga/Yoga.cpp +++ b/yoga/Yoga.cpp @@ -5,6 +5,7 @@ * file in the root directory of this source tree. */ #include "Yoga.h" +#include "log.h" #include #include #include @@ -25,6 +26,7 @@ __forceinline const float fmaxf(const float a, const float b) { #endif using namespace facebook::yoga; +using detail::Log; #ifdef ANDROID static int YGAndroidLog( @@ -1028,7 +1030,7 @@ static void YGNodePrintInternal( const YGPrintOptions options) { std::string str; facebook::yoga::YGNodeToString(str, node, options, 0); - YGLog(node, YGLogLevelDebug, str.c_str()); + Log::log(node, YGLogLevelDebug, str.c_str()); } void YGNodePrint(const YGNodeRef node, const YGPrintOptions options) { @@ -3814,7 +3816,7 @@ bool YGLayoutNodeInternal( : layoutMarkerData.cachedMeasures) += 1; if (gPrintChanges && gPrintSkips) { - YGLog( + Log::log( node, YGLogLevelVerbose, "%s%d.{[skipped] ", @@ -3823,7 +3825,7 @@ bool YGLayoutNodeInternal( if (node->getPrintFunc() != nullptr) { node->getPrintFunc()(node); } - YGLog( + Log::log( node, YGLogLevelVerbose, "wm: %s, hm: %s, aw: %f ah: %f => d: (%f, %f) %s\n", @@ -3837,7 +3839,7 @@ bool YGLayoutNodeInternal( } } else { if (gPrintChanges) { - YGLog( + Log::log( node, YGLogLevelVerbose, "%s%d.{%s", @@ -3847,7 +3849,7 @@ bool YGLayoutNodeInternal( if (node->getPrintFunc() != nullptr) { node->getPrintFunc()(node); } - YGLog( + Log::log( node, YGLogLevelVerbose, "wm: %s, hm: %s, aw: %f ah: %f %s\n", @@ -3872,7 +3874,7 @@ bool YGLayoutNodeInternal( layoutMarkerData); if (gPrintChanges) { - YGLog( + Log::log( node, YGLogLevelVerbose, "%s%d.}%s", @@ -3882,7 +3884,7 @@ bool YGLayoutNodeInternal( if (node->getPrintFunc() != nullptr) { node->getPrintFunc()(node); } - YGLog( + Log::log( node, YGLogLevelVerbose, "wm: %s, hm: %s, d: (%f, %f) %s\n", @@ -3903,7 +3905,7 @@ bool YGLayoutNodeInternal( } if (layout->nextCachedMeasurementsIndex == YG_MAX_CACHED_RESULT_COUNT) { if (gPrintChanges) { - YGLog(node, YGLogLevelVerbose, "Out of cache entries!\n"); + Log::log(node, YGLogLevelVerbose, "Out of cache entries!\n"); } layout->nextCachedMeasurementsIndex = 0; } @@ -4196,43 +4198,9 @@ void YGConfigSetShouldDiffLayoutWithoutLegacyStretchBehaviour( config->shouldDiffLayoutWithoutLegacyStretchBehaviour = shouldDiffLayout; } -static void YGVLog( - const YGConfigRef config, - const YGNodeRef node, - YGLogLevel level, - const char* format, - va_list args) { - const YGConfigRef logConfig = - config != nullptr ? config : YGConfigGetDefault(); - logConfig->logger(logConfig, node, level, format, args); - - if (level == YGLogLevelFatal) { - abort(); - } -} - -void YGLogWithConfig( - const YGConfigRef config, - YGLogLevel level, - const char* format, - ...) { - va_list args; - va_start(args, format); - YGVLog(config, nullptr, level, format, args); - va_end(args); -} - -void YGLog(const YGNodeRef node, YGLogLevel level, const char* format, ...) { - va_list args; - va_start(args, format); - YGVLog( - node == nullptr ? nullptr : node->getConfig(), node, level, format, args); - va_end(args); -} - void YGAssert(const bool condition, const char* message) { if (!condition) { - YGLog(nullptr, YGLogLevelFatal, "%s\n", message); + Log::log(YGNodeRef{nullptr}, YGLogLevelFatal, "%s\n", message); } } @@ -4241,7 +4209,7 @@ void YGAssertWithNode( const bool condition, const char* message) { if (!condition) { - YGLog(node, YGLogLevelFatal, "%s\n", message); + Log::log(node, YGLogLevelFatal, "%s\n", message); } } @@ -4250,7 +4218,7 @@ void YGAssertWithConfig( const bool condition, const char* message) { if (!condition) { - YGLogWithConfig(config, YGLogLevelFatal, "%s\n", message); + Log::log(config, YGLogLevelFatal, "%s\n", message); } } diff --git a/yoga/Yoga.h b/yoga/Yoga.h index 214c5f69..e3ca1fea 100644 --- a/yoga/Yoga.h +++ b/yoga/Yoga.h @@ -348,16 +348,6 @@ WIN_EXPORT float YGNodeLayoutGetPadding( const YGEdge edge); WIN_EXPORT void YGConfigSetLogger(const YGConfigRef config, YGLogger logger); -WIN_EXPORT void YGLog( - const YGNodeRef node, - YGLogLevel level, - const char* message, - ...); -WIN_EXPORT void YGLogWithConfig( - const YGConfigRef config, - YGLogLevel level, - const char* format, - ...); WIN_EXPORT void YGAssert(const bool condition, const char* message); WIN_EXPORT void YGAssertWithNode( const YGNodeRef node, diff --git a/yoga/log.cpp b/yoga/log.cpp new file mode 100644 index 00000000..256bd811 --- /dev/null +++ b/yoga/log.cpp @@ -0,0 +1,60 @@ +/** + * 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 "log.h" + +#include "Yoga.h" +#include "YGConfig.h" +#include "YGNode.h" + +namespace facebook { +namespace yoga { +namespace detail { + +namespace { + +void vlog( + YGConfig* config, + YGNode* node, + YGLogLevel level, + const char* format, + va_list args) { + YGConfig* logConfig = config != nullptr ? config : YGConfigGetDefault(); + logConfig->logger(logConfig, node, level, format, args); + + if (level == YGLogLevelFatal) { + abort(); + } +} +} // namespace + +void Log::log( + YGNode* node, + YGLogLevel 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::log( + YGConfig* config, + YGLogLevel level, + const char* format, + ...) noexcept { + va_list args; + va_start(args, format); + vlog(config, nullptr, level, format, args); + va_end(args); +} + +} // namespace detail +} // namespace yoga +} // namespace facebook diff --git a/yoga/log.h b/yoga/log.h new file mode 100644 index 00000000..b1fa9e2d --- /dev/null +++ b/yoga/log.h @@ -0,0 +1,35 @@ +/** + * 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 + +#include "YGEnums.h" + +struct YGNode; +struct YGConfig; + +namespace facebook { +namespace yoga { + +namespace detail { + +struct Log { + static void log( + YGNode* node, + YGLogLevel level, + const char* message, + ...) noexcept; + + static void log( + YGConfig* config, + YGLogLevel level, + const char* format, + ...) noexcept; +}; + +} // namespace detail +} // namespace yoga +} // namespace facebook