error_macros.h 47 KB

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