Skip to content

Commit 378da73

Browse files
sherginfacebook-github-bot
authored andcommitted
DebugStringConvertibleOptions: Formating
Summary: DebugStringConvertibleOptions allows pretty-format debug strings. https://pxl.cl/ch0m Reviewed By: fkgozali Differential Revision: D7312622 fbshipit-source-id: 0ed62520bbc521790bedf5a6d18c796b42f85658
1 parent 4cda0df commit 378da73

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

ReactCommon/fabric/debug/DebugStringConvertible.cpp

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,31 @@
1010
namespace facebook {
1111
namespace react {
1212

13-
std::string DebugStringConvertible::getDebugChildrenDescription(int level) const {
13+
std::string DebugStringConvertible::getDebugChildrenDescription(DebugStringConvertibleOptions options, int depth) const {
14+
if (depth >= options.maximumDepth) {
15+
return "";
16+
}
17+
1418
std::string childrenString = "";
1519

1620
for (auto child : getDebugChildren()) {
17-
childrenString += child->getDebugDescription(level + 1);
21+
childrenString += child->getDebugDescription(options, depth + 1);
1822
}
1923

2024
return childrenString;
2125
}
2226

23-
std::string DebugStringConvertible::getDebugPropsDescription(int level) const {
27+
std::string DebugStringConvertible::getDebugPropsDescription(DebugStringConvertibleOptions options, int depth) const {
28+
if (depth >= options.maximumDepth) {
29+
return "";
30+
}
31+
2432
std::string propsString = "";
2533

2634
for (auto prop : getDebugProps()) {
2735
auto name = prop->getDebugName();
2836
auto value = prop->getDebugValue();
29-
auto children = prop->getDebugPropsDescription(level + 1);
37+
auto children = prop->getDebugPropsDescription(options, depth + 1);
3038
auto valueAndChildren = value + (children.empty() ? "" : "(" + children + ")");
3139
propsString += " " + name + (valueAndChildren.empty() ? "" : "=" + valueAndChildren);
3240
}
@@ -39,16 +47,19 @@ std::string DebugStringConvertible::getDebugPropsDescription(int level) const {
3947
return propsString;
4048
}
4149

42-
std::string DebugStringConvertible::getDebugDescription(int level) const {
50+
std::string DebugStringConvertible::getDebugDescription(DebugStringConvertibleOptions options, int depth) const {
4351
std::string nameString = getDebugName();
4452
std::string valueString = getDebugValue();
45-
std::string childrenString = getDebugChildrenDescription(level);
46-
std::string propsString = getDebugPropsDescription(level);
53+
std::string childrenString = getDebugChildrenDescription(options, depth + 1);
54+
std::string propsString = getDebugPropsDescription(options, depth /* The first-level props are considered as same-depth things. */);
55+
56+
std::string leading = options.format ? std::string(depth, '\t') : "";
57+
std::string trailing = options.format ? "\n" : "";
4758

48-
return "<" + nameString +
59+
return leading + "<" + nameString +
4960
(valueString.empty() ? "" : "=" + valueString) +
5061
(propsString.empty() ? "" : " " + propsString) +
51-
(childrenString.empty() ? "/>" : ">" + childrenString + "</" + nameString + ">");
62+
(childrenString.empty() ? "/>" + trailing : ">" + trailing + childrenString + leading + "</" + nameString + ">" + trailing);
5263
}
5364

5465
std::string DebugStringConvertible::getDebugName() const {

ReactCommon/fabric/debug/DebugStringConvertible.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ class DebugStringConvertible;
1818
using SharedDebugStringConvertible = std::shared_ptr<const DebugStringConvertible>;
1919
using SharedDebugStringConvertibleList = std::vector<const SharedDebugStringConvertible>;
2020

21+
struct DebugStringConvertibleOptions {
22+
bool format {true};
23+
int maximumDepth {INT_MAX};
24+
};
25+
2126
// Abstract class describes conformance to DebugStringConvertible concept
2227
// and implements basic recursive debug string assembly algorithm.
2328
// Use this as a base class for providing a debugging textual representation
@@ -47,12 +52,12 @@ class DebugStringConvertible {
4752
// Returns a string which represents the object in a human-readable way.
4853
// Default implementation returns a description of the subtree
4954
// rooted at this node, represented in XML-like format.
50-
virtual std::string getDebugDescription(int level = 0) const;
55+
virtual std::string getDebugDescription(DebugStringConvertibleOptions options = {}, int depth = 0) const;
5156

5257
// Do same as `getDebugDescription` but return only *children* and
5358
// *properties* parts (which are used in `getDebugDescription`).
54-
virtual std::string getDebugPropsDescription(int level = 0) const;
55-
virtual std::string getDebugChildrenDescription(int level = 0) const;
59+
virtual std::string getDebugPropsDescription(DebugStringConvertibleOptions options = {}, int depth = 0) const;
60+
virtual std::string getDebugChildrenDescription(DebugStringConvertibleOptions options = {}, int depth = 0) const;
5661
};
5762

5863
} // namespace react

0 commit comments

Comments
 (0)