error_macros.h 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. /*************************************************************************/
  2. /* error_macros.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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 = 0;
  60. next = 0;
  61. userdata = 0;
  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. #ifndef _STR
  75. #define _STR(m_x) #m_x
  76. #define _MKSTR(m_x) _STR(m_x)
  77. #endif
  78. #define _FNL __FILE__ ":"
  79. /** An index has failed if m_index<0 or m_index >=m_size, the function exits */
  80. #ifdef __GNUC__
  81. //#define FUNCTION_STR __PRETTY_FUNCTION__ - too annoying
  82. #define FUNCTION_STR __FUNCTION__
  83. #else
  84. #define FUNCTION_STR __FUNCTION__
  85. #endif
  86. // Don't use this directly; instead, use any of the CRASH_* macros
  87. #ifdef _MSC_VER
  88. #define GENERATE_TRAP \
  89. __debugbreak(); \
  90. /* Avoid warning about control paths */ \
  91. for (;;) { \
  92. }
  93. #else
  94. #define GENERATE_TRAP __builtin_trap();
  95. #endif
  96. // Used to strip debug messages in release mode
  97. #ifdef DEBUG_ENABLED
  98. #define DEBUG_STR(m_msg) m_msg
  99. #else
  100. #define DEBUG_STR(m_msg) ""
  101. #endif
  102. // (*): See https://stackoverflow.com/questions/257418/do-while-0-what-is-it-good-for
  103. /**
  104. * If `m_index` is less than 0 or greater than or equal to `m_size`, prints a generic
  105. * error message and returns from the function. This macro should be preferred to
  106. * `ERR_FAIL_COND` for bounds checking.
  107. */
  108. #define ERR_FAIL_INDEX(m_index, m_size) \
  109. do { \
  110. if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
  111. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size)); \
  112. return; \
  113. } \
  114. } while (0); // (*)
  115. /**
  116. * If `m_index` is less than 0 or greater than or equal to `m_size`, prints a custom
  117. * error message and returns from the function. This macro should be preferred to
  118. * `ERR_FAIL_COND_MSG` for bounds checking.
  119. */
  120. #define ERR_FAIL_INDEX_MSG(m_index, m_size, m_msg) \
  121. do { \
  122. if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
  123. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), DEBUG_STR(m_msg)); \
  124. return; \
  125. } \
  126. } while (0); // (*)
  127. /**
  128. * If `m_index` is less than 0 or greater than or equal to `m_size`,
  129. * prints a generic error message and returns the value specified in `m_retval`.
  130. * This macro should be preferred to `ERR_FAIL_COND_V` for bounds checking.
  131. */
  132. #define ERR_FAIL_INDEX_V(m_index, m_size, m_retval) \
  133. do { \
  134. if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
  135. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size)); \
  136. return m_retval; \
  137. } \
  138. } while (0); // (*)
  139. /**
  140. * If `m_index` is less than 0 or greater than or equal to `m_size`,
  141. * prints a custom error message and returns the value specified in `m_retval`.
  142. * This macro should be preferred to `ERR_FAIL_COND_V_MSG` for bounds checking.
  143. */
  144. #define ERR_FAIL_INDEX_V_MSG(m_index, m_size, m_retval, m_msg) \
  145. do { \
  146. if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
  147. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), DEBUG_STR(m_msg)); \
  148. return m_retval; \
  149. } \
  150. } while (0); // (*)
  151. /**
  152. * If `m_index` is greater than or equal to `m_size`,
  153. * prints a generic error message and returns the value specified in `m_retval`.
  154. * This macro should be preferred to `ERR_FAIL_COND_V` for unsigned bounds checking.
  155. */
  156. #define ERR_FAIL_UNSIGNED_INDEX(m_index, m_size) \
  157. do { \
  158. if (unlikely((m_index) >= (m_size))) { \
  159. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size)); \
  160. return; \
  161. } \
  162. } while (0); // (*)
  163. /**
  164. * If `m_index` is greater than or equal to `m_size`,
  165. * prints a generic error message and returns the value specified in `m_retval`.
  166. * This macro should be preferred to `ERR_FAIL_COND_V` for unsigned bounds checking.
  167. */
  168. #define ERR_FAIL_UNSIGNED_INDEX_V(m_index, m_size, m_retval) \
  169. do { \
  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)); \
  172. return m_retval; \
  173. } \
  174. } while (0); // (*)
  175. /**
  176. * If `m_index` is greater than or equal to `m_size`,
  177. * prints a custom error message and returns the value specified in `m_retval`.
  178. * This macro should be preferred to `ERR_FAIL_COND_V_MSG` for unsigned bounds checking.
  179. */
  180. #define ERR_FAIL_UNSIGNED_INDEX_V_MSG(m_index, m_size, m_retval, m_msg) \
  181. do { \
  182. if (unlikely((m_index) >= (m_size))) { \
  183. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), DEBUG_STR(m_msg)); \
  184. return m_retval; \
  185. } \
  186. } while (0); // (*)
  187. /**
  188. * If `m_index` is less than 0 or greater than or equal to `m_size`,
  189. * crashes the engine immediately with a generic error message.
  190. * Only use this if there's no sensible fallback (i.e. the error is unrecoverable).
  191. * This macro should be preferred to `CRASH_COND` for bounds checking.
  192. */
  193. #define CRASH_BAD_INDEX(m_index, m_size) \
  194. do { \
  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), "", true); \
  197. GENERATE_TRAP \
  198. } \
  199. } while (0); // (*)
  200. /**
  201. * If `m_index` is less than 0 or greater than or equal to `m_size`,
  202. * crashes the engine immediately with a custom error message.
  203. * Only use this if there's no sensible fallback (i.e. the error is unrecoverable).
  204. * This macro should be preferred to `CRASH_COND` for bounds checking.
  205. */
  206. #define CRASH_BAD_INDEX_MSG(m_index, m_size, m_msg) \
  207. do { \
  208. if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
  209. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), m_msg, true); \
  210. GENERATE_TRAP \
  211. } \
  212. } while (0); // (*)
  213. /**
  214. * If `m_index` is greater than or equal to `m_size`,
  215. * crashes the engine immediately with a generic error message.
  216. * Only use this if there's no sensible fallback (i.e. the error is unrecoverable).
  217. * This macro should be preferred to `CRASH_COND` for bounds checking.
  218. */
  219. #define CRASH_BAD_UNSIGNED_INDEX(m_index, m_size) \
  220. do { \
  221. if (unlikely((m_index) >= (m_size))) { \
  222. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), "", true); \
  223. GENERATE_TRAP \
  224. } \
  225. } while (0); // (*)
  226. /**
  227. * If `m_param` is `null`, prints a generic error message and returns from the function.
  228. */
  229. #define ERR_FAIL_NULL(m_param) \
  230. { \
  231. if (unlikely(!m_param)) { \
  232. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Parameter \"" _STR(m_param) "\" is null."); \
  233. return; \
  234. } \
  235. }
  236. /**
  237. * If `m_param` is `null`, prints a custom error message and returns from the function.
  238. */
  239. #define ERR_FAIL_NULL_MSG(m_param, m_msg) \
  240. { \
  241. if (unlikely(!m_param)) { \
  242. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Parameter \"" _STR(m_param) "\" is null.", DEBUG_STR(m_msg)); \
  243. return; \
  244. } \
  245. }
  246. /**
  247. * If `m_param` is `null`, prints a generic error message and returns the value specified in `m_retval`.
  248. */
  249. #define ERR_FAIL_NULL_V(m_param, m_retval) \
  250. { \
  251. if (unlikely(!m_param)) { \
  252. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Parameter \"" _STR(m_param) "\" is null."); \
  253. return m_retval; \
  254. } \
  255. }
  256. /**
  257. * If `m_param` is `null`, prints a custom error message and returns the value specified in `m_retval`.
  258. */
  259. #define ERR_FAIL_NULL_V_MSG(m_param, m_retval, m_msg) \
  260. { \
  261. if (unlikely(!m_param)) { \
  262. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Parameter \"" _STR(m_param) "\" is null.", DEBUG_STR(m_msg)); \
  263. return m_retval; \
  264. } \
  265. }
  266. /**
  267. * If `m_cond` evaluates to `true`, prints a generic error message and returns from the function.
  268. */
  269. #define ERR_FAIL_COND(m_cond) \
  270. { \
  271. if (unlikely(m_cond)) { \
  272. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true."); \
  273. return; \
  274. } \
  275. }
  276. /**
  277. * If `m_cond` evaluates to `true`, prints a custom error message and returns from the function.
  278. */
  279. #define ERR_FAIL_COND_MSG(m_cond, m_msg) \
  280. { \
  281. if (unlikely(m_cond)) { \
  282. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true.", DEBUG_STR(m_msg)); \
  283. return; \
  284. } \
  285. }
  286. /**
  287. * If `m_cond` evaluates to `true`, crashes the engine immediately with a generic error message.
  288. * Only use this if there's no sensible fallback (i.e. the error is unrecoverable).
  289. */
  290. #define CRASH_COND(m_cond) \
  291. { \
  292. if (unlikely(m_cond)) { \
  293. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "FATAL: Condition \"" _STR(m_cond) "\" is true."); \
  294. GENERATE_TRAP \
  295. } \
  296. }
  297. /**
  298. * If `m_cond` evaluates to `true`, crashes the engine immediately with a custom error message.
  299. * Only use this if there's no sensible fallback (i.e. the error is unrecoverable).
  300. */
  301. #define CRASH_COND_MSG(m_cond, m_msg) \
  302. { \
  303. if (unlikely(m_cond)) { \
  304. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "FATAL: Condition \"" _STR(m_cond) "\" is true.", DEBUG_STR(m_msg)); \
  305. GENERATE_TRAP \
  306. } \
  307. }
  308. /**
  309. * If `m_cond` evaluates to `true`, prints a generic error message and returns the value specified in `m_retval`.
  310. */
  311. #define ERR_FAIL_COND_V(m_cond, m_retval) \
  312. { \
  313. if (unlikely(m_cond)) { \
  314. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Returned: " _STR(m_retval)); \
  315. return m_retval; \
  316. } \
  317. }
  318. /**
  319. * If `m_cond` evaluates to `true`, prints a custom error message and returns the value specified in `m_retval`.
  320. */
  321. #define ERR_FAIL_COND_V_MSG(m_cond, m_retval, m_msg) \
  322. { \
  323. if (unlikely(m_cond)) { \
  324. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Returned: " _STR(m_retval), DEBUG_STR(m_msg)); \
  325. return m_retval; \
  326. } \
  327. }
  328. /**
  329. * If `m_cond` evaluates to `true`, prints a custom error message and continues the loop the macro is located in.
  330. */
  331. #define ERR_CONTINUE(m_cond) \
  332. { \
  333. if (unlikely(m_cond)) { \
  334. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Continuing."); \
  335. continue; \
  336. } \
  337. }
  338. /**
  339. * If `m_cond` evaluates to `true`, prints a custom error message and continues the loop the macro is located in.
  340. */
  341. #define ERR_CONTINUE_MSG(m_cond, m_msg) \
  342. { \
  343. if (unlikely(m_cond)) { \
  344. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Continuing.", DEBUG_STR(m_msg)); \
  345. continue; \
  346. } \
  347. }
  348. /**
  349. * If `m_cond` evaluates to `true`, prints a generic error message and breaks from the loop the macro is located in.
  350. */
  351. #define ERR_BREAK(m_cond) \
  352. { \
  353. if (unlikely(m_cond)) { \
  354. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Breaking."); \
  355. break; \
  356. } \
  357. }
  358. /**
  359. * If `m_cond` evaluates to `true`, prints a custom error message and breaks from the loop the macro is located in.
  360. */
  361. #define ERR_BREAK_MSG(m_cond, m_msg) \
  362. { \
  363. if (unlikely(m_cond)) { \
  364. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Breaking.", DEBUG_STR(m_msg)); \
  365. break; \
  366. } \
  367. }
  368. /**
  369. * Prints a generic error message and returns from the function.
  370. */
  371. #define ERR_FAIL() \
  372. { \
  373. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Method failed."); \
  374. return; \
  375. }
  376. /**
  377. * Prints a custom error message and returns from the function.
  378. */
  379. #define ERR_FAIL_MSG(m_msg) \
  380. { \
  381. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Method failed.", DEBUG_STR(m_msg)); \
  382. return; \
  383. }
  384. /**
  385. * Prints a generic error message and returns the value specified in `m_retval`.
  386. */
  387. #define ERR_FAIL_V(m_retval) \
  388. { \
  389. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Method failed. Returning: " __STR(m_retval)); \
  390. return m_retval; \
  391. }
  392. /**
  393. * Prints a custom error message and returns the value specified in `m_retval`.
  394. */
  395. #define ERR_FAIL_V_MSG(m_retval, m_msg) \
  396. { \
  397. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Method failed. Returning: " __STR(m_retval), DEBUG_STR(m_msg)); \
  398. return m_retval; \
  399. }
  400. /**
  401. * Crashes the engine immediately with a generic error message.
  402. * Only use this if there's no sensible fallback (i.e. the error is unrecoverable).
  403. */
  404. #define CRASH_NOW() \
  405. { \
  406. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "FATAL: Method failed."); \
  407. GENERATE_TRAP \
  408. }
  409. /**
  410. * Crashes the engine immediately with a custom error message.
  411. * Only use this if there's no sensible fallback (i.e. the error is unrecoverable).
  412. */
  413. #define CRASH_NOW_MSG(m_msg) \
  414. { \
  415. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "FATAL: Method failed.", DEBUG_STR(m_msg)); \
  416. GENERATE_TRAP \
  417. }
  418. /**
  419. * Prints an error message without returning.
  420. */
  421. #define ERR_PRINT(m_string) \
  422. { \
  423. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_string); \
  424. }
  425. /**
  426. * Prints an error message without returning.
  427. * FIXME: Remove this macro and replace all uses with `ERR_PRINT` as it's identical.
  428. */
  429. #define ERR_PRINTS(m_string) \
  430. { \
  431. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_string); \
  432. }
  433. /**
  434. * Prints an error message without returning, but only do so once in the application lifecycle.
  435. * This can be used to avoid spamming the console with error messages.
  436. */
  437. #define ERR_PRINT_ONCE(m_string) \
  438. { \
  439. static bool first_print = true; \
  440. if (first_print) { \
  441. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_string); \
  442. first_print = false; \
  443. } \
  444. }
  445. /**
  446. * Prints a warning message without returning. To warn about deprecated usage,
  447. * use `WARN_DEPRECATED` or `WARN_DEPRECATED_MSG` instead.
  448. */
  449. #define WARN_PRINT(m_string) \
  450. { \
  451. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_string, ERR_HANDLER_WARNING); \
  452. }
  453. /**
  454. * Prints a warning message without returning.
  455. * FIXME: Remove this macro and replace all uses with `WARN_PRINT` as it's identical.
  456. */
  457. #define WARN_PRINTS(m_string) \
  458. { \
  459. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_string, ERR_HANDLER_WARNING); \
  460. }
  461. /**
  462. * Prints a warning message without returning, but only do so once in the application lifecycle.
  463. * This can be used to avoid spamming the console with warning messages.
  464. */
  465. #define WARN_PRINT_ONCE(m_string) \
  466. { \
  467. static bool first_print = true; \
  468. if (first_print) { \
  469. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_string, ERR_HANDLER_WARNING); \
  470. first_print = false; \
  471. } \
  472. }
  473. /**
  474. * Prints a generic deprecation warning message without returning.
  475. * This should be preferred to `WARN_PRINT` for deprecation warnings.
  476. */
  477. #define WARN_DEPRECATED \
  478. { \
  479. static SafeFlag warning_shown; \
  480. if (!warning_shown.is_set()) { \
  481. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "This method has been deprecated and will be removed in the future.", ERR_HANDLER_WARNING); \
  482. warning_shown.set(); \
  483. } \
  484. }
  485. /**
  486. * Prints a custom deprecation warning message without returning.
  487. * This should be preferred to `WARN_PRINT` for deprecation warnings.
  488. */
  489. #define WARN_DEPRECATED_MSG(m_msg) \
  490. { \
  491. static SafeFlag warning_shown; \
  492. if (!warning_shown.is_set()) { \
  493. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "This method has been deprecated and will be removed in the future.", m_msg, ERR_HANDLER_WARNING); \
  494. warning_shown.set(); \
  495. } \
  496. }
  497. #endif