This commit is contained in:
aozhiwei 2021-09-18 02:45:04 +00:00
parent 9593a04aa3
commit 0535045139
2 changed files with 10 additions and 5 deletions

View File

@ -4,19 +4,17 @@
#ifdef DEBUG
#if __GNUC__ > 4
static void ErrorCallback(void *data, const char *msg, int errnum)
{
abort();
}
static int SimpleCallback(void *data, uintptr_t pc)
{
return 0;
}
#endif
void TraceMgr::Init(const std::string& filename)
{
filename_ = filename;
#if __GNUC__ > 4
state_ = backtrace_create_state(filename_.c_str(),
0,
ErrorCallback,
@ -24,6 +22,7 @@ void TraceMgr::Init(const std::string& filename)
if (!state_) {
abort();
}
#endif
log_file_ = fopen("backtrace.log", "w");
if (!log_file_) {
abort();
@ -40,10 +39,12 @@ void TraceMgr::UnInit()
void TraceMgr::Trace(const std::string& hint)
{
#if __GNUC__ > 4
fputs(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n", log_file_);
fputs(hint.c_str(), log_file_);
backtrace_print(state_, 0, log_file_);
fputs("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n", log_file_);
#endif
}
#endif

View File

@ -2,7 +2,9 @@
#ifdef DEBUG
#if __GNUC__ > 4
#include <backtrace.h>
#endif
class TraceMgr : public a8::Singleton<TraceMgr>
{
@ -20,7 +22,9 @@ class TraceMgr : public a8::Singleton<TraceMgr>
private:
std::string filename_;
#if __GNUC__ > 4
backtrace_state* state_ = nullptr;
#endif
FILE* log_file_ = nullptr;
};