error_macros.h
1 /*************************************************************************/
2 /* error_macros.h */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* http://www.godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
9 /* */
10 /* Permission is hereby granted, free of charge, to any person obtaining */
11 /* a copy of this software and associated documentation files (the */
12 /* "Software"), to deal in the Software without restriction, including */
13 /* without limitation the rights to use, copy, modify, merge, publish, */
14 /* distribute, sublicense, and/or sell copies of the Software, and to */
15 /* permit persons to whom the Software is furnished to do so, subject to */
16 /* the following conditions: */
17 /* */
18 /* The above copyright notice and this permission notice shall be */
19 /* included in all copies or substantial portions of the Software. */
20 /* */
21 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
22 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
23 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
24 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
25 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
26 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
27 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
28 /*************************************************************************/
29 #ifndef ERROR_MACROS_H
30 #define ERROR_MACROS_H
31 
32 
47 // function, file, line, error, explanation
48 
49 enum ErrorHandlerType {
50  ERR_HANDLER_ERROR,
51  ERR_HANDLER_WARNING,
52  ERR_HANDLER_SCRIPT
53 };
54 
55 typedef void (*ErrorHandlerFunc)(void*,const char*,const char*,int p_line,const char *, const char *,ErrorHandlerType p_type);
56 void _err_set_last_error(const char* p_err);
57 void _err_clear_last_error();
58 
60 
61  ErrorHandlerFunc errfunc;
62  void *userdata;
63 
64  ErrorHandlerList*next;
65 
66  ErrorHandlerList() { errfunc=0; next=0; userdata=0; }
67 };
68 
69 void add_error_handler(ErrorHandlerList *p_handler);
70 void remove_error_handler(ErrorHandlerList *p_handler);
71 
72 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);
73 
74 #ifndef _STR
75 #define _STR(m_x) #m_x
76 #define _MKSTR(m_x) _STR(m_x)
77 #endif
78 
79 #define _FNL __FILE__":"
80 
83 extern bool _err_error_exists;
84 
85 #ifdef DEBUG_ENABLED
86 
88 #define ERR_EXPLAINC(m_reason) {_err_set_last_error(m_reason); _err_error_exists=true;}
89 #define ERR_EXPLAIN(m_string) {_err_set_last_error(String(m_string).utf8().get_data()); _err_error_exists=true;}
90 
91 #else
92 
93 #define ERR_EXPLAIN( m_text )
94 #define ERR_EXPLAINC( m_text )
95 
96 #endif
97 
98 #ifdef __GNUC__
99 //#define FUNCTION_STR __PRETTY_FUNCTION__ - too annoying
100 #define FUNCTION_STR __FUNCTION__
101 #else
102 #define FUNCTION_STR __FUNCTION__
103 #endif
104 
105 #define ERR_FAIL_INDEX(m_index,m_size) \
106  do {if ((m_index)<0 || (m_index)>=(m_size)) { \
107  _err_print_error(FUNCTION_STR,__FILE__,__LINE__,"Index " _STR(m_index)" out of size (" _STR(m_size)")."); \
108  return; \
109  } else _err_error_exists=false; } while(0); \
110 
111 
116 #define ERR_FAIL_INDEX_V(m_index,m_size,m_retval) \
117  do {if ((m_index)<0 || (m_index)>=(m_size)) { \
118  _err_print_error(FUNCTION_STR,__FILE__,__LINE__,"Index " _STR(m_index)" out of size (" _STR(m_size)")."); \
119  return m_retval; \
120  } else _err_error_exists=false;} while (0);
121 
126  #define ERR_FAIL_NULL(m_param) \
127  { if ( !m_param ) { \
128  _err_print_error(FUNCTION_STR,__FILE__,__LINE__,"Parameter ' " _STR(m_param)" ' is null."); \
129  return; \
130  }else _err_error_exists=false; } \
131 
132 
133 #define ERR_FAIL_NULL_V(m_param,m_retval) \
134  { if ( !m_param ) { \
135  _err_print_error(FUNCTION_STR,__FILE__,__LINE__,"Parameter ' " _STR(m_param)" ' is null."); \
136  return m_retval; \
137  }else _err_error_exists=false; } \
138 
139 
143 #define ERR_FAIL_COND(m_cond) \
144  { if ( m_cond ) { \
145  _err_print_error(FUNCTION_STR,__FILE__,__LINE__,"Condition ' " _STR(m_cond)" ' is true."); \
146  return; \
147  }else _err_error_exists=false; } \
148 
149 
155 #define ERR_FAIL_COND_V(m_cond,m_retval) \
156  { if ( m_cond ) { \
157  _err_print_error(FUNCTION_STR,__FILE__,__LINE__,"Condition ' " _STR(m_cond)" ' is true. returned: " _STR(m_retval)); \
158  return m_retval; \
159  }else _err_error_exists=false; } \
160 
161 
165 #define ERR_CONTINUE(m_cond) \
166  { if ( m_cond ) { \
167  _err_print_error(FUNCTION_STR,__FILE__,__LINE__,"Condition ' " _STR(m_cond)" ' is true. Continuing..:"); \
168  continue;\
169  } else _err_error_exists=false;} \
170 
171 
175 #define ERR_BREAK(m_cond) \
176  { if ( m_cond ) { \
177  _err_print_error(FUNCTION_STR,__FILE__,__LINE__,"Condition ' " _STR(m_cond)" ' is true. Breaking..:"); \
178  break;\
179  } else _err_error_exists=false;} \
180 
181 
184 #define ERR_FAIL() \
185 { \
186  _err_print_error(FUNCTION_STR,__FILE__,__LINE__,"Method/Function Failed."); \
187  _err_error_exists=false;\
188  return;\
189 } \
190 
191 
194 #define ERR_FAIL_V(m_value) \
195 { \
196  _err_print_error(FUNCTION_STR,__FILE__,__LINE__,"Method/Function Failed, returning: " __STR(m_value)); \
197  _err_error_exists=false; \
198  return m_value;\
199 } \
200 
201 
204 #define ERR_PRINT(m_string) \
205  { \
206  _err_print_error(FUNCTION_STR,__FILE__,__LINE__,m_string); \
207  _err_error_exists=false;\
208  } \
209 
210 #define ERR_PRINTS(m_string) \
211  { \
212  _err_print_error(FUNCTION_STR,__FILE__,__LINE__,String(m_string).utf8().get_data()); \
213  _err_error_exists=false;\
214  } \
215 
216 
219 #define WARN_PRINT(m_string) \
220  { \
221  _err_print_error(FUNCTION_STR,__FILE__,__LINE__,m_string,ERR_HANDLER_WARNING); \
222  _err_error_exists=false;\
223  } \
224 
225 
226 
227 #endif
Definition: error_macros.h:59