Skip to content

Commit 586b570

Browse files
woehrl01facebook-github-bot
authored andcommitted
Optimize log print by using html format
Summary: See #453. Optimizes the node log print by generating some enum text via ```enum.py``` and moving printing to new functions to reduce boilerplate code. Changes the log output to format the nodes in html to be able to copy paste it into browsers for quick debugging. Hides all default values. Closes #479 Reviewed By: gkassabli Differential Revision: D4802184 Pulled By: emilsjolander fbshipit-source-id: 143bd63cbc31fb0755d711062cb4e6a448049ba3
1 parent 5112564 commit 586b570

File tree

8 files changed

+455
-165
lines changed

8 files changed

+455
-165
lines changed

csharp/tests/Facebook.Yoga/YogaNodeTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ public void TestPrint()
255255
parent.Insert(0, child0);
256256
parent.Insert(0, child1);
257257
parent.CalculateLayout();
258-
Assert.AreEqual("{layout: {width: 100, height: 120, top: 0, left: 0}, flexDirection: 'column', alignItems: 'stretch', flexGrow: 0, flexShrink: 0, flexBasis: nan%, overflow: 'visible', width: 100pt, height: 120pt, children: [\n {layout: {width: 35, height: 45, top: 0, left: 0}, flexDirection: 'column', alignItems: 'stretch', flexGrow: 0, flexShrink: 0, flexBasis: nan%, overflow: 'visible', width: 35pt, height: 45pt, },\n {layout: {width: 30, height: 40, top: 45, left: 0}, flexDirection: 'column', alignItems: 'stretch', flexGrow: 0, flexShrink: 0, flexBasis: nan%, overflow: 'visible', width: 30pt, height: 40pt, },\n]},\n", parent.Print());
258+
Assert.AreEqual("<div layout=\"width: 100; height: 120; top: 0; left: 0;\" style=\"width: 100px; height: 120px; \" >\n <div layout=\"width: 35; height: 45; top: 0; left: 0;\" style=\"width: 35px; height: 45px; \" ></div>\n <div layout=\"width: 30; height: 40; top: 45; left: 0;\" style=\"width: 30px; height: 40px; \" ></div>\n</div>", parent.Print());
259259
}
260260

261261
[Test]

enums.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,16 @@ def to_java_upper(symbol):
125125
out += c.upper()
126126
return out
127127

128+
def to_log_lower(symbol):
129+
symbol = str(symbol)
130+
out = ''
131+
for i in range(0, len(symbol)):
132+
c = symbol[i]
133+
if str.istitle(c) and i is not 0 and not str.istitle(symbol[i - 1]):
134+
out += '-'
135+
out += c.lower()
136+
return out
137+
128138

129139
root = os.path.dirname(os.path.abspath(__file__))
130140

@@ -143,9 +153,28 @@ def to_java_upper(symbol):
143153
else:
144154
f.write(' YG%s%s,\n' % (name, value))
145155
f.write('} YG_ENUM_END(YG%s);\n' % name)
156+
f.write('WIN_EXPORT const char *YG%sToString(const YG%s value);\n' % (name, name))
146157
f.write('\n')
147158
f.write('YG_EXTERN_C_END\n')
148159

160+
# write out C body for printing
161+
with open(root + '/yoga/YGEnums.c', 'w') as f:
162+
f.write(LICENSE)
163+
f.write('#include "YGEnums.h"\n\n')
164+
for name, values in sorted(ENUMS.items()):
165+
f.write('const char *YG%sToString(const YG%s value){\n' % (name, name))
166+
f.write(' switch(value){\n')
167+
for value in values:
168+
if isinstance(value, tuple):
169+
f.write(' case YG%s%s:\n' % (name, value[0]))
170+
f.write(' return "%s";\n' % to_log_lower(value[0]))
171+
else:
172+
f.write(' case YG%s%s:\n' % (name, value))
173+
f.write(' return "%s";\n' % to_log_lower(value))
174+
f.write(' }\n')
175+
f.write(' return "unknown";\n')
176+
f.write('}\n\n')
177+
149178
# write out java files
150179
for name, values in sorted(ENUMS.items()):
151180
with open(root + '/java/com/facebook/yoga/Yoga%s.java' % name, 'w') as f:

