DebugStringConvertibleOptions: Formating

Summary:
DebugStringConvertibleOptions allows pretty-format debug strings.
https://pxl.cl/ch0m

Reviewed By: fkgozali

Differential Revision: D7312622

fbshipit-source-id: 0ed62520bbc521790bedf5a6d18c796b42f85658
This commit is contained in:
Valentin Shergin
2018-03-18 19:17:39 -07:00
committed by Facebook Github Bot
parent 4cda0df2e5
commit 378da73201
2 changed files with 28 additions and 12 deletions
@@ -10,23 +10,31 @@
namespace facebook {
namespace react {
std::string DebugStringConvertible::getDebugChildrenDescription(int level) const {
std::string DebugStringConvertible::getDebugChildrenDescription(DebugStringConvertibleOptions options, int depth) const {
if (depth >= options.maximumDepth) {
return "";
}
std::string childrenString = "";
for (auto child : getDebugChildren()) {
childrenString += child->getDebugDescription(level + 1);
childrenString += child->getDebugDescription(options, depth + 1);
}
return childrenString;
}
std::string DebugStringConvertible::getDebugPropsDescription(int level) const {
std::string DebugStringConvertible::getDebugPropsDescription(DebugStringConvertibleOptions options, int depth) const {
if (depth >= options.maximumDepth) {
return "";
}
std::string propsString = "";
for (auto prop : getDebugProps()) {
auto name = prop->getDebugName();
auto value = prop->getDebugValue();
auto children = prop->getDebugPropsDescription(level + 1);
auto children = prop->getDebugPropsDescription(options, depth + 1);
auto valueAndChildren = value + (children.empty() ? "" : "(" + children + ")");
propsString += " " + name + (valueAndChildren.empty() ? "" : "=" + valueAndChildren);
}
@@ -39,16 +47,19 @@ std::string DebugStringConvertible::getDebugPropsDescription(int level) const {
return propsString;
}
std::string DebugStringConvertible::getDebugDescription(int level) const {
std::string DebugStringConvertible::getDebugDescription(DebugStringConvertibleOptions options, int depth) const {
std::string nameString = getDebugName();
std::string valueString = getDebugValue();
std::string childrenString = getDebugChildrenDescription(level);
std::string propsString = getDebugPropsDescription(level);
std::string childrenString = getDebugChildrenDescription(options, depth + 1);
std::string propsString = getDebugPropsDescription(options, depth /* The first-level props are considered as same-depth things. */);
return "<" + nameString +
std::string leading = options.format ? std::string(depth, '\t') : "";
std::string trailing = options.format ? "\n" : "";
return leading + "<" + nameString +
(valueString.empty() ? "" : "=" + valueString) +
(propsString.empty() ? "" : " " + propsString) +
(childrenString.empty() ? "/>" : ">" + childrenString + "</" + nameString + ">");
(childrenString.empty() ? "/>" + trailing : ">" + trailing + childrenString + leading + "</" + nameString + ">" + trailing);
}
std::string DebugStringConvertible::getDebugName() const {