error_macros.h 50 KB

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