3 /* xbt/error.h - Error tracking support */
5 /* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved. */
7 /* This program is free software; you can redistribute it and/or modify it
8 * under the terms of the license (GNU LGPL) which comes with this package. */
13 #include <stdio.h> /* FIXME: Get rid of it */
15 #include "xbt/misc.h" /* BEGIN_DECL */
20 #define _XBT_ERR_PRE do {
21 #define _XBT_ERR_POST(code) \
25 /** @addtogroup XBT_error
27 * This is how the errors get reported in the SimGrid toolkit. This mechanism is not
28 * as powerful as exceptions, but since we're using C, there is not much we can do.
32 /** @name 1. Type definition and basic operations
36 /** \brief Error types */
38 no_error=0, /**< succes */
39 mismatch_error=1, /**< The provided ID does not match */
40 system_error=2, /**< a syscall did fail */
41 network_error=3, /**< error while sending/receiving data */
42 timeout_error=4, /**< not quick enough, dude */
43 thread_error=5, /**< error while [un]locking */
44 unknown_error=6, /**< unknown error */
46 /* remote errors: result of a RMI/RPC.
47 no_error(=0) is the same for both */
48 remote_mismatch_error=129,
56 const char *xbt_error_name(xbt_error_t errcode);
57 void xbt_abort(void) _XBT_GNUC_NORETURN;
58 void xbt_die(const char *msg) _XBT_GNUC_NORETURN;
62 /** @name 2. TRY macro family
64 * Those functions are used to launch a function call and react automatically
65 * to its return value. They expect such a variable to be declared in the scope:
66 * \verbatim xbt_error_t errcode;\endverbatim
70 /** @brief return the error code if != no_error
73 #define TRY(action) do { \
74 if ((errcode=action) != no_error) { \
75 fprintf (stderr, "%s:%d: '%s' error raising...\n", \
77 xbt_error_name(errcode)); \
81 /** @brief return the error code if != no_error and != \a catched
84 #define TRYCATCH(action,catched) if ((errcode=action) != no_error && errcode !=catched) return errcode
86 /** @brief xbt_abort if the error code != no_error
89 #define TRYFAIL(action) do { \
90 if ((errcode=action) != no_error) { \
91 fprintf(stderr,"%s:%d: Got '%s' error !\n", \
93 xbt_error_name(errcode)); \
99 /** @brief return the error code if != \a expected_error (no_error not ok)
102 #define TRYEXPECT(action,expected_error) do { \
104 if (errcode != expected_error) { \
105 fprintf(stderr,"Got error %s (instead of %s expected)\n", \
106 xbt_error_name(errcode), \
107 xbt_error_name(expected_error)); \
113 /** @name 3. RAISE macro family
115 * Return a error code, doing some logs on stderr.
117 * @todo This should use the logging features, not stderr
122 /** @hideinitializer */
123 #define RAISE0(code,fmt) _XBT_ERR_PRE \
124 fprintf(stderr,"%s:%d:%s: " fmt "\n", \
125 __FILE__,__LINE__,__FUNCTION__); \
127 /** @hideinitializer */
128 #define RAISE1(code,fmt,a1) _XBT_ERR_PRE \
129 fprintf(stderr,"%s:%d:%s: " fmt "\n", \
130 __FILE__,__LINE__,__FUNCTION__,a1); \
132 /** @hideinitializer */
133 #define RAISE2(code,fmt,a1,a2) _XBT_ERR_PRE \
134 fprintf(stderr,"%s:%d:%s: " fmt "\n", \
135 __FILE__,__LINE__,__FUNCTION__,a1,a2); \
137 /** @hideinitializer */
138 #define RAISE3(code,fmt,a1,a2,a3) _XBT_ERR_PRE \
139 fprintf(stderr,"%s:%d:%s: " fmt "\n", \
140 __FILE__,__LINE__,__FUNCTION__,a1,a2,a3); \
142 /** @hideinitializer */
143 #define RAISE4(code,fmt,a1,a2,a3,a4) _XBT_ERR_PRE \
144 fprintf(stderr,"%s:%d:%s: " fmt "\n", \
145 __FILE__,__LINE__,__FUNCTION__,a1,a2,a3,a4); \
147 /** @hideinitializer */
148 #define RAISE5(code,fmt,a1,a2,a3,a4,a5) _XBT_ERR_PRE \
149 fprintf(stderr,"%s:%d:%s: " fmt "\n", \
150 __FILE__,__LINE__,__FUNCTION__,a1,a2,a3,a4,a5); \
152 /** @hideinitializer */
153 #define RAISE6(code,fmt,a1,a2,a3,a4,a5,a6) _XBT_ERR_PRE \
154 fprintf(stderr,"%s:%d:%s: " fmt "\n", \
155 __FILE__,__LINE__,__FUNCTION__,a1,a2,a3,a4,a5,a6); \
160 * \name 4. assert macro familly
162 * Those are the GRAS version of the good ol' assert macro. You can pass them a format message and
163 * arguments, just as if it where a printf. It is converted to a CRITICALn logging request.
168 #define xbt_assert(cond)
169 #define xbt_assert0(cond,msg)
170 #define xbt_assert1(cond,msg,a)
171 #define xbt_assert2(cond,msg,a,b)
172 #define xbt_assert3(cond,msg,a,b,c)
173 #define xbt_assert4(cond,msg,a,b,c,d)
174 #define xbt_assert5(cond,msg,a,b,c,d,e)
175 #define xbt_assert6(cond,msg,a,b,c,d,e,f)
177 /** @brief The condition which failed will be displayed.
179 #define xbt_assert(cond) if (!(cond)) { CRITICAL1("Assertion %s failed", #cond); xbt_abort(); }
180 /** @hideinitializer */
181 #define xbt_assert0(cond,msg) if (!(cond)) { CRITICAL0(msg); xbt_abort(); }
182 /** @hideinitializer */
183 #define xbt_assert1(cond,msg,a) if (!(cond)) { CRITICAL1(msg,a); xbt_abort(); }
184 /** @hideinitializer */
185 #define xbt_assert2(cond,msg,a,b) if (!(cond)) { CRITICAL2(msg,a,b); xbt_abort(); }
186 /** @hideinitializer */
187 #define xbt_assert3(cond,msg,a,b,c) if (!(cond)) { CRITICAL3(msg,a,b,c); xbt_abort(); }
188 /** @hideinitializer */
189 #define xbt_assert4(cond,msg,a,b,c,d) if (!(cond)) { CRITICAL4(msg,a,b,c,d); xbt_abort(); }
190 /** @hideinitializer */
191 #define xbt_assert5(cond,msg,a,b,c,d,e) if (!(cond)) { CRITICAL5(msg,a,b,c,d,e); xbt_abort(); }
192 /** @hideinitializer */
193 #define xbt_assert6(cond,msg,a,b,c,d,e,f) if (!(cond)) { CRITICAL6(msg,a,b,c,d,e,f); xbt_abort(); }
198 /** @name 5. Useful predefined errors
202 #define RAISE_IMPOSSIBLE RAISE0(unknown_error,"The Impossible did happen")
203 #define RAISE_UNIMPLEMENTED RAISE1(unknown_error,"Function %s unimplemented",__FUNCTION__)
205 #define DIE_IMPOSSIBLE xbt_assert0(0,"The Impossible did happen (yet again)")
206 #define xbt_assert_error(a) xbt_assert1(errcode == (a), "Error %s unexpected",xbt_error_name(errcode))
213 #endif /* XBT_ERROR_H */