Skip to content

Commit 897305d

Browse files
authored
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
1 parent c79acd9 commit 897305d

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

paddle/fluid/platform/enforce.h

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ limitations under the License. */
6161
#endif // __APPLE__
6262
#endif // PADDLE_WITH_CUDA
6363

64+
// Note: these headers for simplify demangle type string
65+
#include "paddle/fluid/framework/type_defs.h"
66+
#include "paddle/fluid/imperative/type_defs.h"
67+
6468
namespace paddle {
6569
namespace platform {
6670

@@ -189,6 +193,35 @@ struct BinaryCompareMessageConverter<false> {
189193
};
190194
} // namespace details
191195

196+
template <typename T>
197+
inline std::string ReplaceComplexTypeStr(std::string str,
198+
const std::string& type_name) {
199+
auto demangle_type_str = demangle(typeid(T).name());
200+
size_t start_pos = 0;
201+
while ((start_pos = str.find(demangle_type_str, start_pos)) !=
202+
std::string::npos) {
203+
str.replace(start_pos, demangle_type_str.length(), type_name);
204+
start_pos += type_name.length();
205+
}
206+
return str;
207+
}
208+
209+
#define __REPLACE_COMPLEX_TYPE_STR__(__TYPENAME, __STR) \
210+
do { \
211+
__STR = paddle::platform::ReplaceComplexTypeStr<__TYPENAME>(__STR, \
212+
#__TYPENAME); \
213+
} while (0)
214+
215+
inline std::string SimplifyDemangleStr(std::string str) {
216+
// the older is important, you have to put complex types in front
217+
__REPLACE_COMPLEX_TYPE_STR__(paddle::framework::AttributeMap, str);
218+
__REPLACE_COMPLEX_TYPE_STR__(paddle::framework::Attribute, str);
219+
__REPLACE_COMPLEX_TYPE_STR__(paddle::imperative::NameVariableWrapperMap, str);
220+
__REPLACE_COMPLEX_TYPE_STR__(paddle::imperative::NameVarBaseMap, str);
221+
__REPLACE_COMPLEX_TYPE_STR__(std::string, str);
222+
return str;
223+
}
224+
192225
template <typename StrType>
193226
inline std::string GetTraceBackString(StrType&& what, const char* file,
194227
int line) {
@@ -210,7 +243,8 @@ inline std::string GetTraceBackString(StrType&& what, const char* file,
210243
std::string path(info.dli_fname);
211244
// C++ traceback info are from core.so
212245
if (path.substr(path.length() - 3).compare(".so") == 0) {
213-
sout << string::Sprintf("%-3d %s\n", idx++, demangled);
246+
sout << string::Sprintf("%-3d %s\n", idx++,
247+
SimplifyDemangleStr(demangled));
214248
}
215249
}
216250
}

0 commit comments

Comments
 (0)