You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
3.6 KiB

  1. //
  2. // Created by zhaohe on 19-5-21.
  3. //
  4. #include "zexception.hpp"
  5. #if ENABLE_BACK_WARD
  6. #include "../utils/backward/backward.hpp"
  7. #endif
  8. #include <stdarg.h>
  9. #include "spdlog/spdlog.h"
  10. #include <iostream>
  11. #include <thread>
  12. // #include "zwtimecpp/core/base/interlog/simple_logger.hpp"
  13. // #include "zwtimecpp/core/exception_handle_center.hpp"
  14. // #include "zwtimecpp/core/logger/logger.hpp"
  15. // #include "zwtimecpp/core/system_state.hpp"
  16. using namespace std;
  17. using namespace iflytop;
  18. using namespace core;
  19. #if ENABLE_BACK_WARD
  20. using namespace backward;
  21. #endif
  22. static shared_ptr<zexception> s_theLastedBaseException;
  23. void zexception::setLastException(shared_ptr<zexception> e) { s_theLastedBaseException = e; }
  24. shared_ptr<zexception> zexception::getUnHandledException() {
  25. if (s_theLastedBaseException && !s_theLastedBaseException->isHasCalledToString()) {
  26. return s_theLastedBaseException;
  27. }
  28. if (s_theLastedBaseException) {
  29. return s_theLastedBaseException;
  30. }
  31. return nullptr;
  32. }
  33. zexception::zexception(string description, string stdExceptionTypeinfo, string baseExceptionWhat) {
  34. initialize(description, stdExceptionTypeinfo, baseExceptionWhat);
  35. };
  36. void zexception::initialize(string description, string stdExceptionTypeinfo, string baseExceptionWhat) {
  37. #if ENABLE_BACK_WARD
  38. StackTrace st;
  39. st.load_here(32);
  40. Printer p;
  41. p.object = true;
  42. p.snippet = true;
  43. p.color_mode = ColorMode::always;
  44. p.address = true;
  45. ostringstream out;
  46. p.print(st, out);
  47. #endif
  48. this->description = description;
  49. #if ENABLE_BACK_WARD
  50. this->stackInfo = out.str();
  51. #else
  52. this->stackInfo = "not enable back ward";
  53. #endif
  54. this->pthreadId = pthread_self();
  55. this->stdExceptionWhat = baseExceptionWhat;
  56. this->stdExceptionTypeinfo = stdExceptionTypeinfo;
  57. // setLastException(shared_from_this());
  58. }
  59. zexception::zexception(string description, const std::exception &stdexcep) {
  60. initialize(description, typeid(stdexcep).name(), stdexcep.what());
  61. // 这里不能写成
  62. // zexception(description,typeid(stdexcep).name(),stdexcep.what());
  63. // 不知道为什么对象的成员变量会被清空
  64. }
  65. string zexception::format1024(const char *fmt, ...) {
  66. char buf[1024] = {0};
  67. va_list args;
  68. va_start(args, fmt);
  69. vsnprintf(buf, 1024, fmt, args);
  70. va_end(args);
  71. return string(buf);
  72. }
  73. string zexception::toString() const {
  74. string ret;
  75. ret +=
  76. "\n#-----------------------!!!!!CatchException!!!!!----------------------"
  77. "-------\n";
  78. ret += "# exception type: " + string(typeid(*this).name()) + "\n";
  79. ret += "# description: " + this->description + "\n";
  80. if (!this->stdExceptionTypeinfo.empty()) ret += "# stdExceptionTypeinfo: " + this->stdExceptionTypeinfo + "\n";
  81. if (!this->stdExceptionWhat.empty()) ret += "# stdExceptionWhat: " + this->stdExceptionWhat + "\n";
  82. string threadInfo;
  83. // ret += "#pthreadId: " + CoreSystemState::Instance().get
  84. //(this->pthreadId) + "\n";
  85. ret += "# loseInfo: " + to_string(this->loseInfo) + "\n";
  86. ret += "# stackTrace: \n" + this->stackInfo;
  87. hasCalledToString = true;
  88. return ret;
  89. }
  90. zexception::~zexception() {
  91. if (!hasCalledToString) {
  92. std::cerr << "#Show exception message, because not call "
  93. "exception->toString before it destroy";
  94. std::cerr << toString();
  95. }
  96. }
  97. bool zexception::isHasCalledToString() const { return hasCalledToString; }
  98. const char *zexception::what() const _GLIBCXX_USE_NOEXCEPT {
  99. // return toString().c_str();
  100. // 之所以在这里打印log,因为在arm上由系统自动打印的,what()可能会出现乱码.
  101. spdlog::critical("Catch exception \n{}", toString());
  102. spdlog::default_logger()->flush();
  103. return "end";
  104. }