Repository navigation
Expand file tree
/
Copy pathCException.cpp
More file actions
executable file
·123 lines (97 loc) · 2.72 KB
/
Copy pathCException.cpp
File metadata and controls
executable file
·123 lines (97 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/**
* @file util.cpp
* @author dongjiang(@360.cn)
* @date 2014/08/19 14:24:52
* @brief
*
**/
#include <execinfo.h>
#include <stdlib.h> // use backtrace_symbols backtrace
#include <cxxabi.h> // 使用 abi::__cxa_demangle
#include <iostream> // 使用 cont
#include <sstream> //使用 stringstream
#include "CException.h"
using namespace std;
ExceptionBase::ExceptionBase(const string& msg) throw()
: mMsg(msg),
mFile("<unknown file>"),
mFunc("<unknown func>"),
mLine(-1),
mStackTraceSize(0) {
}
ExceptionBase::~ExceptionBase() throw() {
}
void ExceptionBase::init(const char* file, const char* func, int line) {
mFile = file;
mFunc = func;
mLine = line;
mStackTraceSize = backtrace(mStackTrace, MAX_STACK_TRACE_SIZE);
}
string ExceptionBase::GetClassName() const {
return "ExceptionBase";
}
const char* ExceptionBase::what() const throw() {
return ToString().c_str();
}
const string& ExceptionBase::ToString() const {
if (mWhat.empty())
{
stringstream sstr("");
if (mLine > 0)
{
sstr << mFile << "(" << mLine << ")";
}
sstr << ": " << GetClassName();
if (!GetMessage().empty())
{
sstr << ": " << GetMessage();
}
sstr << "\nStack Trace:\n";
sstr << GetStackTrace();
mWhat = sstr.str();
}
return mWhat;
}
string ExceptionBase::GetMessage() const {
return mMsg;
}
string ExceptionBase::GetStackTrace() const {
if ( mStackTraceSize == 0 )
return "<no stack Trace>\n";
char** strings = backtrace_symbols(mStackTrace, 10);
if (strings == NULL)
return "<Unknown error: backtrace_symbols return NULL>\n";
string result;
for (size_t i = 0; i < mStackTraceSize; ++i)
{
string mangledName = strings[i];
string::size_type begin = mangledName.find('(');
string::size_type end = mangledName.find('+', begin);
if (begin == string::npos || end == string::npos )
{
result += mangledName;
result += '\n';
continue;
}
++begin;
int status;
char* s = abi::__cxa_demangle(mangledName.substr(begin, end-begin).c_str(),
NULL, 0, &status);
if (status != 0)
{
result += mangledName;
result += '\n';
continue;
}
string demangledName(s);
free(s);
// 增加 #0 的首栈信息
result += mangledName.substr(0, begin);
result += demangledName;
result += mangledName.substr(end);
result += '\n';
}
free(strings);
return result;
}
/* vim: set expandtab ts=4 sw=4 sts=4 tw=100: */