diff --git a/java/jni/YGJNI.cpp b/java/jni/YGJNI.cpp index b08342c7..8d0773a2 100644 --- a/java/jni/YGJNI.cpp +++ b/java/jni/YGJNI.cpp @@ -56,6 +56,7 @@ static void YGTransferLayoutOutputsRecursive(YGNodeRef root) { Log::log( root, YGLogLevelError, + nullptr, "Java YGNode was GCed during layout calculation\n"); return; } @@ -159,6 +160,7 @@ static void YGPrint(YGNodeRef node) { Log::log( node, YGLogLevelError, + nullptr, "Java YGNode was GCed during layout calculation\n"); } } @@ -245,6 +247,7 @@ static YGSize YGJNIMeasureFunc( Log::log( node, YGLogLevelError, + nullptr, "Java YGNode was GCed during layout calculation\n"); return YGSize{ widthMode == YGMeasureModeUndefined ? 0 : width, diff --git a/yoga/YGConfig.cpp b/yoga/YGConfig.cpp index c6c3380b..ba334834 100644 --- a/yoga/YGConfig.cpp +++ b/yoga/YGConfig.cpp @@ -6,13 +6,21 @@ */ #include "YGConfig.h" -YGConfig::YGConfig(YGLogger logger) : logger_{logger} {} +YGConfig::YGConfig(YGLogger logger) { + logger_.noContext = logger; + loggerUsesContext_ = false; +} void YGConfig::log( YGConfig* config, YGNode* node, YGLogLevel logLevel, + void* logContext, const char* format, va_list args) { - logger_(config, node, logLevel, format, args); + if (loggerUsesContext_) { + logger_.withContext(config, node, logLevel, logContext, format, args); + } else { + logger_.noContext(config, node, logLevel, format, args); + } } diff --git a/yoga/YGConfig.h b/yoga/YGConfig.h index b4b2748b..d8260739 100644 --- a/yoga/YGConfig.h +++ b/yoga/YGConfig.h @@ -10,8 +10,20 @@ #include "Yoga.h" struct YGConfig { + using LogWithContextFn = void (*)( + YGConfigRef config, + YGNodeRef node, + YGLogLevel level, + void* context, + const char* format, + va_list args); + private: - YGLogger logger_; + union { + LogWithContextFn withContext; + YGLogger noContext; + } logger_; + bool loggerUsesContext_; public: bool useWebDefaults = false; @@ -26,8 +38,16 @@ public: YGMarkerCallbacks markerCallbacks = {nullptr, nullptr}; YGConfig(YGLogger logger); - void log(YGConfig*, YGNode*, YGLogLevel, const char*, va_list); + void log(YGConfig*, YGNode*, YGLogLevel, void*, const char*, va_list); void setLogger(YGLogger logger) { - logger_ = logger; + logger_.noContext = logger; + loggerUsesContext_ = false; + } + void setLogger(LogWithContextFn logger) { + logger_.withContext = logger; + loggerUsesContext_ = true; + } + void setLogger(std::nullptr_t) { + setLogger(YGLogger{nullptr}); } }; diff --git a/yoga/Yoga.cpp b/yoga/Yoga.cpp index 5dd7a816..f15864db 100644 --- a/yoga/Yoga.cpp +++ b/yoga/Yoga.cpp @@ -1030,7 +1030,7 @@ static void YGNodePrintInternal( const YGPrintOptions options) { std::string str; facebook::yoga::YGNodeToString(str, node, options, 0); - Log::log(node, YGLogLevelDebug, str.c_str()); + Log::log(node, YGLogLevelDebug, nullptr, str.c_str()); } void YGNodePrint(const YGNodeRef node, const YGPrintOptions options) { @@ -3819,6 +3819,7 @@ bool YGLayoutNodeInternal( Log::log( node, YGLogLevelVerbose, + nullptr, "%s%d.{[skipped] ", YGSpacer(gDepth), gDepth); @@ -3828,6 +3829,7 @@ bool YGLayoutNodeInternal( Log::log( node, YGLogLevelVerbose, + nullptr, "wm: %s, hm: %s, aw: %f ah: %f => d: (%f, %f) %s\n", YGMeasureModeName(widthMeasureMode, performLayout), YGMeasureModeName(heightMeasureMode, performLayout), @@ -3842,6 +3844,7 @@ bool YGLayoutNodeInternal( Log::log( node, YGLogLevelVerbose, + nullptr, "%s%d.{%s", YGSpacer(gDepth), gDepth, @@ -3852,6 +3855,7 @@ bool YGLayoutNodeInternal( Log::log( node, YGLogLevelVerbose, + nullptr, "wm: %s, hm: %s, aw: %f ah: %f %s\n", YGMeasureModeName(widthMeasureMode, performLayout), YGMeasureModeName(heightMeasureMode, performLayout), @@ -3877,6 +3881,7 @@ bool YGLayoutNodeInternal( Log::log( node, YGLogLevelVerbose, + nullptr, "%s%d.}%s", YGSpacer(gDepth), gDepth, @@ -3887,6 +3892,7 @@ bool YGLayoutNodeInternal( Log::log( node, YGLogLevelVerbose, + nullptr, "wm: %s, hm: %s, d: (%f, %f) %s\n", YGMeasureModeName(widthMeasureMode, performLayout), YGMeasureModeName(heightMeasureMode, performLayout), @@ -3905,7 +3911,7 @@ bool YGLayoutNodeInternal( } if (layout->nextCachedMeasurementsIndex == YG_MAX_CACHED_RESULT_COUNT) { if (gPrintChanges) { - Log::log(node, YGLogLevelVerbose, "Out of cache entries!\n"); + Log::log(node, YGLogLevelVerbose, nullptr, "Out of cache entries!\n"); } layout->nextCachedMeasurementsIndex = 0; } @@ -4200,7 +4206,7 @@ void YGConfigSetShouldDiffLayoutWithoutLegacyStretchBehaviour( void YGAssert(const bool condition, const char* message) { if (!condition) { - Log::log(YGNodeRef{nullptr}, YGLogLevelFatal, "%s\n", message); + Log::log(YGNodeRef{nullptr}, YGLogLevelFatal, nullptr, "%s\n", message); } } @@ -4209,7 +4215,7 @@ void YGAssertWithNode( const bool condition, const char* message) { if (!condition) { - Log::log(node, YGLogLevelFatal, "%s\n", message); + Log::log(node, YGLogLevelFatal, nullptr, "%s\n", message); } } @@ -4218,7 +4224,7 @@ void YGAssertWithConfig( const bool condition, const char* message) { if (!condition) { - Log::log(config, YGLogLevelFatal, "%s\n", message); + Log::log(config, YGLogLevelFatal, nullptr, "%s\n", message); } } diff --git a/yoga/log.cpp b/yoga/log.cpp index 390c581f..ebb99e45 100644 --- a/yoga/log.cpp +++ b/yoga/log.cpp @@ -21,10 +21,11 @@ void vlog( YGConfig* config, YGNode* node, YGLogLevel level, + void* context, const char* format, va_list args) { YGConfig* logConfig = config != nullptr ? config : YGConfigGetDefault(); - logConfig->log(logConfig, node, level, format, args); + logConfig->log(logConfig, node, level, context, format, args); if (level == YGLogLevelFatal) { abort(); @@ -35,23 +36,30 @@ void vlog( void Log::log( YGNode* node, YGLogLevel level, + void* context, const char* format, ...) noexcept { va_list args; va_start(args, format); vlog( - node == nullptr ? nullptr : node->getConfig(), node, level, format, args); + node == nullptr ? nullptr : node->getConfig(), + node, + level, + context, + format, + args); va_end(args); } void Log::log( YGConfig* config, YGLogLevel level, + void* context, const char* format, ...) noexcept { va_list args; va_start(args, format); - vlog(config, nullptr, level, format, args); + vlog(config, nullptr, level, context, format, args); va_end(args); } diff --git a/yoga/log.h b/yoga/log.h index b1fa9e2d..f25ee1a2 100644 --- a/yoga/log.h +++ b/yoga/log.h @@ -20,12 +20,14 @@ struct Log { static void log( YGNode* node, YGLogLevel level, + void*, const char* message, ...) noexcept; static void log( YGConfig* config, YGLogLevel level, + void*, const char* format, ...) noexcept; };