debug.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #pragma once
  17. #include <exception>
  18. #include <cassert>
  19. #include "gettime.h"
  20. #include "log.h"
  21. #ifdef _MSC_VER
  22. #define FUNCTION_NAME __FUNCTION__
  23. #else
  24. #define FUNCTION_NAME __PRETTY_FUNCTION__
  25. #endif
  26. // Whether to catch all std::exceptions.
  27. // When "catching", the program will abort with an error message.
  28. // In debug mode, leave these for the debugger and don't catch them.
  29. #ifdef NDEBUG
  30. #define CATCH_UNHANDLED_EXCEPTIONS 1
  31. #else
  32. #define CATCH_UNHANDLED_EXCEPTIONS 0
  33. #endif
  34. /* Abort program execution immediately
  35. */
  36. [[noreturn]] extern void fatal_error_fn(
  37. const char *msg, const char *file,
  38. unsigned int line, const char *function);
  39. #define FATAL_ERROR(msg) \
  40. fatal_error_fn((msg), __FILE__, __LINE__, FUNCTION_NAME)
  41. #define FATAL_ERROR_IF(expr, msg) \
  42. ((expr) \
  43. ? fatal_error_fn((msg), __FILE__, __LINE__, FUNCTION_NAME) \
  44. : (void)(0))
  45. /*
  46. sanity_check()
  47. Equivalent to assert() but persists in Release builds (i.e. when NDEBUG is
  48. defined)
  49. */
  50. [[noreturn]] extern void sanity_check_fn(
  51. const char *assertion, const char *file,
  52. unsigned int line, const char *function);
  53. #define SANITY_CHECK(expr) \
  54. ((expr) \
  55. ? (void)(0) \
  56. : sanity_check_fn(#expr, __FILE__, __LINE__, FUNCTION_NAME))
  57. #define sanity_check(expr) SANITY_CHECK(expr)
  58. std::string debug_describe_exc(const std::exception &e);
  59. void debug_set_exception_handler();
  60. /*
  61. These should be put into every thread
  62. */
  63. #if CATCH_UNHANDLED_EXCEPTIONS == 1
  64. #define BEGIN_DEBUG_EXCEPTION_HANDLER try {
  65. #define END_DEBUG_EXCEPTION_HANDLER \
  66. } catch (std::exception &e) { \
  67. std::string e_descr = debug_describe_exc(e); \
  68. errorstream << "An unhandled exception occurred: " \
  69. << e_descr << std::endl; \
  70. FATAL_ERROR(e_descr.c_str()); \
  71. }
  72. #else
  73. // Dummy ones
  74. #define BEGIN_DEBUG_EXCEPTION_HANDLER
  75. #define END_DEBUG_EXCEPTION_HANDLER
  76. #endif