diff --git a/ReactCommon/fabric/debug/DebugStringConvertible.cpp b/ReactCommon/fabric/debug/DebugStringConvertible.cpp index 818629764..f669c1bd5 100644 --- a/ReactCommon/fabric/debug/DebugStringConvertible.cpp +++ b/ReactCommon/fabric/debug/DebugStringConvertible.cpp @@ -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 + ""); + (childrenString.empty() ? "/>" + trailing : ">" + trailing + childrenString + leading + "" + trailing); } std::string DebugStringConvertible::getDebugName() const { diff --git a/ReactCommon/fabric/debug/DebugStringConvertible.h b/ReactCommon/fabric/debug/DebugStringConvertible.h index b743a9332..b47d0c0d4 100644 --- a/ReactCommon/fabric/debug/DebugStringConvertible.h +++ b/ReactCommon/fabric/debug/DebugStringConvertible.h @@ -18,6 +18,11 @@ class DebugStringConvertible; using SharedDebugStringConvertible = std::shared_ptr; using SharedDebugStringConvertibleList = std::vector; +struct DebugStringConvertibleOptions { + bool format {true}; + int maximumDepth {INT_MAX}; +}; + // Abstract class describes conformance to DebugStringConvertible concept // and implements basic recursive debug string assembly algorithm. // Use this as a base class for providing a debugging textual representation @@ -47,12 +52,12 @@ public: // Returns a string which represents the object in a human-readable way. // Default implementation returns a description of the subtree // rooted at this node, represented in XML-like format. - virtual std::string getDebugDescription(int level = 0) const; + virtual std::string getDebugDescription(DebugStringConvertibleOptions options = {}, int depth = 0) const; // Do same as `getDebugDescription` but return only *children* and // *properties* parts (which are used in `getDebugDescription`). - virtual std::string getDebugPropsDescription(int level = 0) const; - virtual std::string getDebugChildrenDescription(int level = 0) const; + virtual std::string getDebugPropsDescription(DebugStringConvertibleOptions options = {}, int depth = 0) const; + virtual std::string getDebugChildrenDescription(DebugStringConvertibleOptions options = {}, int depth = 0) const; }; } // namespace react