error_macros.h 50 KB

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