save the callstack information to file when exception throws test=dev… (#19324)

* save the callstack information to file when exception throws test=develop
assert
wopeizl 6 years ago committed by GitHub
parent 3f392fd4bc
commit b8aa37d529
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -26,6 +26,7 @@ limitations under the License. */
#include <thrust/system_error.h> #include <thrust/system_error.h>
#endif // PADDLE_WITH_CUDA #endif // PADDLE_WITH_CUDA
#include <fstream>
#include <iomanip> #include <iomanip>
#include <iostream> #include <iostream>
#include <memory> #include <memory>
@ -82,6 +83,22 @@ struct EnforceNotMet : public std::exception {
const char* what() const noexcept override { return err_str_.c_str(); } const char* what() const noexcept override { return err_str_.c_str(); }
private: private:
const std::string output_file_name{"paddle_err_info"};
void saveErrorInformation(const std::string& err) {
std::stringstream ss;
ss << output_file_name;
std::time_t t = std::time(nullptr);
std::tm* tm = std::localtime(&t);
char mbstr[100];
std::strftime(mbstr, sizeof(mbstr), "%F-%H-%M-%S", tm);
ss << "_" << mbstr << ".log";
std::ofstream err_file(ss.str(), std::ofstream::out);
if (err_file.is_open()) {
err_file << err;
err_file.close();
}
}
template <typename StrType> template <typename StrType>
inline void Init(StrType what, const char* f, int l) { inline void Init(StrType what, const char* f, int l) {
static constexpr int TRACE_STACK_LIMIT = 100; static constexpr int TRACE_STACK_LIMIT = 100;
@ -112,6 +129,8 @@ struct EnforceNotMet : public std::exception {
sout << "Windows not support stack backtrace yet."; sout << "Windows not support stack backtrace yet.";
#endif #endif
err_str_ = sout.str(); err_str_ = sout.str();
saveErrorInformation(err_str_);
} }
}; };

Loading…
Cancel
Save