2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* error_macros.h */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
2019-01-01 12:53:14 +01:00
|
|
|
/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
|
2014-02-10 02:10:30 +01:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
#ifndef ERROR_MACROS_H
|
|
|
|
#define ERROR_MACROS_H
|
|
|
|
|
2018-09-11 18:13:45 +02:00
|
|
|
#include "core/typedefs.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
/**
|
2018-02-21 17:30:55 +01:00
|
|
|
* Error macros. Unlike exceptions and asserts, these macros try to maintain consistency and stability
|
2014-02-10 02:10:30 +01:00
|
|
|
* inside the code. It is recommended to always return processable data, so in case of an error, the
|
2016-03-09 00:00:52 +01:00
|
|
|
* engine can stay working well.
|
2014-02-10 02:10:30 +01:00
|
|
|
* In most cases, bugs and/or invalid data are not fatal and should never allow a perfectly running application
|
|
|
|
* to fail or crash.
|
|
|
|
*/
|
|
|
|
|
2016-03-09 00:00:52 +01:00
|
|
|
/**
|
2014-02-10 02:10:30 +01:00
|
|
|
* Pointer to the error macro priting function. Reassign to any function to have errors printed
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** Function used by the error macros */
|
|
|
|
|
|
|
|
// function, file, line, error, explanation
|
|
|
|
|
|
|
|
enum ErrorHandlerType {
|
|
|
|
ERR_HANDLER_ERROR,
|
|
|
|
ERR_HANDLER_WARNING,
|
2016-10-10 23:31:01 +02:00
|
|
|
ERR_HANDLER_SCRIPT,
|
|
|
|
ERR_HANDLER_SHADER,
|
2014-02-10 02:10:30 +01:00
|
|
|
};
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
typedef void (*ErrorHandlerFunc)(void *, const char *, const char *, int p_line, const char *, const char *, ErrorHandlerType p_type);
|
|
|
|
void _err_set_last_error(const char *p_err);
|
2014-02-10 02:10:30 +01:00
|
|
|
void _err_clear_last_error();
|
|
|
|
|
|
|
|
struct ErrorHandlerList {
|
|
|
|
|
|
|
|
ErrorHandlerFunc errfunc;
|
|
|
|
void *userdata;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ErrorHandlerList *next;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ErrorHandlerList() {
|
|
|
|
errfunc = 0;
|
|
|
|
next = 0;
|
|
|
|
userdata = 0;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
void add_error_handler(ErrorHandlerList *p_handler);
|
|
|
|
void remove_error_handler(ErrorHandlerList *p_handler);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void _err_print_error(const char *p_function, const char *p_file, int p_line, const char *p_error, ErrorHandlerType p_type = ERR_HANDLER_ERROR);
|
2017-10-17 16:37:40 +02:00
|
|
|
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, bool fatal = false);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
#ifndef _STR
|
|
|
|
#define _STR(m_x) #m_x
|
|
|
|
#define _MKSTR(m_x) _STR(m_x)
|
|
|
|
#endif
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
#define _FNL __FILE__ ":"
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
/** An index has failed if m_index<0 or m_index >=m_size, the function exists */
|
|
|
|
|
|
|
|
extern bool _err_error_exists;
|
|
|
|
|
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
/** Print a warning string.
|
|
|
|
*/
|
2017-03-05 16:44:50 +01:00
|
|
|
#define ERR_EXPLAINC(m_reason) \
|
|
|
|
{ \
|
|
|
|
_err_set_last_error(m_reason); \
|
|
|
|
_err_error_exists = true; \
|
|
|
|
}
|
|
|
|
#define ERR_EXPLAIN(m_string) \
|
|
|
|
{ \
|
|
|
|
_err_set_last_error(String(m_string).utf8().get_data()); \
|
|
|
|
_err_error_exists = true; \
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
#define ERR_EXPLAIN(m_text)
|
|
|
|
#define ERR_EXPLAINC(m_text)
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef __GNUC__
|
|
|
|
//#define FUNCTION_STR __PRETTY_FUNCTION__ - too annoying
|
|
|
|
#define FUNCTION_STR __FUNCTION__
|
|
|
|
#else
|
|
|
|
#define FUNCTION_STR __FUNCTION__
|
|
|
|
#endif
|
|
|
|
|
2017-05-26 21:11:16 +02:00
|
|
|
// Don't use this directly; instead, use any of the CRASH_* macros
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#define GENERATE_TRAP \
|
|
|
|
__debugbreak(); \
|
|
|
|
/* Avoid warning about control paths */ \
|
|
|
|
for (;;) { \
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define GENERATE_TRAP __builtin_trap();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// (*): See https://stackoverflow.com/questions/257418/do-while-0-what-is-it-good-for
|
|
|
|
|
2017-10-17 16:37:40 +02:00
|
|
|
#define ERR_FAIL_INDEX(m_index, m_size) \
|
|
|
|
do { \
|
|
|
|
if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
|
|
|
|
_err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size)); \
|
|
|
|
return; \
|
|
|
|
} else \
|
|
|
|
_err_error_exists = false; \
|
2017-05-26 21:11:16 +02:00
|
|
|
} while (0); // (*)
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
/** An index has failed if m_index<0 or m_index >=m_size, the function exists.
|
2017-05-26 21:11:16 +02:00
|
|
|
* This function returns an error value, if returning Error, please select the most
|
|
|
|
* appropriate error condition from error_macros.h
|
|
|
|
*/
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-10-17 16:37:40 +02:00
|
|
|
#define ERR_FAIL_INDEX_V(m_index, m_size, m_retval) \
|
|
|
|
do { \
|
|
|
|
if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
|
|
|
|
_err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size)); \
|
|
|
|
return m_retval; \
|
|
|
|
} else \
|
|
|
|
_err_error_exists = false; \
|
2017-05-26 21:11:16 +02:00
|
|
|
} while (0); // (*)
|
|
|
|
|
|
|
|
/** Use this one if there is no sensible fallback, that is, the error is unrecoverable.
|
|
|
|
* We'll return a null reference and try to keep running.
|
|
|
|
*/
|
2017-10-17 16:37:40 +02:00
|
|
|
#define CRASH_BAD_INDEX(m_index, m_size) \
|
|
|
|
do { \
|
|
|
|
if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
|
|
|
|
_err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), true); \
|
|
|
|
GENERATE_TRAP \
|
|
|
|
} \
|
2017-05-26 21:11:16 +02:00
|
|
|
} while (0); // (*)
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
/** An error condition happened (m_cond tested true) (WARNING this is the opposite as assert().
|
2014-02-10 02:10:30 +01:00
|
|
|
* the function will exit.
|
|
|
|
*/
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
#define ERR_FAIL_NULL(m_param) \
|
|
|
|
{ \
|
2017-09-20 11:04:50 +02:00
|
|
|
if (unlikely(!m_param)) { \
|
2017-03-05 16:44:50 +01:00
|
|
|
_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Parameter ' " _STR(m_param) " ' is null."); \
|
|
|
|
return; \
|
|
|
|
} else \
|
|
|
|
_err_error_exists = false; \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define ERR_FAIL_NULL_V(m_param, m_retval) \
|
|
|
|
{ \
|
2017-09-20 11:04:50 +02:00
|
|
|
if (unlikely(!m_param)) { \
|
2017-03-05 16:44:50 +01:00
|
|
|
_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Parameter ' " _STR(m_param) " ' is null."); \
|
|
|
|
return m_retval; \
|
|
|
|
} else \
|
|
|
|
_err_error_exists = false; \
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-03-09 00:00:52 +01:00
|
|
|
/** An error condition happened (m_cond tested true) (WARNING this is the opposite as assert().
|
2014-02-10 02:10:30 +01:00
|
|
|
* the function will exit.
|
|
|
|
*/
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
#define ERR_FAIL_COND(m_cond) \
|
|
|
|
{ \
|
2017-09-20 11:04:50 +02:00
|
|
|
if (unlikely(m_cond)) { \
|
2017-03-05 16:44:50 +01:00
|
|
|
_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition ' " _STR(m_cond) " ' is true."); \
|
|
|
|
return; \
|
|
|
|
} else \
|
|
|
|
_err_error_exists = false; \
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-05-26 21:11:16 +02:00
|
|
|
/** Use this one if there is no sensible fallback, that is, the error is unrecoverable.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define CRASH_COND(m_cond) \
|
|
|
|
{ \
|
2017-09-20 11:04:50 +02:00
|
|
|
if (unlikely(m_cond)) { \
|
2017-05-26 21:11:16 +02:00
|
|
|
_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "FATAL: Condition ' " _STR(m_cond) " ' is true."); \
|
|
|
|
GENERATE_TRAP \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
2016-03-09 00:00:52 +01:00
|
|
|
/** An error condition happened (m_cond tested true) (WARNING this is the opposite as assert().
|
2014-02-10 02:10:30 +01:00
|
|
|
* the function will exit.
|
|
|
|
* This function returns an error value, if returning Error, please select the most
|
|
|
|
* appropriate error condition from error_macros.h
|
|
|
|
*/
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
#define ERR_FAIL_COND_V(m_cond, m_retval) \
|
|
|
|
{ \
|
2017-09-20 11:04:50 +02:00
|
|
|
if (unlikely(m_cond)) { \
|
2017-03-05 16:44:50 +01:00
|
|
|
_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition ' " _STR(m_cond) " ' is true. returned: " _STR(m_retval)); \
|
|
|
|
return m_retval; \
|
|
|
|
} else \
|
|
|
|
_err_error_exists = false; \
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-03-09 00:00:52 +01:00
|
|
|
/** An error condition happened (m_cond tested true) (WARNING this is the opposite as assert().
|
2014-02-10 02:10:30 +01:00
|
|
|
* the loop will skip to the next iteration.
|
|
|
|
*/
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
#define ERR_CONTINUE(m_cond) \
|
|
|
|
{ \
|
2017-09-20 11:04:50 +02:00
|
|
|
if (unlikely(m_cond)) { \
|
2017-03-05 16:44:50 +01:00
|
|
|
_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition ' " _STR(m_cond) " ' is true. Continuing..:"); \
|
|
|
|
continue; \
|
|
|
|
} else \
|
|
|
|
_err_error_exists = false; \
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-03-09 00:00:52 +01:00
|
|
|
/** An error condition happened (m_cond tested true) (WARNING this is the opposite as assert().
|
2014-02-10 02:10:30 +01:00
|
|
|
* the loop will break
|
|
|
|
*/
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
#define ERR_BREAK(m_cond) \
|
|
|
|
{ \
|
2017-09-20 11:04:50 +02:00
|
|
|
if (unlikely(m_cond)) { \
|
2017-03-05 16:44:50 +01:00
|
|
|
_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition ' " _STR(m_cond) " ' is true. Breaking..:"); \
|
|
|
|
break; \
|
|
|
|
} else \
|
|
|
|
_err_error_exists = false; \
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
/** Print an error string and return
|
|
|
|
*/
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
#define ERR_FAIL() \
|
|
|
|
{ \
|
|
|
|
_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Method/Function Failed."); \
|
|
|
|
_err_error_exists = false; \
|
|
|
|
return; \
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
/** Print an error string and return with value
|
|
|
|
*/
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
#define ERR_FAIL_V(m_value) \
|
|
|
|
{ \
|
|
|
|
_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Method/Function Failed, returning: " __STR(m_value)); \
|
|
|
|
_err_error_exists = false; \
|
|
|
|
return m_value; \
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-05-26 21:11:16 +02:00
|
|
|
/** Use this one if there is no sensible fallback, that is, the error is unrecoverable.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define CRASH_NOW() \
|
|
|
|
{ \
|
|
|
|
_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "FATAL: Method/Function Failed."); \
|
|
|
|
GENERATE_TRAP \
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
/** Print an error string.
|
|
|
|
*/
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
#define ERR_PRINT(m_string) \
|
|
|
|
{ \
|
|
|
|
_err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_string); \
|
|
|
|
_err_error_exists = false; \
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
#define ERR_PRINTS(m_string) \
|
|
|
|
{ \
|
|
|
|
_err_print_error(FUNCTION_STR, __FILE__, __LINE__, String(m_string).utf8().get_data()); \
|
|
|
|
_err_error_exists = false; \
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
/** Print a warning string.
|
|
|
|
*/
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
#define WARN_PRINT(m_string) \
|
|
|
|
{ \
|
|
|
|
_err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_string, ERR_HANDLER_WARNING); \
|
|
|
|
_err_error_exists = false; \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define WARN_PRINTS(m_string) \
|
|
|
|
{ \
|
|
|
|
_err_print_error(FUNCTION_STR, __FILE__, __LINE__, String(m_string).utf8().get_data(), ERR_HANDLER_WARNING); \
|
|
|
|
_err_error_exists = false; \
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2018-05-31 08:58:37 +02:00
|
|
|
#define WARN_DEPRECATED \
|
|
|
|
{ \
|
2018-08-07 17:25:48 +02:00
|
|
|
static volatile bool warning_shown = false; \
|
2018-05-31 08:58:37 +02:00
|
|
|
if (!warning_shown) { \
|
|
|
|
_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "This method has been deprecated and will be removed in the future", ERR_HANDLER_WARNING); \
|
|
|
|
_err_error_exists = false; \
|
|
|
|
warning_shown = true; \
|
|
|
|
} \
|
2018-04-18 14:10:25 +02:00
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
#endif
|