error_macros.h 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  1. /**************************************************************************/
  2. /* error_macros.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef ERROR_MACROS_H
  31. #define ERROR_MACROS_H
  32. #include "core/object/object_id.h"
  33. #include "core/typedefs.h"
  34. #include <atomic> // We'd normally use safe_refcount.h, but that would cause circular includes.
  35. class String;
  36. enum ErrorHandlerType {
  37. ERR_HANDLER_ERROR,
  38. ERR_HANDLER_WARNING,
  39. ERR_HANDLER_SCRIPT,
  40. ERR_HANDLER_SHADER,
  41. };
  42. // Pointer to the error handler printing function. Reassign to any function to have errors printed.
  43. // Parameters: userdata, function, file, line, error, explanation, type.
  44. typedef void (*ErrorHandlerFunc)(void *, const char *, const char *, int p_line, const char *, const char *, bool p_editor_notify, ErrorHandlerType p_type);
  45. struct ErrorHandlerList {
  46. ErrorHandlerFunc errfunc = nullptr;
  47. void *userdata = nullptr;
  48. ErrorHandlerList *next = nullptr;
  49. ErrorHandlerList() {}
  50. };
  51. void add_error_handler(ErrorHandlerList *p_handler);
  52. void remove_error_handler(const ErrorHandlerList *p_handler);
  53. // Functions used by the error macros.
  54. 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);
  55. 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);
  56. 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);
  57. 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);
  58. 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);
  59. 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);
  60. void _err_print_error_asap(const String &p_error, ErrorHandlerType p_type = ERR_HANDLER_ERROR);
  61. 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);
  62. 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);
  63. void _err_flush_stdout();
  64. void _physics_interpolation_warning(const char *p_function, const char *p_file, int p_line, ObjectID p_id, const char *p_warn_string);
  65. #ifdef __GNUC__
  66. //#define FUNCTION_STR __PRETTY_FUNCTION__ - too annoying
  67. #define FUNCTION_STR __FUNCTION__
  68. #else
  69. #define FUNCTION_STR __FUNCTION__
  70. #endif
  71. #ifdef _MSC_VER
  72. /**
  73. * Don't use GENERATE_TRAP() directly, should only be used be the macros below.
  74. */
  75. #define GENERATE_TRAP() __debugbreak()
  76. #else
  77. /**
  78. * Don't use GENERATE_TRAP() directly, should only be used be the macros below.
  79. */
  80. #define GENERATE_TRAP() __builtin_trap()
  81. #endif
  82. /**
  83. * Error macros.
  84. * WARNING: These macros work in the opposite way to assert().
  85. *
  86. * Unlike exceptions and asserts, these macros try to maintain consistency and stability.
  87. * In most cases, bugs and/or invalid data are not fatal. They should never allow a perfectly
  88. * running application to fail or crash.
  89. * Always try to return processable data, so the engine can keep running well.
  90. * Use the _MSG versions to print a meaningful message to help with debugging.
  91. *
  92. * The `((void)0)` no-op statement is used as a trick to force us to put a semicolon after
  93. * those macros, making them look like proper statements.
  94. * The if wrappers are used to ensure that the macro replacement does not trigger unexpected
  95. * issues when expanded e.g. after an `if (cond) ERR_FAIL();` without braces.
  96. */
  97. // Index out of bounds error macros.
  98. // These macros should be used instead of `ERR_FAIL_COND` for bounds checking.
  99. // Integer index out of bounds error macros.
  100. /**
  101. * Try using `ERR_FAIL_INDEX_MSG`.
  102. * Only use this macro if there is no sensible error message.
  103. *
  104. * Ensures an integer index `m_index` is less than `m_size` and greater than or equal to 0.
  105. * If not, the current function returns.
  106. */
  107. #define ERR_FAIL_INDEX(m_index, m_size) \
  108. if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
  109. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size)); \
  110. return; \
  111. } else \
  112. ((void)0)
  113. /**
  114. * Ensures an integer index `m_index` is less than `m_size` and greater than or equal to 0.
  115. * If not, prints `m_msg` and the current function returns.
  116. */
  117. #define ERR_FAIL_INDEX_MSG(m_index, m_size, m_msg) \
  118. if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
  119. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), m_msg); \
  120. return; \
  121. } else \
  122. ((void)0)
  123. /**
  124. * Same as `ERR_FAIL_INDEX_MSG` but also notifies the editor.
  125. */
  126. #define ERR_FAIL_INDEX_EDMSG(m_index, m_size, m_msg) \
  127. if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
  128. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), m_msg, true); \
  129. return; \
  130. } else \
  131. ((void)0)
  132. /**
  133. * Try using `ERR_FAIL_INDEX_V_MSG`.
  134. * Only use this macro if there is no sensible error message.
  135. *
  136. * Ensures an integer index `m_index` is less than `m_size` and greater than or equal to 0.
  137. * If not, the current function returns `m_retval`.
  138. */
  139. #define ERR_FAIL_INDEX_V(m_index, m_size, m_retval) \
  140. if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
  141. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size)); \
  142. return m_retval; \
  143. } else \
  144. ((void)0)
  145. /**
  146. * Ensures an integer index `m_index` is less than `m_size` and greater than or equal to 0.
  147. * If not, prints `m_msg` and the current function returns `m_retval`.
  148. */
  149. #define ERR_FAIL_INDEX_V_MSG(m_index, m_size, m_retval, m_msg) \
  150. if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
  151. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), m_msg); \
  152. return m_retval; \
  153. } else \
  154. ((void)0)
  155. /**
  156. * Same as `ERR_FAIL_INDEX_V_MSG` but also notifies the editor.
  157. */
  158. #define ERR_FAIL_INDEX_V_EDMSG(m_index, m_size, m_retval, m_msg) \
  159. if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
  160. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), m_msg, true); \
  161. return m_retval; \
  162. } else \
  163. ((void)0)
  164. /**
  165. * Try using `ERR_FAIL_INDEX_MSG` or `ERR_FAIL_INDEX_V_MSG`.
  166. * Only use this macro if there is no sensible fallback i.e. the error is unrecoverable, and
  167. * there is no sensible error message.
  168. *
  169. * Ensures an integer index `m_index` is less than `m_size` and greater than or equal to 0.
  170. * If not, the application crashes.
  171. */
  172. #define CRASH_BAD_INDEX(m_index, m_size) \
  173. if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
  174. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), "", false, true); \
  175. _err_flush_stdout(); \
  176. GENERATE_TRAP(); \
  177. } else \
  178. ((void)0)
  179. /**
  180. * Try using `ERR_FAIL_INDEX_MSG` or `ERR_FAIL_INDEX_V_MSG`.
  181. * Only use this macro if there is no sensible fallback i.e. the error is unrecoverable.
  182. *
  183. * Ensures an integer index `m_index` is less than `m_size` and greater than or equal to 0.
  184. * If not, prints `m_msg` and the application crashes.
  185. */
  186. #define CRASH_BAD_INDEX_MSG(m_index, m_size, m_msg) \
  187. if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
  188. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), m_msg, false, true); \
  189. _err_flush_stdout(); \
  190. GENERATE_TRAP(); \
  191. } else \
  192. ((void)0)
  193. // Unsigned integer index out of bounds error macros.
  194. /**
  195. * Try using `ERR_FAIL_UNSIGNED_INDEX_MSG`.
  196. * Only use this macro if there is no sensible error message.
  197. *
  198. * Ensures an unsigned integer index `m_index` is less than `m_size`.
  199. * If not, the current function returns.
  200. */
  201. #define ERR_FAIL_UNSIGNED_INDEX(m_index, m_size) \
  202. if (unlikely((m_index) >= (m_size))) { \
  203. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size)); \
  204. return; \
  205. } else \
  206. ((void)0)
  207. /**
  208. * Ensures an unsigned integer index `m_index` is less than `m_size`.
  209. * If not, prints `m_msg` and the current function returns.
  210. */
  211. #define ERR_FAIL_UNSIGNED_INDEX_MSG(m_index, m_size, m_msg) \
  212. if (unlikely((m_index) >= (m_size))) { \
  213. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), m_msg); \
  214. return; \
  215. } else \
  216. ((void)0)
  217. /**
  218. * Same as `ERR_FAIL_UNSIGNED_INDEX_MSG` but also notifies the editor.
  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. * Same as `ERR_FAIL_UNSIGNED_INDEX_V_EDMSG` but also notifies the editor.
  251. */
  252. #define ERR_FAIL_UNSIGNED_INDEX_V_EDMSG(m_index, m_size, m_retval, m_msg) \
  253. if (unlikely((m_index) >= (m_size))) { \
  254. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), m_msg, true); \
  255. return m_retval; \
  256. } else \
  257. ((void)0)
  258. /**
  259. * Try using `ERR_FAIL_UNSIGNED_INDEX_MSG` or `ERR_FAIL_UNSIGNED_INDEX_V_MSG`.
  260. * Only use this macro if there is no sensible fallback i.e. the error is unrecoverable, and
  261. * there is no sensible error message.
  262. *
  263. * Ensures an unsigned integer index `m_index` is less than `m_size`.
  264. * If not, the application crashes.
  265. */
  266. #define CRASH_BAD_UNSIGNED_INDEX(m_index, m_size) \
  267. if (unlikely((m_index) >= (m_size))) { \
  268. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), "", false, true); \
  269. _err_flush_stdout(); \
  270. GENERATE_TRAP(); \
  271. } else \
  272. ((void)0)
  273. /**
  274. * Try using `ERR_FAIL_UNSIGNED_INDEX_MSG` or `ERR_FAIL_UNSIGNED_INDEX_V_MSG`.
  275. * Only use this macro if there is no sensible fallback i.e. the error is unrecoverable.
  276. *
  277. * Ensures an unsigned integer index `m_index` is less than `m_size`.
  278. * If not, prints `m_msg` and the application crashes.
  279. */
  280. #define CRASH_BAD_UNSIGNED_INDEX_MSG(m_index, m_size, m_msg) \
  281. if (unlikely((m_index) >= (m_size))) { \
  282. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), m_msg, false, true); \
  283. _err_flush_stdout(); \
  284. GENERATE_TRAP(); \
  285. } else \
  286. ((void)0)
  287. // Null reference error macros.
  288. /**
  289. * Try using `ERR_FAIL_NULL_MSG`.
  290. * Only use this macro if there is no sensible error message.
  291. *
  292. * Ensures a pointer `m_param` is not null.
  293. * If it is null, the current function returns.
  294. */
  295. #define ERR_FAIL_NULL(m_param) \
  296. if (unlikely(m_param == nullptr)) { \
  297. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Parameter \"" _STR(m_param) "\" is null."); \
  298. return; \
  299. } else \
  300. ((void)0)
  301. /**
  302. * Ensures a pointer `m_param` is not null.
  303. * If it is null, prints `m_msg` and the current function returns.
  304. */
  305. #define ERR_FAIL_NULL_MSG(m_param, m_msg) \
  306. if (unlikely(m_param == nullptr)) { \
  307. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Parameter \"" _STR(m_param) "\" is null.", m_msg); \
  308. return; \
  309. } else \
  310. ((void)0)
  311. /**
  312. * Same as `ERR_FAIL_NULL_MSG` but also notifies the editor.
  313. */
  314. #define ERR_FAIL_NULL_EDMSG(m_param, m_msg) \
  315. if (unlikely(m_param == nullptr)) { \
  316. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Parameter \"" _STR(m_param) "\" is null.", m_msg, true); \
  317. return; \
  318. } else \
  319. ((void)0)
  320. /**
  321. * Try using `ERR_FAIL_NULL_V_MSG`.
  322. * Only use this macro if there is no sensible error message.
  323. *
  324. * Ensures a pointer `m_param` is not null.
  325. * If it is null, the current function returns `m_retval`.
  326. */
  327. #define ERR_FAIL_NULL_V(m_param, m_retval) \
  328. if (unlikely(m_param == nullptr)) { \
  329. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Parameter \"" _STR(m_param) "\" is null."); \
  330. return m_retval; \
  331. } else \
  332. ((void)0)
  333. /**
  334. * Ensures a pointer `m_param` is not null.
  335. * If it is null, prints `m_msg` and the current function returns `m_retval`.
  336. */
  337. #define ERR_FAIL_NULL_V_MSG(m_param, m_retval, m_msg) \
  338. if (unlikely(m_param == nullptr)) { \
  339. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Parameter \"" _STR(m_param) "\" is null.", m_msg); \
  340. return m_retval; \
  341. } else \
  342. ((void)0)
  343. /**
  344. * Same as `ERR_FAIL_NULL_V_MSG` but also notifies the editor.
  345. */
  346. #define ERR_FAIL_NULL_V_EDMSG(m_param, m_retval, m_msg) \
  347. if (unlikely(m_param == nullptr)) { \
  348. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Parameter \"" _STR(m_param) "\" is null.", m_msg, true); \
  349. return m_retval; \
  350. } else \
  351. ((void)0)
  352. /**
  353. * Try using `ERR_FAIL_COND_MSG`.
  354. * Only use this macro if there is no sensible error message.
  355. * If checking for null use ERR_FAIL_NULL_MSG instead.
  356. * If checking index bounds use ERR_FAIL_INDEX_MSG instead.
  357. *
  358. * Ensures `m_cond` is false.
  359. * If `m_cond` is true, the current function returns.
  360. */
  361. #define ERR_FAIL_COND(m_cond) \
  362. if (unlikely(m_cond)) { \
  363. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true."); \
  364. return; \
  365. } else \
  366. ((void)0)
  367. /**
  368. * Ensures `m_cond` is false.
  369. * If `m_cond` is true, prints `m_msg` and the current function returns.
  370. *
  371. * If checking for null use ERR_FAIL_NULL_MSG instead.
  372. * If checking index bounds use ERR_FAIL_INDEX_MSG instead.
  373. */
  374. #define ERR_FAIL_COND_MSG(m_cond, m_msg) \
  375. if (unlikely(m_cond)) { \
  376. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true.", m_msg); \
  377. return; \
  378. } else \
  379. ((void)0)
  380. /**
  381. * Same as `ERR_FAIL_COND_MSG` but also notifies the editor.
  382. */
  383. #define ERR_FAIL_COND_EDMSG(m_cond, m_msg) \
  384. if (unlikely(m_cond)) { \
  385. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true.", m_msg, true); \
  386. return; \
  387. } else \
  388. ((void)0)
  389. /**
  390. * Try using `ERR_FAIL_COND_V_MSG`.
  391. * Only use this macro if there is no sensible error message.
  392. * If checking for null use ERR_FAIL_NULL_V_MSG instead.
  393. * If checking index bounds use ERR_FAIL_INDEX_V_MSG instead.
  394. *
  395. * Ensures `m_cond` is false.
  396. * If `m_cond` is true, the current function returns `m_retval`.
  397. */
  398. #define ERR_FAIL_COND_V(m_cond, m_retval) \
  399. if (unlikely(m_cond)) { \
  400. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Returning: " _STR(m_retval)); \
  401. return m_retval; \
  402. } else \
  403. ((void)0)
  404. /**
  405. * Ensures `m_cond` is false.
  406. * If `m_cond` is true, prints `m_msg` and the current function returns `m_retval`.
  407. *
  408. * If checking for null use ERR_FAIL_NULL_V_MSG instead.
  409. * If checking index bounds use ERR_FAIL_INDEX_V_MSG instead.
  410. */
  411. #define ERR_FAIL_COND_V_MSG(m_cond, m_retval, m_msg) \
  412. if (unlikely(m_cond)) { \
  413. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Returning: " _STR(m_retval), m_msg); \
  414. return m_retval; \
  415. } else \
  416. ((void)0)
  417. /**
  418. * Same as `ERR_FAIL_COND_V_MSG` but also notifies the editor.
  419. */
  420. #define ERR_FAIL_COND_V_EDMSG(m_cond, m_retval, m_msg) \
  421. if (unlikely(m_cond)) { \
  422. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Returning: " _STR(m_retval), m_msg, true); \
  423. return m_retval; \
  424. } else \
  425. ((void)0)
  426. /**
  427. * Try using `ERR_CONTINUE_MSG`.
  428. * Only use this macro if there is no sensible error message.
  429. *
  430. * Ensures `m_cond` is false.
  431. * If `m_cond` is true, the current loop continues.
  432. */
  433. #define ERR_CONTINUE(m_cond) \
  434. if (unlikely(m_cond)) { \
  435. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Continuing."); \
  436. continue; \
  437. } else \
  438. ((void)0)
  439. /**
  440. * Ensures `m_cond` is false.
  441. * If `m_cond` is true, prints `m_msg` and the current loop continues.
  442. */
  443. #define ERR_CONTINUE_MSG(m_cond, m_msg) \
  444. if (unlikely(m_cond)) { \
  445. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Continuing.", m_msg); \
  446. continue; \
  447. } else \
  448. ((void)0)
  449. /**
  450. * Same as `ERR_CONTINUE_MSG` but also notifies the editor.
  451. */
  452. #define ERR_CONTINUE_EDMSG(m_cond, m_msg) \
  453. if (unlikely(m_cond)) { \
  454. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Continuing.", m_msg, true); \
  455. continue; \
  456. } else \
  457. ((void)0)
  458. /**
  459. * Try using `ERR_BREAK_MSG`.
  460. * Only use this macro if there is no sensible error message.
  461. *
  462. * Ensures `m_cond` is false.
  463. * If `m_cond` is true, the current loop breaks.
  464. */
  465. #define ERR_BREAK(m_cond) \
  466. if (unlikely(m_cond)) { \
  467. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Breaking."); \
  468. break; \
  469. } else \
  470. ((void)0)
  471. /**
  472. * Ensures `m_cond` is false.
  473. * If `m_cond` is true, prints `m_msg` and the current loop breaks.
  474. */
  475. #define ERR_BREAK_MSG(m_cond, m_msg) \
  476. if (unlikely(m_cond)) { \
  477. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Breaking.", m_msg); \
  478. break; \
  479. } else \
  480. ((void)0)
  481. /**
  482. * Same as `ERR_BREAK_MSG` but also notifies the editor.
  483. */
  484. #define ERR_BREAK_EDMSG(m_cond, m_msg) \
  485. if (unlikely(m_cond)) { \
  486. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Breaking.", m_msg, true); \
  487. break; \
  488. } else \
  489. ((void)0)
  490. /**
  491. * Try using `ERR_FAIL_COND_MSG` or `ERR_FAIL_COND_V_MSG`.
  492. * Only use this macro if there is no sensible fallback i.e. the error is unrecoverable, and
  493. * there is no sensible error message.
  494. *
  495. * Ensures `m_cond` is false.
  496. * If `m_cond` is true, the application crashes.
  497. */
  498. #define CRASH_COND(m_cond) \
  499. if (unlikely(m_cond)) { \
  500. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "FATAL: Condition \"" _STR(m_cond) "\" is true."); \
  501. _err_flush_stdout(); \
  502. GENERATE_TRAP(); \
  503. } else \
  504. ((void)0)
  505. /**
  506. * Try using `ERR_FAIL_COND_MSG` or `ERR_FAIL_COND_V_MSG`.
  507. * Only use this macro if there is no sensible fallback i.e. the error is unrecoverable.
  508. *
  509. * Ensures `m_cond` is false.
  510. * If `m_cond` is true, prints `m_msg` and the application crashes.
  511. */
  512. #define CRASH_COND_MSG(m_cond, m_msg) \
  513. if (unlikely(m_cond)) { \
  514. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "FATAL: Condition \"" _STR(m_cond) "\" is true.", m_msg); \
  515. _err_flush_stdout(); \
  516. GENERATE_TRAP(); \
  517. } else \
  518. ((void)0)
  519. // Generic error macros.
  520. /**
  521. * Try using `ERR_FAIL_COND_MSG` or `ERR_FAIL_MSG`.
  522. * Only use this macro if more complex error detection or recovery is required, and
  523. * there is no sensible error message.
  524. *
  525. * The current function returns.
  526. */
  527. #define ERR_FAIL() \
  528. if (true) { \
  529. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Method/function failed."); \
  530. return; \
  531. } else \
  532. ((void)0)
  533. /**
  534. * Try using `ERR_FAIL_COND_MSG`.
  535. * Only use this macro if more complex error detection or recovery is required.
  536. *
  537. * Prints `m_msg`, and the current function returns.
  538. */
  539. #define ERR_FAIL_MSG(m_msg) \
  540. if (true) { \
  541. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Method/function failed.", m_msg); \
  542. return; \
  543. } else \
  544. ((void)0)
  545. /**
  546. * Same as `ERR_FAIL_MSG` but also notifies the editor.
  547. */
  548. #define ERR_FAIL_EDMSG(m_msg) \
  549. if (true) { \
  550. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Method/function failed.", m_msg, true); \
  551. return; \
  552. } else \
  553. ((void)0)
  554. /**
  555. * Try using `ERR_FAIL_COND_V_MSG` or `ERR_FAIL_V_MSG`.
  556. * Only use this macro if more complex error detection or recovery is required, and
  557. * there is no sensible error message.
  558. *
  559. * The current function returns `m_retval`.
  560. */
  561. #define ERR_FAIL_V(m_retval) \
  562. if (true) { \
  563. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Method/function failed. Returning: " _STR(m_retval)); \
  564. return m_retval; \
  565. } else \
  566. ((void)0)
  567. /**
  568. * Try using `ERR_FAIL_COND_V_MSG`.
  569. * Only use this macro if more complex error detection or recovery is required.
  570. *
  571. * Prints `m_msg`, and the current function returns `m_retval`.
  572. */
  573. #define ERR_FAIL_V_MSG(m_retval, m_msg) \
  574. if (true) { \
  575. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Method/function failed. Returning: " _STR(m_retval), m_msg); \
  576. return m_retval; \
  577. } else \
  578. ((void)0)
  579. /**
  580. * Same as `ERR_FAIL_V_MSG` but also notifies the editor.
  581. */
  582. #define ERR_FAIL_V_EDMSG(m_retval, m_msg) \
  583. if (true) { \
  584. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Method/function failed. Returning: " _STR(m_retval), m_msg, true); \
  585. return m_retval; \
  586. } else \
  587. ((void)0)
  588. /**
  589. * Try using `ERR_FAIL_COND_MSG`, `ERR_FAIL_COND_V_MSG`, `ERR_CONTINUE_MSG` or `ERR_BREAK_MSG`.
  590. * Only use this macro at the start of a function that has not been implemented yet, or
  591. * if more complex error detection or recovery is required.
  592. *
  593. * Prints `m_msg`.
  594. */
  595. #define ERR_PRINT(m_msg) \
  596. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_msg)
  597. /**
  598. * Same as `ERR_PRINT` but also notifies the editor.
  599. */
  600. #define ERR_PRINT_ED(m_msg) \
  601. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_msg, true)
  602. /**
  603. * Prints `m_msg` once during the application lifetime.
  604. */
  605. #define ERR_PRINT_ONCE(m_msg) \
  606. if (true) { \
  607. static bool first_print = true; \
  608. if (first_print) { \
  609. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_msg); \
  610. first_print = false; \
  611. } \
  612. } else \
  613. ((void)0)
  614. /**
  615. * Same as `ERR_PRINT_ONCE` but also notifies the editor.
  616. */
  617. #define ERR_PRINT_ONCE_ED(m_msg) \
  618. if (true) { \
  619. static bool first_print = true; \
  620. if (first_print) { \
  621. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_msg, true); \
  622. first_print = false; \
  623. } \
  624. } else \
  625. ((void)0)
  626. // Print warning message macros.
  627. /**
  628. * Prints `m_msg`.
  629. *
  630. * If warning about deprecated usage, use `WARN_DEPRECATED` or `WARN_DEPRECATED_MSG` instead.
  631. */
  632. #define WARN_PRINT(m_msg) \
  633. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_msg, false, ERR_HANDLER_WARNING)
  634. /**
  635. * Same as `WARN_PRINT` but also notifies the editor.
  636. */
  637. #define WARN_PRINT_ED(m_msg) \
  638. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_msg, true, ERR_HANDLER_WARNING)
  639. /**
  640. * Prints `m_msg` once during the application lifetime.
  641. *
  642. * If warning about deprecated usage, use `WARN_DEPRECATED` or `WARN_DEPRECATED_MSG` instead.
  643. */
  644. #define WARN_PRINT_ONCE(m_msg) \
  645. if (true) { \
  646. static bool first_print = true; \
  647. if (first_print) { \
  648. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_msg, false, ERR_HANDLER_WARNING); \
  649. first_print = false; \
  650. } \
  651. } else \
  652. ((void)0)
  653. /**
  654. * Same as `WARN_PRINT_ONCE` but also notifies the editor.
  655. */
  656. #define WARN_PRINT_ONCE_ED(m_msg) \
  657. if (true) { \
  658. static bool first_print = true; \
  659. if (first_print) { \
  660. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_msg, true, ERR_HANDLER_WARNING); \
  661. first_print = false; \
  662. } \
  663. } else \
  664. ((void)0)
  665. /**
  666. * Warns about `m_msg` only when verbose mode is enabled.
  667. */
  668. #define WARN_VERBOSE(m_msg) \
  669. { \
  670. if (is_print_verbose_enabled()) { \
  671. WARN_PRINT(m_msg); \
  672. } \
  673. }
  674. // Print deprecated warning message macros.
  675. /**
  676. * Warns that the current function is deprecated.
  677. */
  678. #define WARN_DEPRECATED \
  679. if (true) { \
  680. static std::atomic<bool> warning_shown; \
  681. if (!warning_shown.load()) { \
  682. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "This method has been deprecated and will be removed in the future.", false, ERR_HANDLER_WARNING); \
  683. warning_shown.store(true); \
  684. } \
  685. } else \
  686. ((void)0)
  687. /**
  688. * Warns that the current function is deprecated and prints `m_msg`.
  689. */
  690. #define WARN_DEPRECATED_MSG(m_msg) \
  691. if (true) { \
  692. static std::atomic<bool> warning_shown; \
  693. if (!warning_shown.load()) { \
  694. _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); \
  695. warning_shown.store(true); \
  696. } \
  697. } else \
  698. ((void)0)
  699. /**
  700. * Do not use.
  701. * If the application should never reach this point use CRASH_NOW_MSG(m_msg) to explain why.
  702. *
  703. * The application crashes.
  704. */
  705. #define CRASH_NOW() \
  706. if (true) { \
  707. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "FATAL: Method/function failed."); \
  708. _err_flush_stdout(); \
  709. GENERATE_TRAP(); \
  710. } else \
  711. ((void)0)
  712. /**
  713. * Only use if the application should never reach this point.
  714. *
  715. * Prints `m_msg`, and then the application crashes.
  716. */
  717. #define CRASH_NOW_MSG(m_msg) \
  718. if (true) { \
  719. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "FATAL: Method/function failed.", m_msg); \
  720. _err_flush_stdout(); \
  721. GENERATE_TRAP(); \
  722. } else \
  723. ((void)0)
  724. /**
  725. * Note: IN MOST CASES YOU SHOULD NOT USE THIS MACRO.
  726. * Do not use unless you understand the trade-offs.
  727. *
  728. * DEV macros will be compiled out in releases, they are wrapped in DEV_ENABLED.
  729. *
  730. * Prefer WARNINGS / ERR_FAIL macros (which fail without crashing) - ERR_FAIL should be used in most cases.
  731. * Then CRASH_NOW_MSG macros (on rare occasions where error cannot be recovered).
  732. *
  733. * DEV_ASSERT should generally only be used when both of the following conditions are met:
  734. * 1) Bottleneck code where a check in release would be too expensive.
  735. * 2) Situations where the check would fail obviously and straight away during the maintenance of the code
  736. * (i.e. strict conditions that should be true no matter what)
  737. * and that can't fail for other contributors once the code is finished and merged.
  738. */
  739. #ifdef DEV_ENABLED
  740. #define DEV_ASSERT(m_cond) \
  741. if (unlikely(!(m_cond))) { \
  742. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "FATAL: DEV_ASSERT failed \"" _STR(m_cond) "\" is false."); \
  743. _err_flush_stdout(); \
  744. GENERATE_TRAP(); \
  745. } else \
  746. ((void)0)
  747. #else
  748. #define DEV_ASSERT(m_cond)
  749. #endif
  750. #ifdef DEV_ENABLED
  751. #define DEV_CHECK_ONCE(m_cond) \
  752. if (unlikely(!(m_cond))) { \
  753. ERR_PRINT_ONCE("DEV_CHECK_ONCE failed \"" _STR(m_cond) "\" is false."); \
  754. } else \
  755. ((void)0)
  756. #else
  757. #define DEV_CHECK_ONCE(m_cond)
  758. #endif
  759. /**
  760. * Physics Interpolation warnings.
  761. * These are spam protection warnings.
  762. */
  763. #define PHYSICS_INTERPOLATION_NODE_WARNING(m_object_id, m_string) \
  764. _physics_interpolation_warning(FUNCTION_STR, __FILE__, __LINE__, m_object_id, m_string)
  765. #define PHYSICS_INTERPOLATION_WARNING(m_string) \
  766. _physics_interpolation_warning(FUNCTION_STR, __FILE__, __LINE__, ObjectID(UINT64_MAX), m_string)
  767. #endif // ERROR_MACROS_H