error_macros.h 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. /**************************************************************************/
  2. /* error_macros.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef ERROR_MACROS_H
  31. #define ERROR_MACROS_H
  32. #include "core/safe_refcount.h"
  33. #include "core/typedefs.h"
  34. /**
  35. * Error macros. Unlike exceptions and asserts, these macros try to maintain consistency and stability
  36. * inside the code. It is recommended to always return processable data, so in case of an error,
  37. * the engine can keep working well.
  38. * In most cases, bugs and/or invalid data are not fatal and should never allow a perfectly running application
  39. * to fail or crash.
  40. */
  41. /**
  42. * Pointer to the error macro printing function. Reassign to any function to have errors printed
  43. */
  44. /** Function used by the error macros */
  45. // function, file, line, error, explanation
  46. enum ErrorHandlerType {
  47. ERR_HANDLER_ERROR,
  48. ERR_HANDLER_WARNING,
  49. ERR_HANDLER_SCRIPT,
  50. ERR_HANDLER_SHADER,
  51. };
  52. class String;
  53. typedef void (*ErrorHandlerFunc)(void *, const char *, const char *, int p_line, const char *, const char *, ErrorHandlerType p_type);
  54. struct ErrorHandlerList {
  55. ErrorHandlerFunc errfunc;
  56. void *userdata;
  57. ErrorHandlerList *next;
  58. ErrorHandlerList() {
  59. errfunc = nullptr;
  60. next = nullptr;
  61. userdata = nullptr;
  62. }
  63. };
  64. void add_error_handler(ErrorHandlerList *p_handler);
  65. void remove_error_handler(ErrorHandlerList *p_handler);
  66. void _err_print_error(const char *p_function, const char *p_file, int p_line, const char *p_error, ErrorHandlerType p_type = ERR_HANDLER_ERROR);
  67. void _err_print_error(const char *p_function, const char *p_file, int p_line, const String &p_error, ErrorHandlerType p_type = ERR_HANDLER_ERROR);
  68. void _err_print_error(const char *p_function, const char *p_file, int p_line, const char *p_error, const char *p_message, ErrorHandlerType p_type = ERR_HANDLER_ERROR);
  69. void _err_print_error(const char *p_function, const char *p_file, int p_line, const String &p_error, const char *p_message, ErrorHandlerType p_type = ERR_HANDLER_ERROR);
  70. void _err_print_error(const char *p_function, const char *p_file, int p_line, const char *p_error, const String &p_message, ErrorHandlerType p_type = ERR_HANDLER_ERROR);
  71. void _err_print_error(const char *p_function, const char *p_file, int p_line, const String &p_error, const String &p_message, ErrorHandlerType p_type = ERR_HANDLER_ERROR);
  72. void _err_print_index_error(const char *p_function, const char *p_file, int p_line, int64_t p_index, int64_t p_size, const char *p_index_str, const char *p_size_str, const char *p_message = "", bool fatal = false);
  73. void _err_print_index_error(const char *p_function, const char *p_file, int p_line, int64_t p_index, int64_t p_size, const char *p_index_str, const char *p_size_str, const String &p_message, bool fatal = false);
  74. void _err_flush_stdout();
  75. #ifndef _STR
  76. #define _STR(m_x) #m_x
  77. #define _MKSTR(m_x) _STR(m_x)
  78. #endif
  79. #define _FNL __FILE__ ":"
  80. /** An index has failed if m_index<0 or m_index >=m_size, the function exits */
  81. #ifdef __GNUC__
  82. //#define FUNCTION_STR __PRETTY_FUNCTION__ - too annoying
  83. #define FUNCTION_STR __FUNCTION__
  84. #else
  85. #define FUNCTION_STR __FUNCTION__
  86. #endif
  87. // Don't use this directly; instead, use any of the CRASH_* macros
  88. #ifdef _MSC_VER
  89. #define GENERATE_TRAP \
  90. __debugbreak(); \
  91. /* Avoid warning about control paths */ \
  92. for (;;) { \
  93. }
  94. #else
  95. #define GENERATE_TRAP __builtin_trap();
  96. #endif
  97. // (*): See https://stackoverflow.com/questions/257418/do-while-0-what-is-it-good-for
  98. /**
  99. * If `m_index` is less than 0 or greater than or equal to `m_size`, prints a generic
  100. * error message and returns from the function. This macro should be preferred to
  101. * `ERR_FAIL_COND` for bounds checking.
  102. */
  103. #define ERR_FAIL_INDEX(m_index, m_size) \
  104. if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
  105. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size)); \
  106. return; \
  107. } else \
  108. ((void)0)
  109. /**
  110. * If `m_index` is less than 0 or greater than or equal to `m_size`, prints a custom
  111. * error message and returns from the function. This macro should be preferred to
  112. * `ERR_FAIL_COND_MSG` for bounds checking.
  113. */
  114. #define ERR_FAIL_INDEX_MSG(m_index, m_size, m_msg) \
  115. if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
  116. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), m_msg); \
  117. return; \
  118. } else \
  119. ((void)0)
  120. /**
  121. * If `m_index` is less than 0 or greater than or equal to `m_size`,
  122. * prints a generic error message and returns the value specified in `m_retval`.
  123. * This macro should be preferred to `ERR_FAIL_COND_V` for bounds checking.
  124. */
  125. #define ERR_FAIL_INDEX_V(m_index, m_size, m_retval) \
  126. if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
  127. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size)); \
  128. return m_retval; \
  129. } else \
  130. ((void)0)
  131. /**
  132. * If `m_index` is less than 0 or greater than or equal to `m_size`,
  133. * prints a custom error message and returns the value specified in `m_retval`.
  134. * This macro should be preferred to `ERR_FAIL_COND_V_MSG` for bounds checking.
  135. */
  136. #define ERR_FAIL_INDEX_V_MSG(m_index, m_size, m_retval, m_msg) \
  137. if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
  138. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), m_msg); \
  139. return m_retval; \
  140. } else \
  141. ((void)0)
  142. /**
  143. * If `m_index` is greater than or equal to `m_size`,
  144. * prints a generic error message and returns the value specified in `m_retval`.
  145. * This macro should be preferred to `ERR_FAIL_COND_V` for unsigned bounds checking.
  146. */
  147. #define ERR_FAIL_UNSIGNED_INDEX(m_index, m_size) \
  148. if (unlikely((m_index) >= (m_size))) { \
  149. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size)); \
  150. return; \
  151. } else \
  152. ((void)0)
  153. /**
  154. * If `m_index` is greater than or equal to `m_size`,
  155. * prints a generic error message and returns the value specified in `m_retval`.
  156. * This macro should be preferred to `ERR_FAIL_COND_V` for unsigned bounds checking.
  157. */
  158. #define ERR_FAIL_UNSIGNED_INDEX_V(m_index, m_size, m_retval) \
  159. if (unlikely((m_index) >= (m_size))) { \
  160. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size)); \
  161. return m_retval; \
  162. } else \
  163. ((void)0)
  164. /**
  165. * If `m_index` is greater than or equal to `m_size`,
  166. * prints a custom error message and returns the value specified in `m_retval`.
  167. * This macro should be preferred to `ERR_FAIL_COND_V_MSG` for unsigned bounds checking.
  168. */
  169. #define ERR_FAIL_UNSIGNED_INDEX_V_MSG(m_index, m_size, m_retval, m_msg) \
  170. if (unlikely((m_index) >= (m_size))) { \
  171. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), m_msg); \
  172. return m_retval; \
  173. } else \
  174. ((void)0)
  175. /**
  176. * If `m_index` is less than 0 or greater than or equal to `m_size`,
  177. * crashes the engine immediately with a generic error message.
  178. * Only use this if there's no sensible fallback (i.e. the error is unrecoverable).
  179. * This macro should be preferred to `CRASH_COND` for bounds checking.
  180. */
  181. #define CRASH_BAD_INDEX(m_index, m_size) \
  182. if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
  183. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), "", true); \
  184. _err_flush_stdout(); \
  185. GENERATE_TRAP \
  186. } else \
  187. ((void)0)
  188. /**
  189. * If `m_index` is less than 0 or greater than or equal to `m_size`,
  190. * crashes the engine immediately with a custom error message.
  191. * Only use this if there's no sensible fallback (i.e. the error is unrecoverable).
  192. * This macro should be preferred to `CRASH_COND` for bounds checking.
  193. */
  194. #define CRASH_BAD_INDEX_MSG(m_index, m_size, m_msg) \
  195. if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
  196. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), m_msg, true); \
  197. _err_flush_stdout(); \
  198. GENERATE_TRAP \
  199. } else \
  200. ((void)0)
  201. /**
  202. * If `m_index` is greater than or equal to `m_size`,
  203. * crashes the engine immediately with a generic error message.
  204. * Only use this if there's no sensible fallback (i.e. the error is unrecoverable).
  205. * This macro should be preferred to `CRASH_COND` for bounds checking.
  206. */
  207. #define CRASH_BAD_UNSIGNED_INDEX(m_index, m_size) \
  208. if (unlikely((m_index) >= (m_size))) { \
  209. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), "", true); \
  210. _err_flush_stdout(); \
  211. GENERATE_TRAP \
  212. } else \
  213. ((void)0)
  214. /**
  215. * If `m_param` is `null`, prints a generic error message and returns from the function.
  216. */
  217. #define ERR_FAIL_NULL(m_param) \
  218. if (unlikely(!m_param)) { \
  219. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Parameter \"" _STR(m_param) "\" is null."); \
  220. return; \
  221. } else \
  222. ((void)0)
  223. /**
  224. * If `m_param` is `null`, prints a custom error message and returns from the function.
  225. */
  226. #define ERR_FAIL_NULL_MSG(m_param, m_msg) \
  227. if (unlikely(!m_param)) { \
  228. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Parameter \"" _STR(m_param) "\" is null.", m_msg); \
  229. return; \
  230. } else \
  231. ((void)0)
  232. /**
  233. * If `m_param` is `null`, prints a generic error message and returns the value specified in `m_retval`.
  234. */
  235. #define ERR_FAIL_NULL_V(m_param, m_retval) \
  236. if (unlikely(!m_param)) { \
  237. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Parameter \"" _STR(m_param) "\" is null."); \
  238. return m_retval; \
  239. } else \
  240. ((void)0)
  241. /**
  242. * If `m_param` is `null`, prints a custom error message and returns the value specified in `m_retval`.
  243. */
  244. #define ERR_FAIL_NULL_V_MSG(m_param, m_retval, m_msg) \
  245. if (unlikely(!m_param)) { \
  246. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Parameter \"" _STR(m_param) "\" is null.", m_msg); \
  247. return m_retval; \
  248. } else \
  249. ((void)0)
  250. /**
  251. * If `m_cond` evaluates to `true`, prints a generic error message and returns from the function.
  252. */
  253. #define ERR_FAIL_COND(m_cond) \
  254. if (unlikely(m_cond)) { \
  255. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true."); \
  256. return; \
  257. } else \
  258. ((void)0)
  259. /**
  260. * If `m_cond` evaluates to `true`, prints a custom error message and returns from the function.
  261. */
  262. #define ERR_FAIL_COND_MSG(m_cond, m_msg) \
  263. if (unlikely(m_cond)) { \
  264. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true.", m_msg); \
  265. return; \
  266. } else \
  267. ((void)0)
  268. /**
  269. * If `m_cond` evaluates to `true`, crashes the engine immediately with a generic error message.
  270. * Only use this if there's no sensible fallback (i.e. the error is unrecoverable).
  271. */
  272. #define CRASH_COND(m_cond) \
  273. if (unlikely(m_cond)) { \
  274. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "FATAL: Condition \"" _STR(m_cond) "\" is true."); \
  275. _err_flush_stdout(); \
  276. GENERATE_TRAP \
  277. } else \
  278. ((void)0)
  279. /**
  280. * If `m_cond` evaluates to `true`, crashes the engine immediately with a custom error message.
  281. * Only use this if there's no sensible fallback (i.e. the error is unrecoverable).
  282. */
  283. #define CRASH_COND_MSG(m_cond, m_msg) \
  284. if (unlikely(m_cond)) { \
  285. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "FATAL: Condition \"" _STR(m_cond) "\" is true.", m_msg); \
  286. _err_flush_stdout(); \
  287. GENERATE_TRAP \
  288. } else \
  289. ((void)0)
  290. /**
  291. * If `m_cond` evaluates to `true`, prints a generic error message and returns the value specified in `m_retval`.
  292. */
  293. #define ERR_FAIL_COND_V(m_cond, m_retval) \
  294. if (unlikely(m_cond)) { \
  295. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Returned: " _STR(m_retval)); \
  296. return m_retval; \
  297. } else \
  298. ((void)0)
  299. /**
  300. * If `m_cond` evaluates to `true`, prints a custom error message and returns the value specified in `m_retval`.
  301. */
  302. #define ERR_FAIL_COND_V_MSG(m_cond, m_retval, m_msg) \
  303. if (unlikely(m_cond)) { \
  304. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Returned: " _STR(m_retval), m_msg); \
  305. return m_retval; \
  306. } else \
  307. ((void)0)
  308. /**
  309. * If `m_cond` evaluates to `true`, prints a custom error message and continues the loop the macro is located in.
  310. */
  311. #define ERR_CONTINUE(m_cond) \
  312. if (unlikely(m_cond)) { \
  313. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Continuing."); \
  314. continue; \
  315. } else \
  316. ((void)0)
  317. /**
  318. * If `m_cond` evaluates to `true`, prints a custom error message and continues the loop the macro is located in.
  319. */
  320. #define ERR_CONTINUE_MSG(m_cond, m_msg) \
  321. if (unlikely(m_cond)) { \
  322. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Continuing.", m_msg); \
  323. continue; \
  324. } else \
  325. ((void)0)
  326. /**
  327. * If `m_cond` evaluates to `true`, prints a generic error message and breaks from the loop the macro is located in.
  328. */
  329. #define ERR_BREAK(m_cond) \
  330. if (unlikely(m_cond)) { \
  331. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Breaking."); \
  332. break; \
  333. } else \
  334. ((void)0)
  335. /**
  336. * If `m_cond` evaluates to `true`, prints a custom error message and breaks from the loop the macro is located in.
  337. */
  338. #define ERR_BREAK_MSG(m_cond, m_msg) \
  339. if (unlikely(m_cond)) { \
  340. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Breaking.", m_msg); \
  341. break; \
  342. } else \
  343. ((void)0)
  344. /**
  345. * Prints a generic error message and returns from the function.
  346. */
  347. #define ERR_FAIL() \
  348. if (true) { \
  349. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Method failed."); \
  350. return; \
  351. } else \
  352. ((void)0)
  353. /**
  354. * Prints a custom error message and returns from the function.
  355. */
  356. #define ERR_FAIL_MSG(m_msg) \
  357. if (true) { \
  358. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Method failed.", m_msg); \
  359. return; \
  360. } else \
  361. ((void)0)
  362. /**
  363. * Prints a generic error message and returns the value specified in `m_retval`.
  364. */
  365. #define ERR_FAIL_V(m_retval) \
  366. if (true) { \
  367. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Method failed. Returning: " __STR(m_retval)); \
  368. return m_retval; \
  369. } else \
  370. ((void)0)
  371. /**
  372. * Prints a custom error message and returns the value specified in `m_retval`.
  373. */
  374. #define ERR_FAIL_V_MSG(m_retval, m_msg) \
  375. if (true) { \
  376. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Method failed. Returning: " __STR(m_retval), m_msg); \
  377. return m_retval; \
  378. } else \
  379. ((void)0)
  380. /**
  381. * Crashes the engine immediately with a generic error message.
  382. * Only use this if there's no sensible fallback (i.e. the error is unrecoverable).
  383. */
  384. #define CRASH_NOW() \
  385. if (true) { \
  386. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "FATAL: Method failed."); \
  387. _err_flush_stdout(); \
  388. GENERATE_TRAP \
  389. } else \
  390. ((void)0)
  391. /**
  392. * Crashes the engine immediately with a custom error message.
  393. * Only use this if there's no sensible fallback (i.e. the error is unrecoverable).
  394. */
  395. #define CRASH_NOW_MSG(m_msg) \
  396. if (true) { \
  397. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "FATAL: Method failed.", m_msg); \
  398. _err_flush_stdout(); \
  399. GENERATE_TRAP \
  400. } else \
  401. ((void)0)
  402. /**
  403. * Prints an error message without returning.
  404. */
  405. #define ERR_PRINT(m_string) \
  406. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_string)
  407. /**
  408. * Prints an error message without returning, but only do so once in the application lifecycle.
  409. * This can be used to avoid spamming the console with error messages.
  410. */
  411. #define ERR_PRINT_ONCE(m_string) \
  412. if (true) { \
  413. static bool first_print = true; \
  414. if (first_print) { \
  415. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_string); \
  416. first_print = false; \
  417. } \
  418. } else \
  419. ((void)0)
  420. /**
  421. * Prints a warning message without returning. To warn about deprecated usage,
  422. * use `WARN_DEPRECATED` or `WARN_DEPRECATED_MSG` instead.
  423. */
  424. #define WARN_PRINT(m_string) \
  425. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_string, ERR_HANDLER_WARNING)
  426. /**
  427. * Prints a warning message without returning, but only do so once in the application lifecycle.
  428. * This can be used to avoid spamming the console with warning messages.
  429. */
  430. #define WARN_PRINT_ONCE(m_string) \
  431. if (true) { \
  432. static bool first_print = true; \
  433. if (first_print) { \
  434. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_string, ERR_HANDLER_WARNING); \
  435. first_print = false; \
  436. } \
  437. } else \
  438. ((void)0)
  439. /**
  440. * Prints a generic deprecation warning message without returning.
  441. * This should be preferred to `WARN_PRINT` for deprecation warnings.
  442. */
  443. #define WARN_DEPRECATED \
  444. if (true) { \
  445. static SafeFlag warning_shown; \
  446. if (!warning_shown.is_set()) { \
  447. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "This method has been deprecated and will be removed in the future.", ERR_HANDLER_WARNING); \
  448. warning_shown.set(); \
  449. } \
  450. } else \
  451. ((void)0)
  452. /**
  453. * Prints a custom deprecation warning message without returning.
  454. * This should be preferred to `WARN_PRINT` for deprecation warnings.
  455. */
  456. #define WARN_DEPRECATED_MSG(m_msg) \
  457. if (true) { \
  458. static SafeFlag warning_shown; \
  459. if (!warning_shown.is_set()) { \
  460. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "This method has been deprecated and will be removed in the future.", m_msg, ERR_HANDLER_WARNING); \
  461. warning_shown.set(); \
  462. } \
  463. } else \
  464. ((void)0)
  465. /**
  466. * This should be a 'free' assert for program flow and should not be needed in any releases,
  467. * only used in dev builds.
  468. */
  469. #ifdef DEV_ENABLED
  470. #define DEV_ASSERT(m_cond) \
  471. if (unlikely(!(m_cond))) { \
  472. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "FATAL: DEV_ASSERT failed \"" _STR(m_cond) "\" is false."); \
  473. _err_flush_stdout(); \
  474. GENERATE_TRAP \
  475. } else \
  476. ((void)0)
  477. #else
  478. #define DEV_ASSERT(m_cond)
  479. #endif
  480. /**
  481. * These should be 'free' checks for program flow and should not be needed in any releases,
  482. * only used in dev builds.
  483. */
  484. #ifdef DEV_ENABLED
  485. #define DEV_CHECK(m_cond) \
  486. if (unlikely(!(m_cond))) { \
  487. ERR_PRINT("DEV_CHECK failed \"" _STR(m_cond) "\" is false."); \
  488. } else \
  489. ((void)0)
  490. #else
  491. #define DEV_CHECK(m_cond)
  492. #endif
  493. #ifdef DEV_ENABLED
  494. #define DEV_CHECK_ONCE(m_cond) \
  495. if (unlikely(!(m_cond))) { \
  496. ERR_PRINT_ONCE("DEV_CHECK_ONCE failed \"" _STR(m_cond) "\" is false."); \
  497. } else \
  498. ((void)0)
  499. #else
  500. #define DEV_CHECK_ONCE(m_cond)
  501. #endif
  502. #endif // ERROR_MACROS_H