項目地址:https://github.com/wlgq2/orcaios
orca中定義一套錯誤號碼及提示信息以及斷言,當程序發生錯誤時候,會輸出錯誤信息或者終止程序。
在orca/base/error/ErrorInfo.h中能夠到錯誤號碼的定義以下:git
namespace orca { namespace base { class ErrorInfo { public: enum ErrorId { UVWriteFail = -2048, UVConnectFail, UVDisconnectFromServer, UVSigPipe, UndefinedError = -1024, NoFindActorName, ActorNameTooLong, NoFindActorAddr, ReDefineActorName, MessagePackNull, PackMessageError, NoFindRemoteFramework, RepeatedRemoteFrameworkID, }; ErrorInfo(ErrorId id,std::string info); ErrorId getErrorId(); std::string& getErrorInfo(); private: ErrorId id_; std::string info_; }; } }
orca的錯誤處理函數以下:github
void error(ErrorInfo info) { if (handle_) handle_(info); else std::cerr << "error id "<< info.getErrorId() << ":" << info.getErrorInfo() << std::endl; }
若是用戶沒有自定義錯誤處理回調,orca只會cerr錯誤,不然會運行用戶註冊錯誤回調函數。
orca中經過RegisterErrorHandle接口來註冊錯誤回調函數。
一個完整的例子:函數
#include <iostream> #include <orca/orca.h> REGISTER_MESSAGE_TYPE(std::string); void errorHandle(orca::base::ErrorInfo info) { std::cout << "error id : " << info.getErrorId() << std::endl; std::cout << "error message: " << info.getErrorInfo() << std::endl; } int main(int argc, char** args) { //actor framework. orca::Framework framework; framework.RegisterErrorHandle(std::bind(&errorHandle,std::placeholders::_1)); orca::base::ErrorHandle::Instance()->error(orca::base::ErrorInfo::UndefinedError,"undefine error"); framework.loop(); }
此外,orca簡單的封裝了斷言用於打印信息並終止錯誤程序。在orca/core/Assert.h文件中,代碼以下:oop
class Assert { public: static void IsFail(bool nomal, const char* const file, const unsigned int line, const std::string message = "") { if (!nomal) { Fail(file,line,message); } } static void Fail(const char* const file, const unsigned int line, const std::string message = "") { std::cerr<<"fail in :"<<file<<" at "<<line<<" lines."; if ("" != message) { std::cerr<<"\n:" << message; } std::cerr << "\n"; assert(false); } };
經過相關宏定義使用,能夠輸出異常在代碼中的發生位置:spa
#define ORCA_FAIL() orca::Assert::Fail(__FILE__, __LINE__) #define ORCA_FAIL_MSG(msg) orca::Assert::Fail(__FILE__, __LINE__, msg) #define ORCA_ASSERT(condition) orca::Assert::IsFail(condition,__FILE__, __LINE__) #define ORCA_ASSERT_MSG(condition, msg) orca::Assert::IsFail(condition,__FILE__, __LINE__,msg)