tests/YGLoggerTest.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
#include <gtest/gtest.h>
11+
#include <yoga/Yoga.h>
12+
#include <stdarg.h>
13+
14+
namespace {
15+
char writeBuffer[4096];
16+
int _unmanagedLogger(YGLogLevel level, const char *format, va_list args) {
17+
return vsnprintf(writeBuffer + strlen(writeBuffer), sizeof(writeBuffer) - strlen(writeBuffer), format, args);
18+
}
19+
}
20+
21+
TEST(YogaTest, logger_default_node_should_print_no_style_info) {
22+
writeBuffer[0] = '\0';
23+
YGSetLogger(_unmanagedLogger);
24+
const YGNodeRef root = YGNodeNew();
25+
YGNodeCalculateLayout(root, YGUnitUndefined, YGUnitUndefined, YGDirectionLTR);
26+
YGNodePrint(root, (YGPrintOptions)(YGPrintOptionsLayout | YGPrintOptionsChildren | YGPrintOptionsStyle));
27+
YGSetLogger(NULL);
28+
YGNodeFree(root);
29+
30+
const char * expected = "<div layout=\"width: 0; height: 0; top: 0; left: 0;\" style=\"\" ></div>";
31+
ASSERT_STREQ(expected, writeBuffer);
32+
}
33+
34+
TEST(YogaTest, logger_node_with_percentage_absolute_position_and_margin) {
35+
writeBuffer[0] = '\0';
36+
YGSetLogger(_unmanagedLogger);
37+
const YGNodeRef root = YGNodeNew();
38+
YGNodeStyleSetPositionType(root, YGPositionTypeAbsolute);
39+
YGNodeStyleSetWidthPercent(root, 50);
40+
YGNodeStyleSetHeightPercent(root, 75);
41+
YGNodeStyleSetFlex(root, 1);
42+
YGNodeStyleSetMargin(root, YGEdgeRight, 10);
43+
YGNodeStyleSetMarginAuto(root, YGEdgeLeft);
44+
YGNodeCalculateLayout(root, YGUnitUndefined, YGUnitUndefined, YGDirectionLTR);
45+
YGNodePrint(root, (YGPrintOptions)(YGPrintOptionsLayout | YGPrintOptionsChildren | YGPrintOptionsStyle));
46+
YGSetLogger(NULL);
47+
YGNodeFree(root);
48+
49+
const char * expected = "<div layout=\"width: 0; height: 0; top: 0; left: 0;\" style=\"flex: 1; margin-left: auto; margin-right: 10px; width: 50%; height: 75%; position: absolute; \" ></div>";
50+
ASSERT_STREQ(expected, writeBuffer);
51+
}
52+
53+
TEST(YogaTest, logger_node_with_children_should_print_indented) {
54+
writeBuffer[0] = '\0';
55+
YGSetLogger(_unmanagedLogger);
56+
const YGNodeRef root = YGNodeNew();
57+
const YGNodeRef child0 = YGNodeNew();
58+
const YGNodeRef child1 = YGNodeNew();
59+
YGNodeInsertChild(root, child0, 0);
60+
YGNodeInsertChild(root, child1, 1);
61+
YGNodeCalculateLayout(root, YGUnitUndefined, YGUnitUndefined, YGDirectionLTR);
62+
YGNodePrint(root, (YGPrintOptions)(YGPrintOptionsLayout | YGPrintOptionsChildren | YGPrintOptionsStyle));
63+
YGSetLogger(NULL);
64+
YGNodeFreeRecursive(root);
65+
66+
const char * expected = "<div layout=\"width: 0; height: 0; top: 0; left: 0;\" style=\"\" >\n <div layout=\"width: 0; height: 0; top: 0; left: 0;\" style=\"\" ></div>\n <div layout=\"width: 0; height: 0; top: 0; left: 0;\" style=\"\" ></div>\n</div>";
67+
ASSERT_STREQ(expected, writeBuffer);
68+
}

tests/YGMeasureTest.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ TEST(YogaTest, dont_measure_when_min_equals_max_mixed_height_percent) {
180180
}
181181

