[Muduo网络库源码分析] (4) base/Exception_cc_h_带 stack trace 的异常基类

带 stack trace 的异常基类

实现:Exception


功能:实现对boost异常类的封装,可以有效存储异常


知识点:

  • int backtrace(void **buffer, int size);
    该函数用与获取当前线程的调用堆栈,获取的信息将会被存放在buffer中,它是一个指针数组。参数 size 用来指定buffer中可以保存多少个void* 元素。函数返回值是实际获取的指针个数,最大不超过size大小在buffer中的指针实际是从堆栈中获取的返回地址,每一个堆栈框架有一个返回地址。
  • char **backtrace_symbols(void *const *buffer, int size);
    backtrace_symbols将从backtrace函数获取的信息转化为一个字符串数组. 参数buffer应该是从backtrace函数获取的数组指针,size是该数组中的元素个数(backtrace的返回值),函数返回值是一个指向字符串数组的指针,它的大小同buffer相同.每个字符串包含了一个相对于buffer中对应元素的可打印信息.它包括函数名,函数的偏移地址,和实际的返回地址
  • void backtrace_symbols_fd(void *const *buffer, int size, int fd);
    backtrace_symbols_fd与backtrace_symbols 函数具有相同的功能,不同的是它不会给调用者返回字符串数组,而是将结果写入文件描述符为fd的文件中,每个函数对应一行.它不需要调用malloc函数,因此适用于有可能调用该函数会失败的情况。

  • Boost::exception类使用


用途:

用于异常的抛出和记录

代码及分析:

Exception.h
// Use of this source code is governed by a BSD-style license
// that can be found in the License file.
//
// Author: Shuo Chen (chenshuo at chenshuo dot com)

#ifndef MUDUO_BASE_EXCEPTION_H
#define MUDUO_BASE_EXCEPTION_H

#include <muduo/base/Types.h>
#include <exception>

namespace muduo
{

//重载boost库中的exception基类
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_;//用来存储堆栈信息
};

}

#endif  // MUDUO_BASE_EXCEPTION_H


Exception.cc
// Use of this source code is governed by a BSD-style license // that can be found in the License file.
//
// Author: Shuo Chen (chenshuo at chenshuo dot com)

#include <muduo/base/Exception.h>

//#include <cxxabi.h>
#include <execinfo.h>
#include <stdlib.h>

using namespace muduo;
//构造函数
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];
  //该函数用与获取当前线程的调用堆栈
  int nptrs = ::backtrace(buffer, len);
  //backtrace_symbols将从backtrace函数获取的信息转化为一个字符串数组
  char** strings = ::backtrace_symbols(buffer, nptrs);
  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);
  }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值