Exception类是为异常捕获而设计,可以获得异常的信息以及栈的回溯信息

(原来的代码没有demangle成员函数,输出的格式比较难看,加了demangle成员函数,利用demangle成员函数可以转换格式,使得输出的格式更加接近我们的习惯)

以下的代码是加入了demangle成员函数后的:

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
{ 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 demangle(const char* symbol);//添加一个成员函数转换一下栈回溯信息输出的格式
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>
#include <stdio.h> using namespace muduo; Exception::Exception(const char* msg) : message_(msg)
{
fillStackTrace();//构造函数直接调用 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];//保存200个地址,是一个数组的指针
//栈回溯,保存各个栈帧的地址
//buffer中的每一个项都是void*,用于保存函数的地址
int nptrs = ::backtrace(buffer, len);//nptr为实际保存的个数
//根据地址,转成相应的函数符号
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_字符串
stack_.append(demangle(strings[i]));//转化后在存入
stack_.push_back('\n');
}
free(strings);//存放的地址使用malloc开辟出来的,需要我们自己释放
}
}
//该函数实现栈回溯信息的格式转换
string Exception::demangle(const char* symbol)
{
size_t size;
int status;
char temp[128];
char* demangled;
//first, try to demangle a c++ name
if (1 == sscanf(symbol, "%*[^(]%*[^_]%127[^)+]", temp)) {
if (NULL != (demangled = abi::__cxa_demangle(temp, NULL, &size, &status))) {
string result(demangled);
free(demangled);
return result;
}
}
//if that didn't work, try to get a regular c symbol
if (1 == sscanf(symbol, "%127s", temp)) {
return temp;
} //if all else fails, just return the symbol
return symbol;
}

以下是一个简单的测试函数:

//Exception类测试函数
#include <muduo/base/Exception.h>
#include <stdio.h>
//定义Bar类
class Bar
{
public:
void test()
{
throw muduo::Exception("oops");//抛出异常
}
};
void foo()
{
Bar b;
b.test();
}
int main()
{
try
{
foo();//可能发生异常
}
catch (const muduo::Exception& ex)//捕获异常
{
printf("reason: %s\n", ex.what());//抛出异常信息
printf("stack trace: %s\n", ex.stackTrace());//把异常栈回溯信息抛出,即在哪里抛出异常,打印函数的调用栈
}
}

单独编译后运行结果如下:(没有使用demangle成员函数)



使用demangle成员函数:

muduo网络库源码学习————Exception类的更多相关文章

  1. muduo网络库源码学习————线程类

    muduo库里面的线程类是使用基于对象的编程思想,源码目录为muduo/base,如下所示: 线程类头文件: // Use of this source code is governed by a B ...

  2. muduo网络库源码学习————日志类封装

    muduo库里面的日志使方法如下 这里定义了一个宏 #define LOG_INFO if (muduo::Logger::logLevel() <= muduo::Logger::INFO) ...

  3. muduo网络库源码学习————Timestamp.cc

    今天开始学习陈硕先生的muduo网络库,moduo网络库得到很多好评,陈硕先生自己也说核心代码不超过5000行,所以我觉得有必要拿过来好好学习下,学习的时候在源码上面添加一些自己的注释,方便日后理解, ...

  4. muduo网络库源码学习————线程池实现

    muduo库里面的线程池是固定线程池,即创建的线程池里面的线程个数是一定的,不是动态的.线程池里面一般要包含线程队列还有任务队列,外部程序将任务存放到线程池的任务队列中,线程池中的线程队列执行任务,也 ...

  5. muduo网络库源码学习————互斥锁

    muduo源码的互斥锁源码位于muduo/base,Mutex.h,进行了两个类的封装,在实际的使用中更常使用MutexLockGuard类,因为该类可以在析构函数中自动解锁,避免了某些情况忘记解锁. ...

  6. muduo网络库源码学习————线程本地单例类封装

    muduo库中线程本地单例类封装代码是ThreadLocalSingleton.h 如下所示: //线程本地单例类封装 // Use of this source code is governed b ...

  7. muduo网络库源码学习————日志滚动

    muduo库里面的实现日志滚动有两种条件,一种是日志文件大小达到预设值,另一种是时间到达超过当天.滚动日志类的文件是LogFile.cc ,LogFile.h 代码如下: LogFile.cc #in ...

  8. muduo网络库源码学习————线程特定数据

    muduo库线程特定数据源码文件为ThreadLocal.h //线程本地存储 // Use of this source code is governed by a BSD-style licens ...

  9. muduo网络库源码学习————无界队列和有界队列

    muduo库里实现了两个队列模板类:无界队列为BlockingQueue.h,有界队列为BoundedBlockingQueue.h,两个测试程序实现了生产者和消费者模型.(这里以无界队列为例,有界队 ...

随机推荐

  1. JAVA集合框架之List和Set、泛型

    一 List是有序可重复的集合 可以进行增删改查,直接看代码 package com.collection; import java.util.ArrayList; import java.util. ...

  2. 中阶 d03.5 (正篇)完整的Dao 操作数据库

    1.目录结构: util---JDBCUtil.java(工具包,整合建立链接和释放资源的方法) dao---UserDao.java(接口,定义方法) impl---UserDaoImpl.java ...

  3. Python设计模式(2)-策略模式

    # 策略模式和简单工厂模式相比,少了使用switch case 做判断,然后去实例化相应的 # 对象,比简单工厂模式更灵活. 它们代码的区别就在于此处使用了抽象类代替工厂类 # coding=utf- ...

  4. Linux C++ 网络编程学习系列(3)——多路IO之poll实现

    poll实现多路IO 源码地址:https://github.com/whuwzp/linuxc/tree/master/poll 源码说明: server.cpp: 监听127.1:6666,功能是 ...

  5. CKEDITOR (FCKEDITOR) --- 目前最优秀的可见即可得网页编辑器之一

    FCKEDITOR 编辑 同义词 CKEditor一般指FCKEDITOR FCKeditor是目前最优秀的可见即可得网页编辑器之一,它采用JavaScript编写.具备功能强大.配置容易.跨浏览器. ...

  6. .NET Core技术研究-主机

    前一段时间,和大家分享了 ASP.NET Core技术研究-探秘Host主机启动过程 但是没有深入说明主机的设计.今天整理了一下主机的一些知识,结合先前的博文,完整地介绍一下.NET Core的主机的 ...

  7. 【一统江湖的大前端(9)】TensorFlow.js 开箱即用的深度学习工具

    示例代码托管在:http://www.github.com/dashnowords/blogs 博客园地址:<大史住在大前端>原创博文目录 目录 一. 上手TensorFlow.js 二. ...

  8. AJ学IOS(47)之网易彩票帮助界面UIWebView的运用

    AJ分享,必须精品 效果: 制作过程 首先是帮助按钮那个地方的点击. 这里是用点击跳转的用的是 NJSettingArrowItem,前面的设置的,从字典通过模型转过来的. // 分享 NJSetti ...

  9. 数据结构(C语言版)---线性表链式存储表示

    1.单链表:线性表的链式存储. 1)特点:用一组任意的存储单元存储数据元素(存储单元可以连续,也可以不连续),逻辑上相邻的元素存储位置不一定相邻. 2)结点包括两个域:数据域(存储数据元素信息).指针 ...

  10. C++关于容器vector的使用方法以及#ifdef #else #endif #if #ifndef 的使用

    //此处根据0还是1来判断具体使用那一段主函数 #if 1 #define WAY #endif #ifdef WAY #include <iostream> #include<st ...