// // Created by zhaohe on 19-5-21. // #include "zexception.hpp" #if ENABLE_BACK_WARD #include "../utils/backward/backward.hpp" #endif #include #include "spdlog/spdlog.h" #include #include // #include "zwtimecpp/core/base/interlog/simple_logger.hpp" // #include "zwtimecpp/core/exception_handle_center.hpp" // #include "zwtimecpp/core/logger/logger.hpp" // #include "zwtimecpp/core/system_state.hpp" using namespace std; using namespace iflytopcpp; using namespace core; #if ENABLE_BACK_WARD using namespace backward; #endif static shared_ptr s_theLastedBaseException; void zexception::setLastException(shared_ptr e) { s_theLastedBaseException = e; } shared_ptr zexception::getUnHandledException() { if (s_theLastedBaseException && !s_theLastedBaseException->isHasCalledToString()) { return s_theLastedBaseException; } if (s_theLastedBaseException) { return s_theLastedBaseException; } return nullptr; } zexception::zexception(string description, string stdExceptionTypeinfo, string baseExceptionWhat) { initialize(description, stdExceptionTypeinfo, baseExceptionWhat); }; void zexception::initialize(string description, string stdExceptionTypeinfo, string baseExceptionWhat) { #if ENABLE_BACK_WARD StackTrace st; st.load_here(32); Printer p; p.object = true; p.snippet = true; p.color_mode = ColorMode::always; p.address = true; ostringstream out; p.print(st, out); #endif this->description = description; #if ENABLE_BACK_WARD this->stackInfo = out.str(); #else this->stackInfo = "not enable back ward"; #endif this->pthreadId = pthread_self(); this->stdExceptionWhat = baseExceptionWhat; this->stdExceptionTypeinfo = stdExceptionTypeinfo; // setLastException(shared_from_this()); } zexception::zexception(string description, const std::exception &stdexcep) { initialize(description, typeid(stdexcep).name(), stdexcep.what()); // 这里不能写成 // zexception(description,typeid(stdexcep).name(),stdexcep.what()); // 不知道为什么对象的成员变量会被清空 } string zexception::format1024(const char *fmt, ...) { char buf[1024] = {0}; va_list args; va_start(args, fmt); vsnprintf(buf, 1024, fmt, args); va_end(args); return string(buf); } string zexception::toString() const { string ret; ret += "\n#-----------------------!!!!!CatchException!!!!!----------------------" "-------\n"; ret += "# exception type: " + string(typeid(*this).name()) + "\n"; ret += "# description: " + this->description + "\n"; if (!this->stdExceptionTypeinfo.empty()) ret += "# stdExceptionTypeinfo: " + this->stdExceptionTypeinfo + "\n"; if (!this->stdExceptionWhat.empty()) ret += "# stdExceptionWhat: " + this->stdExceptionWhat + "\n"; string threadInfo; // ret += "#pthreadId: " + CoreSystemState::Instance().get //(this->pthreadId) + "\n"; ret += "# loseInfo: " + to_string(this->loseInfo) + "\n"; ret += "# stackTrace: \n" + this->stackInfo; hasCalledToString = true; return ret; } zexception::~zexception() { if (!hasCalledToString) { std::cerr << "#Show exception message, because not call " "exception->toString before it destroy"; std::cerr << toString(); } } bool zexception::isHasCalledToString() const { return hasCalledToString; } const char *zexception::what() const _GLIBCXX_USE_NOEXCEPT { // return toString().c_str(); // 之所以在这里打印log,因为在arm上由系统自动打印的,what()可能会出现乱码. spdlog::critical("Catch exception \n{}", toString()); spdlog::default_logger()->flush(); return "end"; }