Add support for context-aware logging functions
Summary: @public Context-aware logging functions are an internal Yoga feature that will be used for Yoga’s JNI code. It will be possible to specify a context when calculating layout, which will be passed on to baseline and measure functions. This will be a private feature. Reviewed By: SidharthGuglani Differential Revision: D14123482 fbshipit-source-id: 8ba3b6c493bf79fe09831f22d2b6da44f09e3d95
This commit is contained in:
committed by
Facebook Github Bot
parent
0bdf36f5d1
commit
d5ad51bccc
@@ -56,6 +56,7 @@ static void YGTransferLayoutOutputsRecursive(YGNodeRef root) {
|
|||||||
Log::log(
|
Log::log(
|
||||||
root,
|
root,
|
||||||
YGLogLevelError,
|
YGLogLevelError,
|
||||||
|
nullptr,
|
||||||
"Java YGNode was GCed during layout calculation\n");
|
"Java YGNode was GCed during layout calculation\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -159,6 +160,7 @@ static void YGPrint(YGNodeRef node) {
|
|||||||
Log::log(
|
Log::log(
|
||||||
node,
|
node,
|
||||||
YGLogLevelError,
|
YGLogLevelError,
|
||||||
|
nullptr,
|
||||||
"Java YGNode was GCed during layout calculation\n");
|
"Java YGNode was GCed during layout calculation\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -245,6 +247,7 @@ static YGSize YGJNIMeasureFunc(
|
|||||||
Log::log(
|
Log::log(
|
||||||
node,
|
node,
|
||||||
YGLogLevelError,
|
YGLogLevelError,
|
||||||
|
nullptr,
|
||||||
"Java YGNode was GCed during layout calculation\n");
|
"Java YGNode was GCed during layout calculation\n");
|
||||||
return YGSize{
|
return YGSize{
|
||||||
widthMode == YGMeasureModeUndefined ? 0 : width,
|
widthMode == YGMeasureModeUndefined ? 0 : width,
|
||||||
|
@@ -6,13 +6,21 @@
|
|||||||
*/
|
*/
|
||||||
#include "YGConfig.h"
|
#include "YGConfig.h"
|
||||||
|
|
||||||
YGConfig::YGConfig(YGLogger logger) : logger_{logger} {}
|
YGConfig::YGConfig(YGLogger logger) {
|
||||||
|
logger_.noContext = logger;
|
||||||
|
loggerUsesContext_ = false;
|
||||||
|
}
|
||||||
|
|
||||||
void YGConfig::log(
|
void YGConfig::log(
|
||||||
YGConfig* config,
|
YGConfig* config,
|
||||||
YGNode* node,
|
YGNode* node,
|
||||||
YGLogLevel logLevel,
|
YGLogLevel logLevel,
|
||||||
|
void* logContext,
|
||||||
const char* format,
|
const char* format,
|
||||||
va_list args) {
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -10,8 +10,20 @@
|
|||||||
#include "Yoga.h"
|
#include "Yoga.h"
|
||||||
|
|
||||||
struct YGConfig {
|
struct YGConfig {
|
||||||
|
using LogWithContextFn = void (*)(
|
||||||
|
YGConfigRef config,
|
||||||
|
YGNodeRef node,
|
||||||
|
YGLogLevel level,
|
||||||
|
void* context,
|
||||||
|
const char* format,
|
||||||
|
va_list args);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
YGLogger logger_;
|
union {
|
||||||
|
LogWithContextFn withContext;
|
||||||
|
YGLogger noContext;
|
||||||
|
} logger_;
|
||||||
|
bool loggerUsesContext_;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
bool useWebDefaults = false;
|
bool useWebDefaults = false;
|
||||||
@@ -26,8 +38,16 @@ public:
|
|||||||
YGMarkerCallbacks markerCallbacks = {nullptr, nullptr};
|
YGMarkerCallbacks markerCallbacks = {nullptr, nullptr};
|
||||||
|
|
||||||
YGConfig(YGLogger logger);
|
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) {
|
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});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@@ -1030,7 +1030,7 @@ static void YGNodePrintInternal(
|
|||||||
const YGPrintOptions options) {
|
const YGPrintOptions options) {
|
||||||
std::string str;
|
std::string str;
|
||||||
facebook::yoga::YGNodeToString(str, node, options, 0);
|
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) {
|
void YGNodePrint(const YGNodeRef node, const YGPrintOptions options) {
|
||||||
@@ -3819,6 +3819,7 @@ bool YGLayoutNodeInternal(
|
|||||||
Log::log(
|
Log::log(
|
||||||
node,
|
node,
|
||||||
YGLogLevelVerbose,
|
YGLogLevelVerbose,
|
||||||
|
nullptr,
|
||||||
"%s%d.{[skipped] ",
|
"%s%d.{[skipped] ",
|
||||||
YGSpacer(gDepth),
|
YGSpacer(gDepth),
|
||||||
gDepth);
|
gDepth);
|
||||||
@@ -3828,6 +3829,7 @@ bool YGLayoutNodeInternal(
|
|||||||
Log::log(
|
Log::log(
|
||||||
node,
|
node,
|
||||||
YGLogLevelVerbose,
|
YGLogLevelVerbose,
|
||||||
|
nullptr,
|
||||||
"wm: %s, hm: %s, aw: %f ah: %f => d: (%f, %f) %s\n",
|
"wm: %s, hm: %s, aw: %f ah: %f => d: (%f, %f) %s\n",
|
||||||
YGMeasureModeName(widthMeasureMode, performLayout),
|
YGMeasureModeName(widthMeasureMode, performLayout),
|
||||||
YGMeasureModeName(heightMeasureMode, performLayout),
|
YGMeasureModeName(heightMeasureMode, performLayout),
|
||||||
@@ -3842,6 +3844,7 @@ bool YGLayoutNodeInternal(
|
|||||||
Log::log(
|
Log::log(
|
||||||
node,
|
node,
|
||||||
YGLogLevelVerbose,
|
YGLogLevelVerbose,
|
||||||
|
nullptr,
|
||||||
"%s%d.{%s",
|
"%s%d.{%s",
|
||||||
YGSpacer(gDepth),
|
YGSpacer(gDepth),
|
||||||
gDepth,
|
gDepth,
|
||||||
@@ -3852,6 +3855,7 @@ bool YGLayoutNodeInternal(
|
|||||||
Log::log(
|
Log::log(
|
||||||
node,
|
node,
|
||||||
YGLogLevelVerbose,
|
YGLogLevelVerbose,
|
||||||
|
nullptr,
|
||||||
"wm: %s, hm: %s, aw: %f ah: %f %s\n",
|
"wm: %s, hm: %s, aw: %f ah: %f %s\n",
|
||||||
YGMeasureModeName(widthMeasureMode, performLayout),
|
YGMeasureModeName(widthMeasureMode, performLayout),
|
||||||
YGMeasureModeName(heightMeasureMode, performLayout),
|
YGMeasureModeName(heightMeasureMode, performLayout),
|
||||||
@@ -3877,6 +3881,7 @@ bool YGLayoutNodeInternal(
|
|||||||
Log::log(
|
Log::log(
|
||||||
node,
|
node,
|
||||||
YGLogLevelVerbose,
|
YGLogLevelVerbose,
|
||||||
|
nullptr,
|
||||||
"%s%d.}%s",
|
"%s%d.}%s",
|
||||||
YGSpacer(gDepth),
|
YGSpacer(gDepth),
|
||||||
gDepth,
|
gDepth,
|
||||||
@@ -3887,6 +3892,7 @@ bool YGLayoutNodeInternal(
|
|||||||
Log::log(
|
Log::log(
|
||||||
node,
|
node,
|
||||||
YGLogLevelVerbose,
|
YGLogLevelVerbose,
|
||||||
|
nullptr,
|
||||||
"wm: %s, hm: %s, d: (%f, %f) %s\n",
|
"wm: %s, hm: %s, d: (%f, %f) %s\n",
|
||||||
YGMeasureModeName(widthMeasureMode, performLayout),
|
YGMeasureModeName(widthMeasureMode, performLayout),
|
||||||
YGMeasureModeName(heightMeasureMode, performLayout),
|
YGMeasureModeName(heightMeasureMode, performLayout),
|
||||||
@@ -3905,7 +3911,7 @@ bool YGLayoutNodeInternal(
|
|||||||
}
|
}
|
||||||
if (layout->nextCachedMeasurementsIndex == YG_MAX_CACHED_RESULT_COUNT) {
|
if (layout->nextCachedMeasurementsIndex == YG_MAX_CACHED_RESULT_COUNT) {
|
||||||
if (gPrintChanges) {
|
if (gPrintChanges) {
|
||||||
Log::log(node, YGLogLevelVerbose, "Out of cache entries!\n");
|
Log::log(node, YGLogLevelVerbose, nullptr, "Out of cache entries!\n");
|
||||||
}
|
}
|
||||||
layout->nextCachedMeasurementsIndex = 0;
|
layout->nextCachedMeasurementsIndex = 0;
|
||||||
}
|
}
|
||||||
@@ -4200,7 +4206,7 @@ void YGConfigSetShouldDiffLayoutWithoutLegacyStretchBehaviour(
|
|||||||
|
|
||||||
void YGAssert(const bool condition, const char* message) {
|
void YGAssert(const bool condition, const char* message) {
|
||||||
if (!condition) {
|
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 bool condition,
|
||||||
const char* message) {
|
const char* message) {
|
||||||
if (!condition) {
|
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 bool condition,
|
||||||
const char* message) {
|
const char* message) {
|
||||||
if (!condition) {
|
if (!condition) {
|
||||||
Log::log(config, YGLogLevelFatal, "%s\n", message);
|
Log::log(config, YGLogLevelFatal, nullptr, "%s\n", message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
14
yoga/log.cpp
14
yoga/log.cpp
@@ -21,10 +21,11 @@ void vlog(
|
|||||||
YGConfig* config,
|
YGConfig* config,
|
||||||
YGNode* node,
|
YGNode* node,
|
||||||
YGLogLevel level,
|
YGLogLevel level,
|
||||||
|
void* context,
|
||||||
const char* format,
|
const char* format,
|
||||||
va_list args) {
|
va_list args) {
|
||||||
YGConfig* logConfig = config != nullptr ? config : YGConfigGetDefault();
|
YGConfig* logConfig = config != nullptr ? config : YGConfigGetDefault();
|
||||||
logConfig->log(logConfig, node, level, format, args);
|
logConfig->log(logConfig, node, level, context, format, args);
|
||||||
|
|
||||||
if (level == YGLogLevelFatal) {
|
if (level == YGLogLevelFatal) {
|
||||||
abort();
|
abort();
|
||||||
@@ -35,23 +36,30 @@ void vlog(
|
|||||||
void Log::log(
|
void Log::log(
|
||||||
YGNode* node,
|
YGNode* node,
|
||||||
YGLogLevel level,
|
YGLogLevel level,
|
||||||
|
void* context,
|
||||||
const char* format,
|
const char* format,
|
||||||
...) noexcept {
|
...) noexcept {
|
||||||
va_list args;
|
va_list args;
|
||||||
va_start(args, format);
|
va_start(args, format);
|
||||||
vlog(
|
vlog(
|
||||||
node == nullptr ? nullptr : node->getConfig(), node, level, format, args);
|
node == nullptr ? nullptr : node->getConfig(),
|
||||||
|
node,
|
||||||
|
level,
|
||||||
|
context,
|
||||||
|
format,
|
||||||
|
args);
|
||||||
va_end(args);
|
va_end(args);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Log::log(
|
void Log::log(
|
||||||
YGConfig* config,
|
YGConfig* config,
|
||||||
YGLogLevel level,
|
YGLogLevel level,
|
||||||
|
void* context,
|
||||||
const char* format,
|
const char* format,
|
||||||
...) noexcept {
|
...) noexcept {
|
||||||
va_list args;
|
va_list args;
|
||||||
va_start(args, format);
|
va_start(args, format);
|
||||||
vlog(config, nullptr, level, format, args);
|
vlog(config, nullptr, level, context, format, args);
|
||||||
va_end(args);
|
va_end(args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -20,12 +20,14 @@ struct Log {
|
|||||||
static void log(
|
static void log(
|
||||||
YGNode* node,
|
YGNode* node,
|
||||||
YGLogLevel level,
|
YGLogLevel level,
|
||||||
|
void*,
|
||||||
const char* message,
|
const char* message,
|
||||||
...) noexcept;
|
...) noexcept;
|
||||||
|
|
||||||
static void log(
|
static void log(
|
||||||
YGConfig* config,
|
YGConfig* config,
|
||||||
YGLogLevel level,
|
YGLogLevel level,
|
||||||
|
void*,
|
||||||
const char* format,
|
const char* format,
|
||||||
...) noexcept;
|
...) noexcept;
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user