Skip to content

Commit 4841474

Browse files
authored
Make formatted ENFORCE stack adapt to more situations (#20826) (#20828)
1 parent 03a8945 commit 4841474

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

paddle/fluid/framework/op_call_stack.cc

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,15 @@ void InsertCallStackInfo(const std::string &type, const AttributeMap &attrs,
4646
}
4747
}
4848
// Step 2. Insert python traceback into err_str_
49-
std::size_t found = exception->err_str_.rfind("PaddleCheckError:");
49+
std::size_t found = exception->err_str_.rfind(
50+
"\n----------------------\nError Message "
51+
"Summary:\n----------------------\n");
5052
if (found != std::string::npos) {
5153
exception->err_str_.insert(found, sout_py_trace.str());
52-
exception->err_str_.insert(found + sout_py_trace.str().length(),
53-
"\n----------------------\nError Message "
54-
"Summary:\n----------------------\n");
5554
} else {
5655
exception->err_str_.append(sout_py_trace.str());
5756
}
58-
// Step 3. Construct final call stack
59-
sout << "\n\n--------------------------------------------\n";
60-
sout << "C++ Call Stacks (More useful to developers):";
61-
sout << "\n--------------------------------------------\n";
57+
// Step 3. Construct final call stack & append error op name
6258
sout << exception->err_str_;
6359
if (callstack) {
6460
sout << " [operator < " << type << " > error]";

paddle/fluid/platform/enforce.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ inline std::string GetTraceBackString(StrType&& what, const char* file,
7474
static constexpr int TRACE_STACK_LIMIT = 100;
7575
std::ostringstream sout;
7676

77+
sout << "\n\n--------------------------------------------\n";
78+
sout << "C++ Call Stacks (More useful to developers):";
79+
sout << "\n--------------------------------------------\n";
7780
#if !defined(_WIN32)
7881
void* call_stack[TRACE_STACK_LIMIT];
7982
auto size = backtrace(call_stack, TRACE_STACK_LIMIT);
@@ -102,8 +105,10 @@ inline std::string GetTraceBackString(StrType&& what, const char* file,
102105
}
103106
free(symbols);
104107
#else
105-
sout << "Windows not support stack backtrace yet.";
108+
sout << "Windows not support stack backtrace yet.\n";
106109
#endif
110+
sout << "\n----------------------\nError Message "
111+
"Summary:\n----------------------\n";
107112
sout << string::Sprintf("PaddleCheckError: %s at [%s:%d]",
108113
std::forward<StrType>(what), file, line)
109114
<< std::endl;

0 commit comments

Comments
 (0)