Update logging to support levels, print messages in Android logcat on assertion failures

Summary: The goal of this diff is to have assertion failures show up as error logs on Android. To do this, I updated the logging API to take calls with log levels. We now have to pass around va_list unfortunately since you can't re-expand or pass along var-args to a subcall.

Reviewed By: emilsjolander

Differential Revision: D4140898

fbshipit-source-id: e0eb9a1f0b08a7d90a8233f66bb857d5b871b6ad
This commit is contained in:
Andy Street 2016-11-09 09:45:37 -08:00 committed by Facebook Github Bot
parent 33fb428a07
commit a731a23d91
3 changed files with 86 additions and 43 deletions

View File

@ -104,16 +104,43 @@ static void _CSSNodeMarkDirty(const CSSNodeRef node);
#ifdef ANDROID #ifdef ANDROID
#include <android/log.h> #include <android/log.h>
static int _csslayoutAndroidLog(const char *format, ...) { static int _csslayoutAndroidLog(CSSLogLevel level, const char *format, va_list args) {
va_list args; int androidLevel = CSSLogLevelDebug;
va_start(args, format); switch (level) {
const int result = __android_log_vprint(ANDROID_LOG_DEBUG, "css-layout", format, args); case CSSLogLevelError:
va_end(args); androidLevel = ANDROID_LOG_ERROR;
break;
case CSSLogLevelWarn:
androidLevel = ANDROID_LOG_WARN;
break;
case CSSLogLevelInfo:
androidLevel = ANDROID_LOG_INFO;
break;
case CSSLogLevelDebug:
androidLevel = ANDROID_LOG_DEBUG;
break;
case CSSLogLevelVerbose:
androidLevel = ANDROID_LOG_VERBOSE;
break;
}
const int result = __android_log_vprint(androidLevel, "css-layout", format, args);
return result; return result;
} }
static CSSLogger gLogger = &_csslayoutAndroidLog; static CSSLogger gLogger = &_csslayoutAndroidLog;
#else #else
static CSSLogger gLogger = &printf; static int _csslayoutDefaultLog(CSSLogLevel level, const char *format, va_list args) {
switch (level) {
case CSSLogLevelError:
return vfprintf(stderr, format, args);
case CSSLogLevelWarn:
case CSSLogLevelInfo:
case CSSLogLevelDebug:
case CSSLogLevelVerbose:
default:
return vprintf(format, args);
}
}
static CSSLogger gLogger = &_csslayoutDefaultLog;
#endif #endif
static inline float computedEdgeValue(const float edges[CSSEdgeCount], static inline float computedEdgeValue(const float edges[CSSEdgeCount],
@ -446,19 +473,19 @@ static inline bool eq(const float a, const float b) {
static void indent(const uint32_t n) { static void indent(const uint32_t n) {
for (uint32_t i = 0; i < n; i++) { for (uint32_t i = 0; i < n; i++) {
gLogger(" "); CSSLog(CSSLogLevelDebug, " ");
} }
} }
static void printNumberIfNotZero(const char *str, const float number) { static void printNumberIfNotZero(const char *str, const float number) {
if (!eq(number, 0)) { if (!eq(number, 0)) {
gLogger("%s: %g, ", str, number); CSSLog(CSSLogLevelDebug, "%s: %g, ", str, number);
} }
} }
static void printNumberIfNotUndefined(const char *str, const float number) { static void printNumberIfNotUndefined(const char *str, const float number) {
if (!CSSValueIsUndefined(number)) { if (!CSSValueIsUndefined(number)) {
gLogger("%s: %g, ", str, number); CSSLog(CSSLogLevelDebug, "%s: %g, ", str, number);
} }
} }
@ -470,66 +497,66 @@ static void _CSSNodePrint(const CSSNodeRef node,
const CSSPrintOptions options, const CSSPrintOptions options,
const uint32_t level) { const uint32_t level) {
indent(level); indent(level);
gLogger("{"); CSSLog(CSSLogLevelDebug, "{");
if (node->print) { if (node->print) {
node->print(node); node->print(node);
} }
if (options & CSSPrintOptionsLayout) { if (options & CSSPrintOptionsLayout) {
gLogger("layout: {"); CSSLog(CSSLogLevelDebug, "layout: {");
gLogger("width: %g, ", node->layout.dimensions[CSSDimensionWidth]); CSSLog(CSSLogLevelDebug, "width: %g, ", node->layout.dimensions[CSSDimensionWidth]);
gLogger("height: %g, ", node->layout.dimensions[CSSDimensionHeight]); CSSLog(CSSLogLevelDebug, "height: %g, ", node->layout.dimensions[CSSDimensionHeight]);
gLogger("top: %g, ", node->layout.position[CSSEdgeTop]); CSSLog(CSSLogLevelDebug, "top: %g, ", node->layout.position[CSSEdgeTop]);
gLogger("left: %g", node->layout.position[CSSEdgeLeft]); CSSLog(CSSLogLevelDebug, "left: %g", node->layout.position[CSSEdgeLeft]);
gLogger("}, "); CSSLog(CSSLogLevelDebug, "}, ");
} }
if (options & CSSPrintOptionsStyle) { if (options & CSSPrintOptionsStyle) {
if (node->style.flexDirection == CSSFlexDirectionColumn) { if (node->style.flexDirection == CSSFlexDirectionColumn) {
gLogger("flexDirection: 'column', "); CSSLog(CSSLogLevelDebug, "flexDirection: 'column', ");
} else if (node->style.flexDirection == CSSFlexDirectionColumnReverse) { } else if (node->style.flexDirection == CSSFlexDirectionColumnReverse) {
gLogger("flexDirection: 'column-reverse', "); CSSLog(CSSLogLevelDebug, "flexDirection: 'column-reverse', ");
} else if (node->style.flexDirection == CSSFlexDirectionRow) { } else if (node->style.flexDirection == CSSFlexDirectionRow) {
gLogger("flexDirection: 'row', "); CSSLog(CSSLogLevelDebug, "flexDirection: 'row', ");
} else if (node->style.flexDirection == CSSFlexDirectionRowReverse) { } else if (node->style.flexDirection == CSSFlexDirectionRowReverse) {
gLogger("flexDirection: 'row-reverse', "); CSSLog(CSSLogLevelDebug, "flexDirection: 'row-reverse', ");
} }
if (node->style.justifyContent == CSSJustifyCenter) { if (node->style.justifyContent == CSSJustifyCenter) {
gLogger("justifyContent: 'center', "); CSSLog(CSSLogLevelDebug, "justifyContent: 'center', ");
} else if (node->style.justifyContent == CSSJustifyFlexEnd) { } else if (node->style.justifyContent == CSSJustifyFlexEnd) {
gLogger("justifyContent: 'flex-end', "); CSSLog(CSSLogLevelDebug, "justifyContent: 'flex-end', ");
} else if (node->style.justifyContent == CSSJustifySpaceAround) { } else if (node->style.justifyContent == CSSJustifySpaceAround) {
gLogger("justifyContent: 'space-around', "); CSSLog(CSSLogLevelDebug, "justifyContent: 'space-around', ");
} else if (node->style.justifyContent == CSSJustifySpaceBetween) { } else if (node->style.justifyContent == CSSJustifySpaceBetween) {
gLogger("justifyContent: 'space-between', "); CSSLog(CSSLogLevelDebug, "justifyContent: 'space-between', ");
} }
if (node->style.alignItems == CSSAlignCenter) { if (node->style.alignItems == CSSAlignCenter) {
gLogger("alignItems: 'center', "); CSSLog(CSSLogLevelDebug, "alignItems: 'center', ");
} else if (node->style.alignItems == CSSAlignFlexEnd) { } else if (node->style.alignItems == CSSAlignFlexEnd) {
gLogger("alignItems: 'flex-end', "); CSSLog(CSSLogLevelDebug, "alignItems: 'flex-end', ");
} else if (node->style.alignItems == CSSAlignStretch) { } else if (node->style.alignItems == CSSAlignStretch) {
gLogger("alignItems: 'stretch', "); CSSLog(CSSLogLevelDebug, "alignItems: 'stretch', ");
} }
if (node->style.alignContent == CSSAlignCenter) { if (node->style.alignContent == CSSAlignCenter) {
gLogger("alignContent: 'center', "); CSSLog(CSSLogLevelDebug, "alignContent: 'center', ");
} else if (node->style.alignContent == CSSAlignFlexEnd) { } else if (node->style.alignContent == CSSAlignFlexEnd) {
gLogger("alignContent: 'flex-end', "); CSSLog(CSSLogLevelDebug, "alignContent: 'flex-end', ");
} else if (node->style.alignContent == CSSAlignStretch) { } else if (node->style.alignContent == CSSAlignStretch) {
gLogger("alignContent: 'stretch', "); CSSLog(CSSLogLevelDebug, "alignContent: 'stretch', ");
} }
if (node->style.alignSelf == CSSAlignFlexStart) { if (node->style.alignSelf == CSSAlignFlexStart) {
gLogger("alignSelf: 'flex-start', "); CSSLog(CSSLogLevelDebug, "alignSelf: 'flex-start', ");
} else if (node->style.alignSelf == CSSAlignCenter) { } else if (node->style.alignSelf == CSSAlignCenter) {
gLogger("alignSelf: 'center', "); CSSLog(CSSLogLevelDebug, "alignSelf: 'center', ");
} else if (node->style.alignSelf == CSSAlignFlexEnd) { } else if (node->style.alignSelf == CSSAlignFlexEnd) {
gLogger("alignSelf: 'flex-end', "); CSSLog(CSSLogLevelDebug, "alignSelf: 'flex-end', ");
} else if (node->style.alignSelf == CSSAlignStretch) { } else if (node->style.alignSelf == CSSAlignStretch) {
gLogger("alignSelf: 'stretch', "); CSSLog(CSSLogLevelDebug, "alignSelf: 'stretch', ");
} }
printNumberIfNotUndefined("flexGrow", CSSNodeStyleGetFlexGrow(node)); printNumberIfNotUndefined("flexGrow", CSSNodeStyleGetFlexGrow(node));
@ -537,11 +564,11 @@ static void _CSSNodePrint(const CSSNodeRef node,
printNumberIfNotUndefined("flexBasis", CSSNodeStyleGetFlexBasis(node)); printNumberIfNotUndefined("flexBasis", CSSNodeStyleGetFlexBasis(node));
if (node->style.overflow == CSSOverflowHidden) { if (node->style.overflow == CSSOverflowHidden) {
gLogger("overflow: 'hidden', "); CSSLog(CSSLogLevelDebug, "overflow: 'hidden', ");
} else if (node->style.overflow == CSSOverflowVisible) { } else if (node->style.overflow == CSSOverflowVisible) {
gLogger("overflow: 'visible', "); CSSLog(CSSLogLevelDebug, "overflow: 'visible', ");
} else if (node->style.overflow == CSSOverflowScroll) { } else if (node->style.overflow == CSSOverflowScroll) {
gLogger("overflow: 'scroll', "); CSSLog(CSSLogLevelDebug, "overflow: 'scroll', ");
} }
if (eqFour(node->style.margin)) { if (eqFour(node->style.margin)) {
@ -590,7 +617,7 @@ static void _CSSNodePrint(const CSSNodeRef node,
printNumberIfNotUndefined("minHeight", node->style.minDimensions[CSSDimensionHeight]); printNumberIfNotUndefined("minHeight", node->style.minDimensions[CSSDimensionHeight]);
if (node->style.positionType == CSSPositionTypeAbsolute) { if (node->style.positionType == CSSPositionTypeAbsolute) {
gLogger("position: 'absolute', "); CSSLog(CSSLogLevelDebug, "position: 'absolute', ");
} }
printNumberIfNotUndefined("left", printNumberIfNotUndefined("left",
@ -605,14 +632,14 @@ static void _CSSNodePrint(const CSSNodeRef node,
const uint32_t childCount = CSSNodeListCount(node->children); const uint32_t childCount = CSSNodeListCount(node->children);
if (options & CSSPrintOptionsChildren && childCount > 0) { if (options & CSSPrintOptionsChildren && childCount > 0) {
gLogger("children: [\n"); CSSLog(CSSLogLevelDebug, "children: [\n");
for (uint32_t i = 0; i < childCount; i++) { for (uint32_t i = 0; i < childCount; i++) {
_CSSNodePrint(CSSNodeGetChild(node, i), options, level + 1); _CSSNodePrint(CSSNodeGetChild(node, i), options, level + 1);
} }
indent(level); indent(level);
gLogger("]},\n"); CSSLog(CSSLogLevelDebug, "]},\n");
} else { } else {
gLogger("},\n"); CSSLog(CSSLogLevelDebug, "},\n");
} }
} }
@ -2441,6 +2468,13 @@ void CSSLayoutSetLogger(CSSLogger logger) {
gLogger = logger; gLogger = logger;
} }
void CSSLog(CSSLogLevel level, const char *format, ...) {
va_list args;
va_start(args, format);
gLogger(level, format, args);
va_end(args);
}
#ifdef CSS_ASSERT_FAIL_ENABLED #ifdef CSS_ASSERT_FAIL_ENABLED
static CSSAssertFailFunc gAssertFailFunc; static CSSAssertFailFunc gAssertFailFunc;

View File

@ -115,6 +115,14 @@ typedef struct CSSSize {
float height; float height;
} CSSSize; } CSSSize;
typedef enum CSSLogLevel {
CSSLogLevelError,
CSSLogLevelWarn,
CSSLogLevelInfo,
CSSLogLevelDebug,
CSSLogLevelVerbose,
} CSSLogLevel;
typedef struct CSSNode *CSSNodeRef; typedef struct CSSNode *CSSNodeRef;
typedef CSSSize (*CSSMeasureFunc)(CSSNodeRef node, typedef CSSSize (*CSSMeasureFunc)(CSSNodeRef node,
float width, float width,
@ -122,7 +130,7 @@ typedef CSSSize (*CSSMeasureFunc)(CSSNodeRef node,
float height, float height,
CSSMeasureMode heightMode); CSSMeasureMode heightMode);
typedef void (*CSSPrintFunc)(CSSNodeRef node); typedef void (*CSSPrintFunc)(CSSNodeRef node);
typedef int (*CSSLogger)(const char *format, ...); typedef int (*CSSLogger)(CSSLogLevel level, const char *format, va_list args);
#ifdef CSS_ASSERT_FAIL_ENABLED #ifdef CSS_ASSERT_FAIL_ENABLED
typedef void (*CSSAssertFailFunc)(const char *message); typedef void (*CSSAssertFailFunc)(const char *message);
@ -232,6 +240,7 @@ CSS_NODE_LAYOUT_PROPERTY(float, Height);
CSS_NODE_LAYOUT_PROPERTY(CSSDirection, Direction); CSS_NODE_LAYOUT_PROPERTY(CSSDirection, Direction);
WIN_EXPORT void CSSLayoutSetLogger(CSSLogger logger); WIN_EXPORT void CSSLayoutSetLogger(CSSLogger logger);
WIN_EXPORT void CSSLog(CSSLogLevel level, const char *message, ...);
#ifdef CSS_ASSERT_FAIL_ENABLED #ifdef CSS_ASSERT_FAIL_ENABLED
// Assert // Assert

View File

@ -36,7 +36,7 @@
#if CSS_ASSERT_FAIL_ENABLED #if CSS_ASSERT_FAIL_ENABLED
#define CSS_ERROR_FUNC(message) CSSAssertFail(message) #define CSS_ERROR_FUNC(message) CSSAssertFail(message)
#else #else
#define CSS_ERROR_FUNC(message) fprintf(stderr, "%s", message) #define CSS_ERROR_FUNC(message) CSSLog(CSSLogLevelError, "%s", message)
#endif #endif
#ifndef CSS_ASSERT #ifndef CSS_ASSERT