muduo網路庫Exception異常類
1 Exception類
Exception類包括兩個檔案 :Exception.cc, Exception.h。
1.1 Exception.h
#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 message_;
string stack_;
};
}
1.2 Exception.cc
#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);
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);
}
}
2 backtrace函式
int backtrace(void **buffer, int size); 棧回溯,儲存各個棧幀的地址;
buffer這個指標指向了一個陣列,陣列中的每一項都是void *型別,用來儲存函式的地址。
3 backtrace_symbols函式
char **backtrace_symbols(void const buffer, int size);根據地址,轉成相應的函式符號;
char strings = ::backtrace_symbols(buffer, nptrs);返回的是指標的指標;
4 還原C++函式的真實的名稱
需要的標頭檔案是<cxxabi.h>和<stdio.h>
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;
}
修改fillStackTrace()
void Exception::fillStackTrace()
{
const int len = 200;
void* buffer[len];
int nptrs = ::backtrace(buffer, len);
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(demangle(strings[i]));
stack_.push_back('\n');
}
free(strings);
}
}
相關文章
- PHP 異常類 ExceptionPHPException
- muduo網路庫Timestamp類
- muduo網路庫學習之muduo_http 庫涉及到的類HTTP
- muduo網路庫AtomicIntegerT原子整數類
- api模式下修改異常類ExceptionAPI模式Exception
- muduo網路庫學習之muduo_inspect 庫涉及到的類
- 異常(Exception)Exception
- Sanic Exception – 異常Exception
- muduo網路庫學習筆記(1):Timestamp類筆記
- muduo網路庫學習筆記(3):Thread類筆記thread
- muduo網路庫學習之Exception類、Thread 類封裝中的知識點(重點講pthread_atfork())Exceptionthread封裝
- muduo網路庫學習之EventLoop(四):EventLoopThread 類、EventLoopThreadPool 類OOPthread
- muduo網路庫使用心得
- oracle 使用異常exceptionOracleException
- muduo網路庫編譯安裝編譯
- PL/SQL 08 異常 exceptionSQLException
- java基礎:Exception異常JavaException
- oracle 異常處理 exceptionOracleException
- muduo網路庫學習筆記(12):TcpServer和TcpConnection類筆記TCPServer
- 淺談java異常[Exception] (轉)JavaException
- muduo網路庫學習筆記(8):高效日誌類的封裝筆記封裝
- muduo網路庫學習之EventLoop(一):事件迴圈類圖簡介和muduo 定時器TimeQueueOOP事件定時器
- muduo網路庫學習之ThreadLocal 類、ThreadLocalSingleton類封裝知識點thread封裝
- Java中Error和Exception的異同以及執行時異常(Runtime exception)與檢查型異常(checked exception)的區別JavaErrorException
- muduo網路庫學習之MutexLock類、MutexLockGuard類、Condition類、CountDownLatch類封裝中的知識點MutexCountDownLatch封裝
- muduo網路庫學習筆記(6):單例類(執行緒安全的)筆記單例執行緒
- muduo網路庫學習之Timestamp類、AtomicIntegerT 類封裝中的知識點封裝
- muduo網路庫學習之BlockinngQueue類、ThreadPool 類、Singleton類封裝中的知識點BloCthread封裝
- PHP基礎:異常處理ExceptionPHPException
- 甩鍋(throws)_ java異常(Exception)處理JavaException
- java異常——RuntimeException和User Define ExceptionJavaException
- muduo網路庫學習之Logger類、LogStream類、LogFile類封裝中的知識點封裝
- 網路異常測試初探
- muduo網路庫學習筆記(2):原子性操作筆記
- muduo網路庫學習之EventLoop(七):TcpClient、ConnectorOOPTCPclient
- 異常分類
- 小心異常(Exception)帶來的風險(2) (轉)Exception
- NETCORE - 全域性異常處理(Exception)NetCoreException