Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 

125 lignes
3.1 KiB

  1. #pragma once
  2. #include "exception.h"
  3. namespace cppcore
  4. {
  5. /* exception */
  6. const exception::print_flags& exception::default_print_flag()
  7. {
  8. static const print_flags value({
  9. print_flag::print_trace,
  10. print_flag::resolve_address,
  11. });
  12. return value;
  13. }
  14. exception::exception()
  15. : exception("")
  16. { }
  17. exception::exception(std::string msg)
  18. : message (msg)
  19. , stack_size (0)
  20. , _msg_cache_empty (true)
  21. {
  22. stack_size = backtrace(&stack[0], max_stack_size);
  23. }
  24. void exception::print(std::ostream& os, const print_flags& flags) const
  25. {
  26. print_message(os);
  27. if (!flags.is_set(print_flag::print_trace))
  28. return;
  29. os << std::endl;
  30. char** lines = nullptr;
  31. if (flags.is_set(print_flag::resolve_address))
  32. lines = backtrace_symbols(stack, stack_size);
  33. for (int i = 0; i < stack_size; ++i)
  34. {
  35. os << " [0x" << std::setw(2 * sizeof(void*)) << std::setfill('0') << std::hex << reinterpret_cast<uintptr_t>(stack[i]) << "]";
  36. if (lines && lines[i])
  37. os << " " << lines[i];
  38. os << std::endl;
  39. }
  40. }
  41. std::string exception::print(const print_flags& flags) const
  42. {
  43. std::ostringstream os;
  44. print(os, flags);
  45. return os.str();
  46. }
  47. const std::string& exception::to_string() const
  48. {
  49. if (_msg_cache_empty)
  50. {
  51. _msg_cache = print();
  52. _msg_cache_empty = false;
  53. }
  54. return _msg_cache;
  55. }
  56. const char* exception::what() const throw()
  57. { return to_string().c_str(); }
  58. void exception::print_message(std::ostream& os) const
  59. {
  60. os << message;
  61. }
  62. std::ostream& operator <<(std::ostream& os, const exception& ex)
  63. {
  64. ex.print(os);
  65. return os;
  66. }
  67. /* error_exception */
  68. error_exception::error_exception(int e)
  69. : exception (std::to_string(e) + " - " + strerror(e))
  70. , error (e)
  71. { }
  72. error_exception::error_exception(const std::string& msg, int e) :
  73. exception(msg + ": " + std::to_string(e) + " - " + strerror(e)),
  74. error(e)
  75. { }
  76. /* range_exception */
  77. range_exception::range_exception(size_t mi, size_t ma, size_t idx, std::string msg)
  78. : exception (msg)
  79. , min (mi)
  80. , max (ma)
  81. , index (idx)
  82. { }
  83. void range_exception::print_message(std::ostream& os) const
  84. {
  85. os << "index out of range (min=" << min << "; max=" << max << "; index=" << index << ")";
  86. if (!message.empty())
  87. os << " - " << message;
  88. }
  89. /* argument_exception */
  90. argument_exception::argument_exception(std::string arg, std::string msg)
  91. : exception (msg)
  92. , argument (arg)
  93. { }
  94. void argument_exception::print_message(std::ostream& os) const
  95. {
  96. os << "invalid argument";
  97. if (!argument.empty())
  98. os << "(" << argument << ")";
  99. if (!message.empty())
  100. os << " - " << message;
  101. }
  102. }