error_macros.h 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  1. /**************************************************************************/
  2. /* error_macros.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef ERROR_MACROS_H
  31. #define ERROR_MACROS_H
  32. #include "core/typedefs.h"
  33. #include <atomic> // We'd normally use safe_refcount.h, but that would cause circular includes.
  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. * Same as `ERR_FAIL_INDEX_MSG` but also notifies the editor.
  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. * Same as `ERR_FAIL_INDEX_V_MSG` but also notifies the editor.
  154. */
  155. #define ERR_FAIL_INDEX_V_EDMSG(m_index, m_size, m_retval, m_msg) \
  156. if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
  157. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), m_msg, true); \
  158. return m_retval; \
  159. } else \
  160. ((void)0)
  161. /**
  162. * Try using `ERR_FAIL_INDEX_MSG` or `ERR_FAIL_INDEX_V_MSG`.
  163. * Only use this macro if there is no sensible fallback i.e. the error is unrecoverable, and
  164. * there is no sensible error message.
  165. *
  166. * Ensures an integer index `m_index` is less than `m_size` and greater than or equal to 0.
  167. * If not, the application crashes.
  168. */
  169. #define CRASH_BAD_INDEX(m_index, m_size) \
  170. if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
  171. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), "", false, true); \
  172. _err_flush_stdout(); \
  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, false, true); \
  186. _err_flush_stdout(); \
  187. GENERATE_TRAP(); \
  188. } else \
  189. ((void)0)
  190. // Unsigned integer index out of bounds error macros.
  191. /**
  192. * Try using `ERR_FAIL_UNSIGNED_INDEX_MSG`.
  193. * Only use this macro if there is no sensible error message.
  194. *
  195. * Ensures an unsigned integer index `m_index` is less than `m_size`.
  196. * If not, the current function returns.
  197. */
  198. #define ERR_FAIL_UNSIGNED_INDEX(m_index, m_size) \
  199. if (unlikely((m_index) >= (m_size))) { \
  200. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size)); \
  201. return; \
  202. } else \
  203. ((void)0)
  204. /**
  205. * Ensures an unsigned integer index `m_index` is less than `m_size`.
  206. * If not, prints `m_msg` and the current function returns.
  207. */
  208. #define ERR_FAIL_UNSIGNED_INDEX_MSG(m_index, m_size, m_msg) \
  209. if (unlikely((m_index) >= (m_size))) { \
  210. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), m_msg); \
  211. return; \
  212. } else \
  213. ((void)0)
  214. /**
  215. * Same as `ERR_FAIL_UNSIGNED_INDEX_MSG` but also notifies the editor.
  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. * Same as `ERR_FAIL_UNSIGNED_INDEX_V_EDMSG` but also notifies the editor.
  248. */
  249. #define ERR_FAIL_UNSIGNED_INDEX_V_EDMSG(m_index, m_size, m_retval, m_msg) \
  250. if (unlikely((m_index) >= (m_size))) { \
  251. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), m_msg, true); \
  252. return m_retval; \
  253. } else \
  254. ((void)0)
  255. /**
  256. * Try using `ERR_FAIL_UNSIGNED_INDEX_MSG` or `ERR_FAIL_UNSIGNED_INDEX_V_MSG`.
  257. * Only use this macro if there is no sensible fallback i.e. the error is unrecoverable, and
  258. * there is no sensible error message.
  259. *
  260. * Ensures an unsigned integer index `m_index` is less than `m_size`.
  261. * If not, the application crashes.
  262. */
  263. #define CRASH_BAD_UNSIGNED_INDEX(m_index, m_size) \
  264. if (unlikely((m_index) >= (m_size))) { \
  265. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), "", false, true); \
  266. _err_flush_stdout(); \
  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, false, true); \
  280. _err_flush_stdout(); \
  281. GENERATE_TRAP(); \
  282. } else \
  283. ((void)0)
  284. // Null reference error macros.
  285. /**
  286. * Try using `ERR_FAIL_NULL_MSG`.
  287. * Only use this macro if there is no sensible error message.
  288. *
  289. * Ensures a pointer `m_param` is not null.
  290. * If it is null, the current function returns.
  291. */
  292. #define ERR_FAIL_NULL(m_param) \
  293. if (unlikely(m_param == nullptr)) { \
  294. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Parameter \"" _STR(m_param) "\" is null."); \
  295. return; \
  296. } else \
  297. ((void)0)
  298. /**
  299. * Ensures a pointer `m_param` is not null.
  300. * If it is null, prints `m_msg` and the current function returns.
  301. */
  302. #define ERR_FAIL_NULL_MSG(m_param, m_msg) \
  303. if (unlikely(m_param == nullptr)) { \
  304. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Parameter \"" _STR(m_param) "\" is null.", m_msg); \
  305. return; \
  306. } else \
  307. ((void)0)
  308. /**
  309. * Same as `ERR_FAIL_NULL_MSG` but also notifies the editor.
  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. * Same as `ERR_FAIL_NULL_V_MSG` but also notifies the editor.
  342. */
  343. #define ERR_FAIL_NULL_V_EDMSG(m_param, m_retval, m_msg) \
  344. if (unlikely(m_param == nullptr)) { \
  345. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Parameter \"" _STR(m_param) "\" is null.", m_msg, true); \
  346. return m_retval; \
  347. } else \
  348. ((void)0)
  349. /**
  350. * Try using `ERR_FAIL_COND_MSG`.
  351. * Only use this macro if there is no sensible error message.
  352. * If checking for null use ERR_FAIL_NULL_MSG instead.
  353. * If checking index bounds use ERR_FAIL_INDEX_MSG instead.
  354. *
  355. * Ensures `m_cond` is false.
  356. * If `m_cond` is true, the current function returns.
  357. */
  358. #define ERR_FAIL_COND(m_cond) \
  359. if (unlikely(m_cond)) { \
  360. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true."); \
  361. return; \
  362. } else \
  363. ((void)0)
  364. /**
  365. * Ensures `m_cond` is false.
  366. * If `m_cond` is true, prints `m_msg` and the current function returns.
  367. *
  368. * If checking for null use ERR_FAIL_NULL_MSG instead.
  369. * If checking index bounds use ERR_FAIL_INDEX_MSG instead.
  370. */
  371. #define ERR_FAIL_COND_MSG(m_cond, m_msg) \
  372. if (unlikely(m_cond)) { \
  373. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true.", m_msg); \
  374. return; \
  375. } else \
  376. ((void)0)
  377. /**
  378. * Same as `ERR_FAIL_COND_MSG` but also notifies the editor.
  379. */
  380. #define ERR_FAIL_COND_EDMSG(m_cond, m_msg) \
  381. if (unlikely(m_cond)) { \
  382. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true.", m_msg, true); \
  383. return; \
  384. } else \
  385. ((void)0)
  386. /**
  387. * Try using `ERR_FAIL_COND_V_MSG`.
  388. * Only use this macro if there is no sensible error message.
  389. * If checking for null use ERR_FAIL_NULL_V_MSG instead.
  390. * If checking index bounds use ERR_FAIL_INDEX_V_MSG instead.
  391. *
  392. * Ensures `m_cond` is false.
  393. * If `m_cond` is true, the current function returns `m_retval`.
  394. */
  395. #define ERR_FAIL_COND_V(m_cond, m_retval) \
  396. if (unlikely(m_cond)) { \
  397. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Returning: " _STR(m_retval)); \
  398. return m_retval; \
  399. } else \
  400. ((void)0)
  401. /**
  402. * Ensures `m_cond` is false.
  403. * If `m_cond` is true, prints `m_msg` and the current function returns `m_retval`.
  404. *
  405. * If checking for null use ERR_FAIL_NULL_V_MSG instead.
  406. * If checking index bounds use ERR_FAIL_INDEX_V_MSG instead.
  407. */
  408. #define ERR_FAIL_COND_V_MSG(m_cond, m_retval, m_msg) \
  409. if (unlikely(m_cond)) { \
  410. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Returning: " _STR(m_retval), m_msg); \
  411. return m_retval; \
  412. } else \
  413. ((void)0)
  414. /**
  415. * Same as `ERR_FAIL_COND_V_MSG` but also notifies the editor.
  416. */
  417. #define ERR_FAIL_COND_V_EDMSG(m_cond, m_retval, m_msg) \
  418. if (unlikely(m_cond)) { \
  419. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Returning: " _STR(m_retval), m_msg, true); \
  420. return m_retval; \
  421. } else \
  422. ((void)0)
  423. /**
  424. * Try using `ERR_CONTINUE_MSG`.
  425. * Only use this macro if there is no sensible error message.
  426. *
  427. * Ensures `m_cond` is false.
  428. * If `m_cond` is true, the current loop continues.
  429. */
  430. #define ERR_CONTINUE(m_cond) \
  431. if (unlikely(m_cond)) { \
  432. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Continuing."); \
  433. continue; \
  434. } else \
  435. ((void)0)
  436. /**
  437. * Ensures `m_cond` is false.
  438. * If `m_cond` is true, prints `m_msg` and the current loop continues.
  439. */
  440. #define ERR_CONTINUE_MSG(m_cond, m_msg) \
  441. if (unlikely(m_cond)) { \
  442. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Continuing.", m_msg); \
  443. continue; \
  444. } else \
  445. ((void)0)
  446. /**
  447. * Same as `ERR_CONTINUE_MSG` but also notifies the editor.
  448. */
  449. #define ERR_CONTINUE_EDMSG(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, true); \
  452. continue; \
  453. } else \
  454. ((void)0)
  455. /**
  456. * Try using `ERR_BREAK_MSG`.
  457. * Only use this macro if there is no sensible error message.
  458. *
  459. * Ensures `m_cond` is false.
  460. * If `m_cond` is true, the current loop breaks.
  461. */
  462. #define ERR_BREAK(m_cond) \
  463. if (unlikely(m_cond)) { \
  464. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Breaking."); \
  465. break; \
  466. } else \
  467. ((void)0)
  468. /**
  469. * Ensures `m_cond` is false.
  470. * If `m_cond` is true, prints `m_msg` and the current loop breaks.
  471. */
  472. #define ERR_BREAK_MSG(m_cond, m_msg) \
  473. if (unlikely(m_cond)) { \
  474. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Breaking.", m_msg); \
  475. break; \
  476. } else \
  477. ((void)0)
  478. /**
  479. * Same as `ERR_BREAK_MSG` but also notifies the editor.
  480. */
  481. #define ERR_BREAK_EDMSG(m_cond, m_msg) \
  482. if (unlikely(m_cond)) { \
  483. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Breaking.", m_msg, true); \
  484. break; \
  485. } else \
  486. ((void)0)
  487. /**
  488. * Try using `ERR_FAIL_COND_MSG` or `ERR_FAIL_COND_V_MSG`.
  489. * Only use this macro if there is no sensible fallback i.e. the error is unrecoverable, and
  490. * there is no sensible error message.
  491. *
  492. * Ensures `m_cond` is false.
  493. * If `m_cond` is true, the application crashes.
  494. */
  495. #define CRASH_COND(m_cond) \
  496. if (unlikely(m_cond)) { \
  497. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "FATAL: Condition \"" _STR(m_cond) "\" is true."); \
  498. _err_flush_stdout(); \
  499. GENERATE_TRAP(); \
  500. } else \
  501. ((void)0)
  502. /**
  503. * Try using `ERR_FAIL_COND_MSG` or `ERR_FAIL_COND_V_MSG`.
  504. * Only use this macro if there is no sensible fallback i.e. the error is unrecoverable.
  505. *
  506. * Ensures `m_cond` is false.
  507. * If `m_cond` is true, prints `m_msg` and the application crashes.
  508. */
  509. #define CRASH_COND_MSG(m_cond, m_msg) \
  510. if (unlikely(m_cond)) { \
  511. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "FATAL: Condition \"" _STR(m_cond) "\" is true.", m_msg); \
  512. _err_flush_stdout(); \
  513. GENERATE_TRAP(); \
  514. } else \
  515. ((void)0)
  516. // Generic error macros.
  517. /**
  518. * Try using `ERR_FAIL_COND_MSG` or `ERR_FAIL_MSG`.
  519. * Only use this macro if more complex error detection or recovery is required, and
  520. * there is no sensible error message.
  521. *
  522. * The current function returns.
  523. */
  524. #define ERR_FAIL() \
  525. if (true) { \
  526. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Method/function failed."); \
  527. return; \
  528. } else \
  529. ((void)0)
  530. /**
  531. * Try using `ERR_FAIL_COND_MSG`.
  532. * Only use this macro if more complex error detection or recovery is required.
  533. *
  534. * Prints `m_msg`, and the current function returns.
  535. */
  536. #define ERR_FAIL_MSG(m_msg) \
  537. if (true) { \
  538. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Method/function failed.", m_msg); \
  539. return; \
  540. } else \
  541. ((void)0)
  542. /**
  543. * Same as `ERR_FAIL_MSG` but also notifies the editor.
  544. */
  545. #define ERR_FAIL_EDMSG(m_msg) \
  546. if (true) { \
  547. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Method/function failed.", m_msg, true); \
  548. return; \
  549. } else \
  550. ((void)0)
  551. /**
  552. * Try using `ERR_FAIL_COND_V_MSG` or `ERR_FAIL_V_MSG`.
  553. * Only use this macro if more complex error detection or recovery is required, and
  554. * there is no sensible error message.
  555. *
  556. * The current function returns `m_retval`.
  557. */
  558. #define ERR_FAIL_V(m_retval) \
  559. if (true) { \
  560. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Method/function failed. Returning: " _STR(m_retval)); \
  561. return m_retval; \
  562. } else \
  563. ((void)0)
  564. /**
  565. * Try using `ERR_FAIL_COND_V_MSG`.
  566. * Only use this macro if more complex error detection or recovery is required.
  567. *
  568. * Prints `m_msg`, and the current function returns `m_retval`.
  569. */
  570. #define ERR_FAIL_V_MSG(m_retval, m_msg) \
  571. if (true) { \
  572. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Method/function failed. Returning: " _STR(m_retval), m_msg); \
  573. return m_retval; \
  574. } else \
  575. ((void)0)
  576. /**
  577. * Same as `ERR_FAIL_V_MSG` but also notifies the editor.
  578. */
  579. #define ERR_FAIL_V_EDMSG(m_retval, m_msg) \
  580. if (true) { \
  581. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Method/function failed. Returning: " _STR(m_retval), m_msg, true); \
  582. return m_retval; \
  583. } else \
  584. ((void)0)
  585. /**
  586. * Try using `ERR_FAIL_COND_MSG`, `ERR_FAIL_COND_V_MSG`, `ERR_CONTINUE_MSG` or `ERR_BREAK_MSG`.
  587. * Only use this macro at the start of a function that has not been implemented yet, or
  588. * if more complex error detection or recovery is required.
  589. *
  590. * Prints `m_msg`.
  591. */
  592. #define ERR_PRINT(m_msg) \
  593. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_msg)
  594. /**
  595. * Same as `ERR_PRINT` but also notifies the editor.
  596. */
  597. #define ERR_PRINT_ED(m_msg) \
  598. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_msg, true)
  599. /**
  600. * Prints `m_msg` once during the application lifetime.
  601. */
  602. #define ERR_PRINT_ONCE(m_msg) \
  603. if (true) { \
  604. static bool first_print = true; \
  605. if (first_print) { \
  606. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_msg); \
  607. first_print = false; \
  608. } \
  609. } else \
  610. ((void)0)
  611. /**
  612. * Same as `ERR_PRINT_ONCE` but also notifies the editor.
  613. */
  614. #define ERR_PRINT_ONCE_ED(m_msg) \
  615. if (true) { \
  616. static bool first_print = true; \
  617. if (first_print) { \
  618. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_msg, true); \
  619. first_print = false; \
  620. } \
  621. } else \
  622. ((void)0)
  623. // Print warning message macros.
  624. /**
  625. * Prints `m_msg`.
  626. *
  627. * If warning about deprecated usage, use `WARN_DEPRECATED` or `WARN_DEPRECATED_MSG` instead.
  628. */
  629. #define WARN_PRINT(m_msg) \
  630. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_msg, false, ERR_HANDLER_WARNING)
  631. /**
  632. * Same as `WARN_PRINT` but also notifies the editor.
  633. */
  634. #define WARN_PRINT_ED(m_msg) \
  635. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_msg, true, ERR_HANDLER_WARNING)
  636. /**
  637. * Prints `m_msg` once during the application lifetime.
  638. *
  639. * If warning about deprecated usage, use `WARN_DEPRECATED` or `WARN_DEPRECATED_MSG` instead.
  640. */
  641. #define WARN_PRINT_ONCE(m_msg) \
  642. if (true) { \
  643. static bool first_print = true; \
  644. if (first_print) { \
  645. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_msg, false, ERR_HANDLER_WARNING); \
  646. first_print = false; \
  647. } \
  648. } else \
  649. ((void)0)
  650. /**
  651. * Same as `WARN_PRINT_ONCE` but also notifies the editor.
  652. */
  653. #define WARN_PRINT_ONCE_ED(m_msg) \
  654. if (true) { \
  655. static bool first_print = true; \
  656. if (first_print) { \
  657. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_msg, true, ERR_HANDLER_WARNING); \
  658. first_print = false; \
  659. } \
  660. } else \
  661. ((void)0)
  662. /**
  663. * Warns about `m_msg` only when verbose mode is enabled.
  664. */
  665. #define WARN_VERBOSE(m_msg) \
  666. { \
  667. if (is_print_verbose_enabled()) { \
  668. WARN_PRINT(m_msg); \
  669. } \
  670. }
  671. // Print deprecated warning message macros.
  672. /**
  673. * Warns that the current function is deprecated.
  674. */
  675. #define WARN_DEPRECATED \
  676. if (true) { \
  677. static std::atomic<bool> warning_shown; \
  678. if (!warning_shown.load()) { \
  679. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "This method has been deprecated and will be removed in the future.", false, ERR_HANDLER_WARNING); \
  680. warning_shown.store(true); \
  681. } \
  682. } else \
  683. ((void)0)
  684. /**
  685. * Warns that the current function is deprecated and prints `m_msg`.
  686. */
  687. #define WARN_DEPRECATED_MSG(m_msg) \
  688. if (true) { \
  689. static std::atomic<bool> warning_shown; \
  690. if (!warning_shown.load()) { \
  691. _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); \
  692. warning_shown.store(true); \
  693. } \
  694. } else \
  695. ((void)0)
  696. /**
  697. * Do not use.
  698. * If the application should never reach this point use CRASH_NOW_MSG(m_msg) to explain why.
  699. *
  700. * The application crashes.
  701. */
  702. #define CRASH_NOW() \
  703. if (true) { \
  704. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "FATAL: Method/function failed."); \
  705. _err_flush_stdout(); \
  706. GENERATE_TRAP(); \
  707. } else \
  708. ((void)0)
  709. /**
  710. * Only use if the application should never reach this point.
  711. *
  712. * Prints `m_msg`, and then the application crashes.
  713. */
  714. #define CRASH_NOW_MSG(m_msg) \
  715. if (true) { \
  716. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "FATAL: Method/function failed.", m_msg); \
  717. _err_flush_stdout(); \
  718. GENERATE_TRAP(); \
  719. } else \
  720. ((void)0)
  721. /**
  722. * This should be a 'free' assert for program flow and should not be needed in any releases,
  723. * only used in dev builds.
  724. */
  725. #ifdef DEV_ENABLED
  726. #define DEV_ASSERT(m_cond) \
  727. if (unlikely(!(m_cond))) { \
  728. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "FATAL: DEV_ASSERT failed \"" _STR(m_cond) "\" is false."); \
  729. _err_flush_stdout(); \
  730. GENERATE_TRAP(); \
  731. } else \
  732. ((void)0)
  733. #else
  734. #define DEV_ASSERT(m_cond)
  735. #endif
  736. #endif // ERROR_MACROS_H