assert.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #include <pikaConfig.h>
  2. #include "assert.h"
  3. #include <cstdlib>
  4. #include <cstdio>
  5. #include <assert.h>
  6. #include <logs/log.h>
  7. #include <compilerIntrinsics.h>
  8. namespace pika
  9. {
  10. namespace assert
  11. {
  12. [[noreturn]]
  13. inline void terminate(...)
  14. {
  15. std::abort();
  16. PIKA_UNREACHABLE(); //optimize code after the exit
  17. }
  18. #ifdef PIKA_WINDOWS
  19. #include <Windows.h>
  20. void assertFunctionDevelopment(const char *expression, const char *file,
  21. int line, const char *comment)
  22. {
  23. char buffer[1024] = {};
  24. std::snprintf(buffer, sizeof(buffer),
  25. "Assertion failed\n\n"
  26. "Expression: \n%s\n\n"
  27. "File: %s\n"
  28. "Line: %d\n\n"
  29. "Comment: \n%s\n\n"
  30. "Press retry to debug."
  31. , expression
  32. , file
  33. , line
  34. , comment
  35. );
  36. int const action = MessageBoxA(0, buffer, "Pika error", MB_TASKMODAL
  37. | MB_ICONHAND | MB_ABORTRETRYIGNORE | MB_SETFOREGROUND);
  38. switch (action)
  39. {
  40. case IDABORT: //Abort the program
  41. {
  42. terminate();
  43. return;
  44. }
  45. case IDRETRY: //Break execution (debug)
  46. {
  47. #ifdef _MSC_VER
  48. __debugbreak();
  49. #endif
  50. //terminate();
  51. return;
  52. }
  53. case IDIGNORE: //Ignore assert
  54. {
  55. return;
  56. }
  57. }
  58. };
  59. void assertFunctionProduction(const char *expression, const char *file,
  60. int line, const char *comment)
  61. {
  62. char buffer[1024] = {};
  63. std::snprintf(buffer, sizeof(buffer),
  64. "Assertion failed\n\n"
  65. "Expression: \n%s\n\n"
  66. "File: %s\n"
  67. "Line: %d\n\n"
  68. "Comment: \n%s\n\n"
  69. "Please report this error to the developer."
  70. , expression
  71. , file
  72. , line
  73. , comment
  74. );
  75. int const action = MessageBoxA(0,
  76. buffer, "Pika error", MB_TASKMODAL
  77. | MB_ICONHAND | MB_OK | MB_SETFOREGROUND);
  78. terminate();
  79. }
  80. #elif defined(PIKA_LINUX)
  81. void assertFunctionDevelopment(const char *expression, const char *file,
  82. int line, const char *comment)
  83. {
  84. terminate();
  85. }
  86. void assertFunctionProduction(const char *expression, const char *file,
  87. int line, const char *comment)
  88. {
  89. terminate();
  90. }
  91. #endif
  92. void assertFunctionToLog(const char *expression, const char *file, int line, const char *comment)
  93. {
  94. char buffer[1024] = {};
  95. std::snprintf(buffer, sizeof(buffer),
  96. "Assertion failed\n"
  97. "Expression: \n%s\n"
  98. "File: %s\n"
  99. "Line: %d\n"
  100. "Comment: \n%s\n"
  101. , expression
  102. , file
  103. , line
  104. , comment
  105. );
  106. pika::logToFile(pika::LogManager::DefaultLogFile, buffer);
  107. }
  108. }
  109. };