error_macros.h 34 KB

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