error_macros.h 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  1. /*************************************************************************/
  2. /* error_macros.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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/typedefs.h"
  33. #include "core/templates/safe_refcount.h"
  34. class String;
  35. enum ErrorHandlerType {
  36. ERR_HANDLER_ERROR,
  37. ERR_HANDLER_WARNING,
  38. ERR_HANDLER_SCRIPT,
  39. ERR_HANDLER_SHADER,
  40. };
  41. // Pointer to the error handler printing function. Reassign to any function to have errors printed.
  42. // Parameters: userdata, function, file, line, error, explanation, type.
  43. typedef void (*ErrorHandlerFunc)(void *, const char *, const char *, int p_line, const char *, const char *, bool p_editor_notify, ErrorHandlerType p_type);
  44. struct ErrorHandlerList {
  45. ErrorHandlerFunc errfunc = nullptr;
  46. void *userdata = nullptr;
  47. ErrorHandlerList *next = nullptr;
  48. ErrorHandlerList() {}
  49. };
  50. void add_error_handler(ErrorHandlerList *p_handler);
  51. void remove_error_handler(const ErrorHandlerList *p_handler);
  52. // Functions used by the error macros.
  53. void _err_print_error(const char *p_function, const char *p_file, int p_line, const char *p_error, bool p_editor_notify = false, ErrorHandlerType p_type = ERR_HANDLER_ERROR);
  54. void _err_print_error(const char *p_function, const char *p_file, int p_line, const String &p_error, bool p_editor_notify = false, ErrorHandlerType p_type = ERR_HANDLER_ERROR);
  55. void _err_print_error(const char *p_function, const char *p_file, int p_line, const char *p_error, const char *p_message, bool p_editor_notify = false, ErrorHandlerType p_type = ERR_HANDLER_ERROR);
  56. void _err_print_error(const char *p_function, const char *p_file, int p_line, const String &p_error, const char *p_message, bool p_editor_notify = false, ErrorHandlerType p_type = ERR_HANDLER_ERROR);
  57. void _err_print_error(const char *p_function, const char *p_file, int p_line, const char *p_error, const String &p_message, bool p_editor_notify = false, ErrorHandlerType p_type = ERR_HANDLER_ERROR);
  58. void _err_print_error(const char *p_function, const char *p_file, int p_line, const String &p_error, const String &p_message, bool p_editor_notify = false, ErrorHandlerType p_type = ERR_HANDLER_ERROR);
  59. 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 p_editor_notify = false, bool fatal = false);
  60. 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 p_editor_notify = false, bool fatal = false);
  61. void _err_flush_stdout();
  62. #ifdef __GNUC__
  63. //#define FUNCTION_STR __PRETTY_FUNCTION__ - too annoying
  64. #define FUNCTION_STR __FUNCTION__
  65. #else
  66. #define FUNCTION_STR __FUNCTION__
  67. #endif
  68. #ifdef _MSC_VER
  69. /**
  70. * Don't use GENERATE_TRAP() directly, should only be used be the macros below.
  71. */
  72. #define GENERATE_TRAP() __debugbreak()
  73. #else
  74. /**
  75. * Don't use GENERATE_TRAP() directly, should only be used be the macros below.
  76. */
  77. #define GENERATE_TRAP() __builtin_trap()
  78. #endif
  79. /**
  80. * Error macros.
  81. * WARNING: These macros work in the opposite way to assert().
  82. *
  83. * Unlike exceptions and asserts, these macros try to maintain consistency and stability.
  84. * In most cases, bugs and/or invalid data are not fatal. They should never allow a perfectly
  85. * running application to fail or crash.
  86. * Always try to return processable data, so the engine can keep running well.
  87. * Use the _MSG versions to print a meaningful message to help with debugging.
  88. *
  89. * The `((void)0)` no-op statement is used as a trick to force us to put a semicolon after
  90. * those macros, making them look like proper statements.
  91. * The if wrappers are used to ensure that the macro replacement does not trigger unexpected
  92. * issues when expanded e.g. after an `if (cond) ERR_FAIL();` without braces.
  93. */
  94. // Index out of bounds error macros.
  95. // These macros should be used instead of `ERR_FAIL_COND` for bounds checking.
  96. // Integer index out of bounds error macros.
  97. /**
  98. * Try using `ERR_FAIL_INDEX_MSG`.
  99. * Only use this macro if there is no sensible error message.
  100. *
  101. * Ensures an integer index `m_index` is less than `m_size` and greater than or equal to 0.
  102. * If not, the current function returns.
  103. */
  104. #define ERR_FAIL_INDEX(m_index, m_size) \
  105. if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
  106. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size)); \
  107. return; \
  108. } else \
  109. ((void)0)
  110. /**
  111. * Ensures an integer index `m_index` is less than `m_size` and greater than or equal to 0.
  112. * If not, prints `m_msg` and the current function returns.
  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. * Ensures an integer index `m_index` is less than `m_size` and greater than or equal to 0.
  122. * If not, prints `m_msg`, notifies in the editor, and the current function returns.
  123. */
  124. #define ERR_FAIL_INDEX_EDMSG(m_index, m_size, m_msg) \
  125. if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
  126. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), m_msg, true); \
  127. return; \
  128. } else \
  129. ((void)0)
  130. /**
  131. * Try using `ERR_FAIL_INDEX_V_MSG`.
  132. * Only use this macro if there is no sensible error message.
  133. *
  134. * Ensures an integer index `m_index` is less than `m_size` and greater than or equal to 0.
  135. * If not, the current function returns `m_retval`.
  136. */
  137. #define ERR_FAIL_INDEX_V(m_index, m_size, m_retval) \
  138. if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
  139. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size)); \
  140. return m_retval; \
  141. } else \
  142. ((void)0)
  143. /**
  144. * Ensures an integer index `m_index` is less than `m_size` and greater than or equal to 0.
  145. * If not, prints `m_msg` and the current function returns `m_retval`.
  146. */
  147. #define ERR_FAIL_INDEX_V_MSG(m_index, m_size, m_retval, m_msg) \
  148. if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
  149. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), m_msg); \
  150. return m_retval; \
  151. } else \
  152. ((void)0)
  153. /**
  154. * Ensures an integer index `m_index` is less than `m_size` and greater than or equal to 0.
  155. * If not, prints `m_msg`, notifies in the editor, and the current function returns `m_retval`.
  156. */
  157. #define ERR_FAIL_INDEX_V_EDMSG(m_index, m_size, m_retval, m_msg) \
  158. if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
  159. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), m_msg, true); \
  160. return m_retval; \
  161. } else \
  162. ((void)0)
  163. /**
  164. * Try using `ERR_FAIL_INDEX_MSG` or `ERR_FAIL_INDEX_V_MSG`.
  165. * Only use this macro if there is no sensible fallback i.e. the error is unrecoverable, and
  166. * there is no sensible error message.
  167. *
  168. * Ensures an integer index `m_index` is less than `m_size` and greater than or equal to 0.
  169. * If not, the application crashes.
  170. */
  171. #define CRASH_BAD_INDEX(m_index, m_size) \
  172. if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
  173. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), "", true); \
  174. _err_flush_stdout(); \
  175. GENERATE_TRAP(); \
  176. } else \
  177. ((void)0)
  178. /**
  179. * Try using `ERR_FAIL_INDEX_MSG` or `ERR_FAIL_INDEX_V_MSG`.
  180. * Only use this macro if there is no sensible fallback i.e. the error is unrecoverable.
  181. *
  182. * Ensures an integer index `m_index` is less than `m_size` and greater than or equal to 0.
  183. * If not, prints `m_msg` and the application crashes.
  184. */
  185. #define CRASH_BAD_INDEX_MSG(m_index, m_size, m_msg) \
  186. if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
  187. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), m_msg, true); \
  188. _err_flush_stdout(); \
  189. GENERATE_TRAP(); \
  190. } else \
  191. ((void)0)
  192. // Unsigned integer index out of bounds error macros.
  193. /**
  194. * Try using `ERR_FAIL_UNSIGNED_INDEX_MSG`.
  195. * Only use this macro if there is no sensible error message.
  196. *
  197. * Ensures an unsigned integer index `m_index` is less than `m_size`.
  198. * If not, the current function returns.
  199. */
  200. #define ERR_FAIL_UNSIGNED_INDEX(m_index, m_size) \
  201. if (unlikely((m_index) >= (m_size))) { \
  202. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size)); \
  203. return; \
  204. } else \
  205. ((void)0)
  206. /**
  207. * Ensures an unsigned integer index `m_index` is less than `m_size`.
  208. * If not, prints `m_msg` and the current function returns.
  209. */
  210. #define ERR_FAIL_UNSIGNED_INDEX_MSG(m_index, m_size, m_msg) \
  211. if (unlikely((m_index) >= (m_size))) { \
  212. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), m_msg); \
  213. return; \
  214. } else \
  215. ((void)0)
  216. /**
  217. * Ensures an unsigned integer index `m_index` is less than `m_size`.
  218. * If not, prints `m_msg`, notifies in the editor, and the current function returns.
  219. */
  220. #define ERR_FAIL_UNSIGNED_INDEX_EDMSG(m_index, m_size, m_msg) \
  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), m_msg, true); \
  223. return; \
  224. } else \
  225. ((void)0)
  226. /**
  227. * Try using `ERR_FAIL_UNSIGNED_INDEX_V_MSG`.
  228. * Only use this macro if there is no sensible error message.
  229. *
  230. * Ensures an unsigned integer index `m_index` is less than `m_size`.
  231. * If not, the current function returns `m_retval`.
  232. */
  233. #define ERR_FAIL_UNSIGNED_INDEX_V(m_index, m_size, m_retval) \
  234. if (unlikely((m_index) >= (m_size))) { \
  235. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size)); \
  236. return m_retval; \
  237. } else \
  238. ((void)0)
  239. /**
  240. * Ensures an unsigned integer index `m_index` is less than `m_size`.
  241. * If not, prints `m_msg` and the current function returns `m_retval`.
  242. */
  243. #define ERR_FAIL_UNSIGNED_INDEX_V_MSG(m_index, m_size, m_retval, m_msg) \
  244. if (unlikely((m_index) >= (m_size))) { \
  245. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), m_msg); \
  246. return m_retval; \
  247. } else \
  248. ((void)0)
  249. /**
  250. * Ensures an unsigned integer index `m_index` is less than `m_size`.
  251. * If not, prints `m_msg`, notifies in the editor, and the current function returns `m_retval`.
  252. */
  253. #define ERR_FAIL_UNSIGNED_INDEX_V_EDMSG(m_index, m_size, m_retval, m_msg) \
  254. if (unlikely((m_index) >= (m_size))) { \
  255. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), m_msg, true); \
  256. return m_retval; \
  257. } else \
  258. ((void)0)
  259. /**
  260. * Try using `ERR_FAIL_UNSIGNED_INDEX_MSG` or `ERR_FAIL_UNSIGNED_INDEX_V_MSG`.
  261. * Only use this macro if there is no sensible fallback i.e. the error is unrecoverable, and
  262. * there is no sensible error message.
  263. *
  264. * Ensures an unsigned integer index `m_index` is less than `m_size`.
  265. * If not, the application crashes.
  266. */
  267. #define CRASH_BAD_UNSIGNED_INDEX(m_index, m_size) \
  268. if (unlikely((m_index) >= (m_size))) { \
  269. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), "", true); \
  270. _err_flush_stdout(); \
  271. GENERATE_TRAP(); \
  272. } else \
  273. ((void)0)
  274. /**
  275. * Try using `ERR_FAIL_UNSIGNED_INDEX_MSG` or `ERR_FAIL_UNSIGNED_INDEX_V_MSG`.
  276. * Only use this macro if there is no sensible fallback i.e. the error is unrecoverable.
  277. *
  278. * Ensures an unsigned integer index `m_index` is less than `m_size`.
  279. * If not, prints `m_msg` and the application crashes.
  280. */
  281. #define CRASH_BAD_UNSIGNED_INDEX_MSG(m_index, m_size, m_msg) \
  282. if (unlikely((m_index) >= (m_size))) { \
  283. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), m_msg, true); \
  284. _err_flush_stdout(); \
  285. GENERATE_TRAP(); \
  286. } else \
  287. ((void)0)
  288. // Null reference error macros.
  289. /**
  290. * Try using `ERR_FAIL_NULL_MSG`.
  291. * Only use this macro if there is no sensible error message.
  292. *
  293. * Ensures a pointer `m_param` is not null.
  294. * If it is null, the current function returns.
  295. */
  296. #define ERR_FAIL_NULL(m_param) \
  297. if (unlikely(m_param == nullptr)) { \
  298. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Parameter \"" _STR(m_param) "\" is null."); \
  299. return; \
  300. } else \
  301. ((void)0)
  302. /**
  303. * Ensures a pointer `m_param` is not null.
  304. * If it is null, prints `m_msg` and the current function returns.
  305. */
  306. #define ERR_FAIL_NULL_MSG(m_param, m_msg) \
  307. if (unlikely(m_param == nullptr)) { \
  308. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Parameter \"" _STR(m_param) "\" is null.", m_msg); \
  309. return; \
  310. } else \
  311. ((void)0)
  312. /**
  313. * Ensures a pointer `m_param` is not null.
  314. * If it is null, prints `m_msg`, notifies in the editor, and the current function returns.
  315. */
  316. #define ERR_FAIL_NULL_EDMSG(m_param, m_msg) \
  317. if (unlikely(m_param == nullptr)) { \
  318. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Parameter \"" _STR(m_param) "\" is null.", m_msg, true); \
  319. return; \
  320. } else \
  321. ((void)0)
  322. /**
  323. * Try using `ERR_FAIL_NULL_V_MSG`.
  324. * Only use this macro if there is no sensible error message.
  325. *
  326. * Ensures a pointer `m_param` is not null.
  327. * If it is null, the current function returns `m_retval`.
  328. */
  329. #define ERR_FAIL_NULL_V(m_param, m_retval) \
  330. if (unlikely(m_param == nullptr)) { \
  331. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Parameter \"" _STR(m_param) "\" is null."); \
  332. return m_retval; \
  333. } else \
  334. ((void)0)
  335. /**
  336. * Ensures a pointer `m_param` is not null.
  337. * If it is null, prints `m_msg` and the current function returns `m_retval`.
  338. */
  339. #define ERR_FAIL_NULL_V_MSG(m_param, m_retval, m_msg) \
  340. if (unlikely(m_param == nullptr)) { \
  341. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Parameter \"" _STR(m_param) "\" is null.", m_msg); \
  342. return m_retval; \
  343. } else \
  344. ((void)0)
  345. /**
  346. * Ensures a pointer `m_param` is not null.
  347. * If it is null, prints `m_msg`, notifies in the editor, and the current function returns `m_retval`.
  348. */
  349. #define ERR_FAIL_NULL_V_EDMSG(m_param, m_retval, m_msg) \
  350. if (unlikely(m_param == nullptr)) { \
  351. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Parameter \"" _STR(m_param) "\" is null.", m_msg, true); \
  352. return m_retval; \
  353. } else \
  354. ((void)0)
  355. /**
  356. * Try using `ERR_FAIL_COND_MSG`.
  357. * Only use this macro if there is no sensible error message.
  358. * If checking for null use ERR_FAIL_NULL_MSG instead.
  359. * If checking index bounds use ERR_FAIL_INDEX_MSG instead.
  360. *
  361. * Ensures `m_cond` is false.
  362. * If `m_cond` is true, the current function returns.
  363. */
  364. #define ERR_FAIL_COND(m_cond) \
  365. if (unlikely(m_cond)) { \
  366. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true."); \
  367. return; \
  368. } else \
  369. ((void)0)
  370. /**
  371. * Ensures `m_cond` is false.
  372. * If `m_cond` is true, prints `m_msg` and the current function returns.
  373. *
  374. * If checking for null use ERR_FAIL_NULL_MSG instead.
  375. * If checking index bounds use ERR_FAIL_INDEX_MSG instead.
  376. */
  377. #define ERR_FAIL_COND_MSG(m_cond, m_msg) \
  378. if (unlikely(m_cond)) { \
  379. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true.", m_msg); \
  380. return; \
  381. } else \
  382. ((void)0)
  383. /**
  384. * Ensures `m_cond` is false.
  385. * If `m_cond` is true, prints `m_msg`, notifies in the editor, and the current function returns.
  386. *
  387. * If checking for null use ERR_FAIL_NULL_MSG instead.
  388. * If checking index bounds use ERR_FAIL_INDEX_MSG instead.
  389. */
  390. #define ERR_FAIL_COND_EDMSG(m_cond, m_msg) \
  391. if (unlikely(m_cond)) { \
  392. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true.", m_msg, true); \
  393. return; \
  394. } else \
  395. ((void)0)
  396. /**
  397. * Try using `ERR_FAIL_COND_V_MSG`.
  398. * Only use this macro if there is no sensible error message.
  399. * If checking for null use ERR_FAIL_NULL_V_MSG instead.
  400. * If checking index bounds use ERR_FAIL_INDEX_V_MSG instead.
  401. *
  402. * Ensures `m_cond` is false.
  403. * If `m_cond` is true, the current function returns `m_retval`.
  404. */
  405. #define ERR_FAIL_COND_V(m_cond, m_retval) \
  406. if (unlikely(m_cond)) { \
  407. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Returning: " _STR(m_retval)); \
  408. return m_retval; \
  409. } else \
  410. ((void)0)
  411. /**
  412. * Ensures `m_cond` is false.
  413. * If `m_cond` is true, prints `m_msg` and the current function returns `m_retval`.
  414. *
  415. * If checking for null use ERR_FAIL_NULL_V_MSG instead.
  416. * If checking index bounds use ERR_FAIL_INDEX_V_MSG instead.
  417. */
  418. #define ERR_FAIL_COND_V_MSG(m_cond, m_retval, m_msg) \
  419. if (unlikely(m_cond)) { \
  420. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Returning: " _STR(m_retval), m_msg); \
  421. return m_retval; \
  422. } else \
  423. ((void)0)
  424. /**
  425. * Ensures `m_cond` is false.
  426. * If `m_cond` is true, prints `m_msg`, notifies in the editor, and the current function returns `m_retval`.
  427. *
  428. * If checking for null use ERR_FAIL_NULL_V_MSG instead.
  429. * If checking index bounds use ERR_FAIL_INDEX_V_MSG instead.
  430. */
  431. #define ERR_FAIL_COND_V_EDMSG(m_cond, m_retval, m_msg) \
  432. if (unlikely(m_cond)) { \
  433. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Returning: " _STR(m_retval), m_msg, true); \
  434. return m_retval; \
  435. } else \
  436. ((void)0)
  437. /**
  438. * Try using `ERR_CONTINUE_MSG`.
  439. * Only use this macro if there is no sensible error message.
  440. *
  441. * Ensures `m_cond` is false.
  442. * If `m_cond` is true, the current loop continues.
  443. */
  444. #define ERR_CONTINUE(m_cond) \
  445. if (unlikely(m_cond)) { \
  446. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Continuing."); \
  447. continue; \
  448. } else \
  449. ((void)0)
  450. /**
  451. * Ensures `m_cond` is false.
  452. * If `m_cond` is true, prints `m_msg` and the current loop continues.
  453. */
  454. #define ERR_CONTINUE_MSG(m_cond, m_msg) \
  455. if (unlikely(m_cond)) { \
  456. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Continuing.", m_msg); \
  457. continue; \
  458. } else \
  459. ((void)0)
  460. /**
  461. * Ensures `m_cond` is false.
  462. * If `m_cond` is true, prints `m_msg`, notifies in the editor, and the current loop continues.
  463. */
  464. #define ERR_CONTINUE_EDMSG(m_cond, m_msg) \
  465. if (unlikely(m_cond)) { \
  466. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Continuing.", m_msg, true); \
  467. continue; \
  468. } else \
  469. ((void)0)
  470. /**
  471. * Try using `ERR_BREAK_MSG`.
  472. * Only use this macro if there is no sensible error message.
  473. *
  474. * Ensures `m_cond` is false.
  475. * If `m_cond` is true, the current loop breaks.
  476. */
  477. #define ERR_BREAK(m_cond) \
  478. if (unlikely(m_cond)) { \
  479. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Breaking."); \
  480. break; \
  481. } else \
  482. ((void)0)
  483. /**
  484. * Ensures `m_cond` is false.
  485. * If `m_cond` is true, prints `m_msg` and the current loop breaks.
  486. */
  487. #define ERR_BREAK_MSG(m_cond, m_msg) \
  488. if (unlikely(m_cond)) { \
  489. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Breaking.", m_msg); \
  490. break; \
  491. } else \
  492. ((void)0)
  493. /**
  494. * Ensures `m_cond` is false.
  495. * If `m_cond` is true, prints `m_msg`, notifies in the editor, and the current loop breaks.
  496. */
  497. #define ERR_BREAK_EDMSG(m_cond, m_msg) \
  498. if (unlikely(m_cond)) { \
  499. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Breaking.", m_msg, true); \
  500. break; \
  501. } else \
  502. ((void)0)
  503. /**
  504. * Try using `ERR_FAIL_COND_MSG` or `ERR_FAIL_COND_V_MSG`.
  505. * Only use this macro if there is no sensible fallback i.e. the error is unrecoverable, and
  506. * there is no sensible error message.
  507. *
  508. * Ensures `m_cond` is false.
  509. * If `m_cond` is true, the application crashes.
  510. */
  511. #define CRASH_COND(m_cond) \
  512. if (unlikely(m_cond)) { \
  513. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "FATAL: Condition \"" _STR(m_cond) "\" is true."); \
  514. _err_flush_stdout(); \
  515. GENERATE_TRAP(); \
  516. } else \
  517. ((void)0)
  518. /**
  519. * Try using `ERR_FAIL_COND_MSG` or `ERR_FAIL_COND_V_MSG`.
  520. * Only use this macro if there is no sensible fallback i.e. the error is unrecoverable.
  521. *
  522. * Ensures `m_cond` is false.
  523. * If `m_cond` is true, prints `m_msg` and the application crashes.
  524. */
  525. #define CRASH_COND_MSG(m_cond, m_msg) \
  526. if (unlikely(m_cond)) { \
  527. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "FATAL: Condition \"" _STR(m_cond) "\" is true.", m_msg); \
  528. _err_flush_stdout(); \
  529. GENERATE_TRAP(); \
  530. } else \
  531. ((void)0)
  532. // Generic error macros.
  533. /**
  534. * Try using `ERR_FAIL_COND_MSG` or `ERR_FAIL_MSG`.
  535. * Only use this macro if more complex error detection or recovery is required, and
  536. * there is no sensible error message.
  537. *
  538. * The current function returns.
  539. */
  540. #define ERR_FAIL() \
  541. if (true) { \
  542. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Method/function failed."); \
  543. return; \
  544. } else \
  545. ((void)0)
  546. /**
  547. * Try using `ERR_FAIL_COND_MSG`.
  548. * Only use this macro if more complex error detection or recovery is required.
  549. *
  550. * Prints `m_msg`, and the current function returns.
  551. */
  552. #define ERR_FAIL_MSG(m_msg) \
  553. if (true) { \
  554. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Method/function failed.", m_msg); \
  555. return; \
  556. } else \
  557. ((void)0)
  558. /**
  559. * Try using `ERR_FAIL_COND_MSG`.
  560. * Only use this macro if more complex error detection or recovery is required.
  561. *
  562. * Prints `m_msg`, notifies in the editor, and the current function returns.
  563. */
  564. #define ERR_FAIL_EDMSG(m_msg) \
  565. if (true) { \
  566. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Method/function failed.", m_msg, true); \
  567. return; \
  568. } else \
  569. ((void)0)
  570. /**
  571. * Try using `ERR_FAIL_COND_V_MSG` or `ERR_FAIL_V_MSG`.
  572. * Only use this macro if more complex error detection or recovery is required, and
  573. * there is no sensible error message.
  574. *
  575. * The current function returns `m_retval`.
  576. */
  577. #define ERR_FAIL_V(m_retval) \
  578. if (true) { \
  579. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Method/function failed. Returning: " _STR(m_retval)); \
  580. return m_retval; \
  581. } else \
  582. ((void)0)
  583. /**
  584. * Try using `ERR_FAIL_COND_V_MSG`.
  585. * Only use this macro if more complex error detection or recovery is required.
  586. *
  587. * Prints `m_msg`, and the current function returns `m_retval`.
  588. */
  589. #define ERR_FAIL_V_MSG(m_retval, m_msg) \
  590. if (true) { \
  591. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Method/function failed. Returning: " _STR(m_retval), m_msg); \
  592. return m_retval; \
  593. } else \
  594. ((void)0)
  595. /**
  596. * Try using `ERR_FAIL_COND_V_MSG`.
  597. * Only use this macro if more complex error detection or recovery is required.
  598. *
  599. * Prints `m_msg`, notifies in the editor, and the current function returns `m_retval`.
  600. */
  601. #define ERR_FAIL_V_EDMSG(m_retval, m_msg) \
  602. if (true) { \
  603. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Method/function failed. Returning: " _STR(m_retval), m_msg, true); \
  604. return m_retval; \
  605. } else \
  606. ((void)0)
  607. /**
  608. * Try using `ERR_FAIL_COND_MSG`, `ERR_FAIL_COND_V_MSG`, `ERR_CONTINUE_MSG` or ERR_BREAK_MSG.
  609. * Only use this macro at the start of a function that has not been implemented yet, or
  610. * if more complex error detection or recovery is required.
  611. *
  612. * Prints `m_msg`.
  613. */
  614. #define ERR_PRINT(m_msg) \
  615. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_msg)
  616. /**
  617. * Try using `ERR_FAIL_COND_MSG`, `ERR_FAIL_COND_V_MSG`, `ERR_CONTINUE_MSG` or ERR_BREAK_MSG.
  618. * Only use this macro at the start of a function that has not been implemented yet, or
  619. * if more complex error detection or recovery is required.
  620. *
  621. * Prints `m_msg` and notifies the editor.
  622. */
  623. #define ERR_PRINT_ED(m_msg) \
  624. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_msg, )
  625. /**
  626. * Prints `m_msg` once during the application lifetime.
  627. */
  628. #define ERR_PRINT_ONCE(m_msg) \
  629. if (true) { \
  630. static bool first_print = true; \
  631. if (first_print) { \
  632. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_msg); \
  633. first_print = false; \
  634. } \
  635. } else \
  636. ((void)0)
  637. /**
  638. * Prints `m_msg` and notifies the editor once during the application lifetime.
  639. */
  640. #define ERR_PRINT_ONCE_ED(m_msg) \
  641. if (true) { \
  642. static bool first_print = true; \
  643. if (first_print) { \
  644. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_msg, true); \
  645. first_print = false; \
  646. } \
  647. } else \
  648. ((void)0)
  649. // Print warning message macros.
  650. /**
  651. * Prints `m_msg`.
  652. *
  653. * If warning about deprecated usage, use `WARN_DEPRECATED` or `WARN_DEPRECATED_MSG` instead.
  654. */
  655. #define WARN_PRINT(m_msg) \
  656. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_msg, false, ERR_HANDLER_WARNING)
  657. /**
  658. * Prints `m_msg` and notifies the editor.
  659. *
  660. * If warning about deprecated usage, use `WARN_DEPRECATED` or `WARN_DEPRECATED_MSG` instead.
  661. */
  662. #define WARN_PRINT_ED(m_msg) \
  663. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_msg, true, ERR_HANDLER_WARNING)
  664. /**
  665. * Prints `m_msg` once during the application lifetime.
  666. *
  667. * If warning about deprecated usage, use `WARN_DEPRECATED` or `WARN_DEPRECATED_MSG` instead.
  668. */
  669. #define WARN_PRINT_ONCE(m_msg) \
  670. if (true) { \
  671. static bool first_print = true; \
  672. if (first_print) { \
  673. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_msg, false, ERR_HANDLER_WARNING); \
  674. first_print = false; \
  675. } \
  676. } else \
  677. ((void)0)
  678. /**
  679. * Prints `m_msg` and notifies the editor once during the application lifetime.
  680. *
  681. * If warning about deprecated usage, use `WARN_DEPRECATED` or `WARN_DEPRECATED_MSG` instead.
  682. */
  683. #define WARN_PRINT_ONCE_ED(m_msg) \
  684. if (true) { \
  685. static bool first_print = true; \
  686. if (first_print) { \
  687. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_msg, true, ERR_HANDLER_WARNING); \
  688. first_print = false; \
  689. } \
  690. } else \
  691. ((void)0)
  692. // Print deprecated warning message macros.
  693. /**
  694. * Warns that the current function is deprecated.
  695. */
  696. #define WARN_DEPRECATED \
  697. if (true) { \
  698. static SafeFlag warning_shown; \
  699. if (!warning_shown.is_set()) { \
  700. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "This method has been deprecated and will be removed in the future.", false, ERR_HANDLER_WARNING); \
  701. warning_shown.set(); \
  702. } \
  703. } else \
  704. ((void)0)
  705. /**
  706. * Warns that the current function is deprecated and prints `m_msg`.
  707. */
  708. #define WARN_DEPRECATED_MSG(m_msg) \
  709. if (true) { \
  710. static SafeFlag warning_shown; \
  711. if (!warning_shown.is_set()) { \
  712. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "This method has been deprecated and will be removed in the future.", m_msg, false, ERR_HANDLER_WARNING); \
  713. warning_shown.set(); \
  714. } \
  715. } else \
  716. ((void)0)
  717. /**
  718. * Do not use.
  719. * If the application should never reach this point use CRASH_NOW_MSG(m_msg) to explain why.
  720. *
  721. * The application crashes.
  722. */
  723. #define CRASH_NOW() \
  724. if (true) { \
  725. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "FATAL: Method/function failed."); \
  726. _err_flush_stdout(); \
  727. GENERATE_TRAP(); \
  728. } else \
  729. ((void)0)
  730. /**
  731. * Only use if the application should never reach this point.
  732. *
  733. * Prints `m_msg`, and then the application crashes.
  734. */
  735. #define CRASH_NOW_MSG(m_msg) \
  736. if (true) { \
  737. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "FATAL: Method/function failed.", m_msg); \
  738. _err_flush_stdout(); \
  739. GENERATE_TRAP(); \
  740. } else \
  741. ((void)0)
  742. /**
  743. * This should be a 'free' assert for program flow and should not be needed in any releases,
  744. * only used in dev builds.
  745. */
  746. #ifdef DEV_ENABLED
  747. #define DEV_ASSERT(m_cond) \
  748. if (unlikely(!(m_cond))) { \
  749. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "FATAL: DEV_ASSERT failed \"" _STR(m_cond) "\" is false."); \
  750. _err_flush_stdout(); \
  751. GENERATE_TRAP(); \
  752. } else \
  753. ((void)0)
  754. #else
  755. #define DEV_ASSERT(m_cond)
  756. #endif
  757. #endif // ERROR_MACROS_H