muduo库源码学习(base)Exception

来源:互联网 发布:阳光优化整合 编辑:程序博客网 时间:2024/06/06 00:49
class Exception : public std::exception{ public:  explicit Exception(const char* what);  explicit Exception(const string& what);  virtual ~Exception() throw();  virtual const char* what() const throw();  const char* stackTrace() const throw(); private:  void fillStackTrace();  string message_;  string stack_;};


Exception::Exception(const char* msg)  : message_(msg){  fillStackTrace();}Exception::Exception(const string& msg)  : message_(msg){  fillStackTrace();}Exception::~Exception() throw (){}const char* Exception::what() const throw(){  return message_.c_str();}const char* Exception::stackTrace() const throw(){  return stack_.c_str();}void Exception::fillStackTrace(){  const int len = 200;  void* buffer[len];  //该函数用于获取当前线程的调用堆栈,获取的信息将会被存放在buffer中,它是一个指针数组  int nptrs = ::backtrace(buffer, len);//猜测是得到trace的数量,每个trace存到buffer[i]里,即从[0]到[nptrs-1]  char** strings = ::backtrace_symbols(buffer, nptrs);//直接返回这个buffer内的可打印信息  if (strings)  {    for (int i = 0; i < nptrs; ++i)    {      // TODO demangle funcion name with abi::__cxa_demangle      stack_.append(strings[i]);      stack_.push_back('\n');    }    free(strings);//直接free即可!也就是说malloc的是一个一维数组,只是逻辑成二维;  }}