未验证 提交 0aed0951 编写于 作者: C Chen Weihang 提交者: GitHub

The third time to simplify the C ++ error stack (#24831)

* simply C++ error stack once again, test=develop

* refactor code remove string pointer and recursive, test=develop
上级 704cad6a
...@@ -63,6 +63,10 @@ limitations under the License. */ ...@@ -63,6 +63,10 @@ limitations under the License. */
#endif // __APPLE__ #endif // __APPLE__
#endif // PADDLE_WITH_CUDA #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 paddle {
namespace platform { namespace platform {
...@@ -191,6 +195,35 @@ struct BinaryCompareMessageConverter<false> { ...@@ -191,6 +195,35 @@ struct BinaryCompareMessageConverter<false> {
}; };
} // namespace details } // namespace details
template <typename T>
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 <typename StrType> template <typename StrType>
inline std::string GetTraceBackString(StrType&& what, const char* file, inline std::string GetTraceBackString(StrType&& what, const char* file,
int line) { int line) {
...@@ -212,7 +245,8 @@ inline std::string GetTraceBackString(StrType&& what, const char* file, ...@@ -212,7 +245,8 @@ inline std::string GetTraceBackString(StrType&& what, const char* file,
std::string path(info.dli_fname); std::string path(info.dli_fname);
// C++ traceback info are from core.so // C++ traceback info are from core.so
if (path.substr(path.length() - 3).compare(".so") == 0) { 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));
} }
} }
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册