From 897305d108533a4d2dd671d3716eb460a651fc7a Mon Sep 17 00:00:00 2001 From: Chen Weihang Date: Thu, 6 Aug 2020 05:10:22 +0800 Subject: [PATCH] The third time to simplify the C ++ error stack (#24831) (#25981) * simply C++ error stack once again, test=develop * refactor code remove string pointer and recursive, test=develop --- paddle/fluid/platform/enforce.h | 36 ++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/paddle/fluid/platform/enforce.h b/paddle/fluid/platform/enforce.h index f2e0c52170b..75fab4671b7 100644 --- a/paddle/fluid/platform/enforce.h +++ b/paddle/fluid/platform/enforce.h @@ -61,6 +61,10 @@ limitations under the License. */ #endif // __APPLE__ #endif // PADDLE_WITH_CUDA +// Note: these headers for simplify demangle type string +#include "paddle/fluid/framework/type_defs.h" +#include "paddle/fluid/imperative/type_defs.h" + namespace paddle { namespace platform { @@ -189,6 +193,35 @@ struct BinaryCompareMessageConverter { }; } // namespace details +template +inline std::string ReplaceComplexTypeStr(std::string str, + const std::string& type_name) { + auto demangle_type_str = demangle(typeid(T).name()); + size_t start_pos = 0; + while ((start_pos = str.find(demangle_type_str, start_pos)) != + std::string::npos) { + str.replace(start_pos, demangle_type_str.length(), type_name); + start_pos += type_name.length(); + } + return str; +} + +#define __REPLACE_COMPLEX_TYPE_STR__(__TYPENAME, __STR) \ + do { \ + __STR = paddle::platform::ReplaceComplexTypeStr<__TYPENAME>(__STR, \ + #__TYPENAME); \ + } while (0) + +inline std::string SimplifyDemangleStr(std::string str) { + // the older is important, you have to put complex types in front + __REPLACE_COMPLEX_TYPE_STR__(paddle::framework::AttributeMap, str); + __REPLACE_COMPLEX_TYPE_STR__(paddle::framework::Attribute, str); + __REPLACE_COMPLEX_TYPE_STR__(paddle::imperative::NameVariableWrapperMap, str); + __REPLACE_COMPLEX_TYPE_STR__(paddle::imperative::NameVarBaseMap, str); + __REPLACE_COMPLEX_TYPE_STR__(std::string, str); + return str; +} + template inline std::string GetTraceBackString(StrType&& what, const char* file, int line) { @@ -210,7 +243,8 @@ inline std::string GetTraceBackString(StrType&& what, const char* file, std::string path(info.dli_fname); // C++ traceback info are from core.so if (path.substr(path.length() - 3).compare(".so") == 0) { - sout << string::Sprintf("%-3d %s\n", idx++, demangled); + sout << string::Sprintf("%-3d %s\n", idx++, + SimplifyDemangleStr(demangled)); } } } -- GitLab