182182
#if GTEST_HAS_DEATH_TEST
183-
TEST(YogaTest, cannot_add_child_to_node_with_measure_func) {
183+
TEST(YogaDeathTest, cannot_add_child_to_node_with_measure_func) {
184184
const YGNodeRef root = YGNodeNew();
185185
YGNodeSetMeasureFunc(root, _measure);
186186

@@ -190,7 +190,7 @@ TEST(YogaTest, cannot_add_child_to_node_with_measure_func) {
190190
YGNodeFreeRecursive(root);
191191
}
192192

193-
TEST(YogaTest, cannot_add_nonnull_measure_func_to_non_leaf_node) {
193+
TEST(YogaDeathTest, cannot_add_nonnull_measure_func_to_non_leaf_node) {
194194
const YGNodeRef root = YGNodeNew();
195195
const YGNodeRef root_child0 = YGNodeNew();
196196
YGNodeInsertChild(root, root_child0, 0);
@@ -199,6 +199,8 @@ TEST(YogaTest, cannot_add_nonnull_measure_func_to_non_leaf_node) {
199199
YGNodeFreeRecursive(root);
200200
}
201201

202+
#endif
203+
202204
TEST(YogaTest, can_nullify_measure_func_on_any_node) {
203205
const YGNodeRef root = YGNodeNew();
204206
YGNodeInsertChild(root, YGNodeNew(), 0);
@@ -208,4 +210,3 @@ TEST(YogaTest, can_nullify_measure_func_on_any_node) {
208210
YGNodeFreeRecursive(root);
209211
}
210212

211-
#endif

tests/YGMemoryFuncTest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,15 @@ TEST(YogaTest, memory_func_test_funcs) {
6363
}
6464

6565
#if GTEST_HAS_DEATH_TEST
66-
TEST(YogaTest, memory_func_assert_zero_nodes) {
66+
TEST(YogaDeathTest, memory_func_assert_zero_nodes) {
6767
gNodeInstanceCount = 0; // Reset YGNode instance count for memory func test
6868
const YGNodeRef root = YGNodeNew();
6969
ASSERT_DEATH(YGSetMemoryFuncs(&testMalloc, &testCalloc, &testRealloc, &testFree),
7070
"Cannot set memory functions: all node must be freed first");
7171
YGNodeFreeRecursive(root);
7272
}
7373

74-
TEST(YogaTest, memory_func_assert_all_non_null) {
74+
TEST(YogaDeathTest, memory_func_assert_all_non_null) {
7575
gNodeInstanceCount = 0; // Reset YGNode instance count for memory func test
7676
ASSERT_DEATH(YGSetMemoryFuncs(NULL, &testCalloc, &testRealloc, &testFree),
7777
"Cannot set memory functions: functions must be all NULL or Non-NULL");

yoga/YGEnums.c

Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
#include "YGEnums.h"
11+
12+
const char *YGAlignToString(const YGAlign value){
13+
switch(value){
14+
case YGAlignAuto:
15+
return "auto";
16+
case YGAlignFlexStart:
17+
return "flex-start";
18+
case YGAlignCenter:
19+
return "center";
20+
case YGAlignFlexEnd:
21+
return "flex-end";
22+
case YGAlignStretch:
23+
return "stretch";
24+
case YGAlignBaseline:
25+
return "baseline";
26+
case YGAlignSpaceBetween:
27+
return "space-between";
28+
case YGAlignSpaceAround:
29+
return "space-around";
30+
}
31+
return "unknown";
32+
}
33+
34+
const char *YGDimensionToString(const YGDimension value){
35+
switch(value){
36+
case YGDimensionWidth:
37+
return "width";
38+
case YGDimensionHeight:
39+
return "height";
40+
}
41+
return "unknown";
42+
}
43+
44+
const char *YGDirectionToString(const YGDirection value){
45+
switch(value){
46+
case YGDirectionInherit:
47+
return "inherit";
48+
case YGDirectionLTR:
49+
return "ltr";
50+
case YGDirectionRTL:
51+
return "rtl";
52+
}
53+
return "unknown";
54+
}
55+
56+
const char *YGDisplayToString(const YGDisplay value){
57+
switch(value){
58+
case YGDisplayFlex:
59+
return "flex";
60+
case YGDisplayNone:
61+
return "none";
62+
}
63+
return "unknown";
64+
}
65+
66+
const char *YGEdgeToString(const YGEdge value){
67+
switch(value){
68+
case YGEdgeLeft:
69+
return "left";
70+
case YGEdgeTop:
71+
return "top";
72+
case YGEdgeRight:
73+
return "right";
74+
case YGEdgeBottom:
75+
return "bottom";
76+
case YGEdgeStart:
77+
return "start";
78+
case YGEdgeEnd:
79+
return "end";
80+
case YGEdgeHorizontal:
81+
return "horizontal";
82+
case YGEdgeVertical:
83+
return "vertical";
84+
case YGEdgeAll:
85+
return "all";
86+
}
87+
return "unknown";
88+
}
89+
90+
const char *YGExperimentalFeatureToString(const YGExperimentalFeature value){
91+
switch(value){
92+
case YGExperimentalFeatureRounding:
93+
return "rounding";
94+
case YGExperimentalFeatureWebFlexBasis:
95+
return "web-flex-basis";
96+
case YGExperimentalFeatureMinFlexFix:
97+
return "min-flex-fix";
98+
}
99+
return "unknown";
100+
}
101+
102+
const char *YGFlexDirectionToString(const YGFlexDirection value){
103+
switch(value){
104+
case YGFlexDirectionColumn:
105+
return "column";
106+
case YGFlexDirectionColumnReverse:
107+
return "column-reverse";
108+
case YGFlexDirectionRow:
109+
return "row";
110+
case YGFlexDirectionRowReverse:
111+
return "row-reverse";
112+
}
113+
return "unknown";
114+
}
115+
116+
const char *YGJustifyToString(const YGJustify value){
117+
switch(value){
118+
case YGJustifyFlexStart:
119+
return "flex-start";
120+
case YGJustifyCenter:
121+
return "center";
122+
case YGJustifyFlexEnd:
123+
return "flex-end";
124+
case YGJustifySpaceBetween:
125+
return "space-between";
126+
case YGJustifySpaceAround:
127+
return "space-around";
128+
}
129+
return "unknown";
130+
}
131+
132+
const char *YGLogLevelToString(const YGLogLevel value){
133+
switch(value){
134+
case YGLogLevelError:
135+
return "error";
136+
case YGLogLevelWarn:
137+
return "warn";
138+
case YGLogLevelInfo:
139+
return "info";
140+
case YGLogLevelDebug:
141+
return "debug";
142+
case YGLogLevelVerbose:
143+
return "verbose";
144+
}
145+
return "unknown";
146+
}
147+
148+
const char *YGMeasureModeToString(const YGMeasureMode value){
149+
switch(value){
150+
case YGMeasureModeUndefined:
151+
return "undefined";
152+
case YGMeasureModeExactly:
153+
return "exactly";
154+
case YGMeasureModeAtMost:
155+
return "at-most";
156+
}
157+
return "unknown";
158+
}
159+
160+
const char *YGOverflowToString(const YGOverflow value){
161+
switch(value){
162+
case YGOverflowVisible:
163+
return "visible";
164+
case YGOverflowHidden:
165+
return "hidden";
166+
case YGOverflowScroll:
167+
return "scroll";
168+
}
169+
return "unknown";
170+
}
171+
172+
const char *YGPositionTypeToString(const YGPositionType value){
173+
switch(value){
174+
case YGPositionTypeRelative:
175+
return "relative";
176+
case YGPositionTypeAbsolute:
177+
return "absolute";
178+
}
179+
return "unknown";
180+
}
181+
182+
const char *YGPrintOptionsToString(const YGPrintOptions value){
183+
switch(value){
184+
case YGPrintOptionsLayout:
185+
return "layout";
186+
case YGPrintOptionsStyle:
187+
return "style";
188+
case YGPrintOptionsChildren:
189+
return "children";
190+
}
191+
return "unknown";
192+
}
193+
194+
const char *YGUnitToString(const YGUnit value){
195+
switch(value){
196+
case YGUnitUndefined:
197+
return "undefined";
198+
case YGUnitPoint:
199+
return "point";
200+
case YGUnitPercent:
201+
return "percent";
202+
case YGUnitAuto:
203+
return "auto";
204+
}
205+
return "unknown";
206+
}
207+
208+
const char *YGWrapToString(const YGWrap value){
209+
switch(value){
210+
case YGWrapNoWrap:
211+
return "no-wrap";
212+
case YGWrapWrap:
213+
return "wrap";
214+
case YGWrapWrapReverse:
215+
return "wrap-reverse";
216+
}
217+
return "unknown";
218+
}
219+

0 commit comments

Comments
 (0)