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 /** @addtogroup XBT_error
22 * This is how the errors get reported in the SimGrid toolkit. This mechanism is not
23 * as powerful as exceptions, but since we're using C, there is not much we can do.
27 /** @name 1. Type definition and basic operations
31 /** \brief Error types */
33 no_error=0, /**< succes */
34 mismatch_error=1, /**< The provided ID does not match */
35 system_error=2, /**< a syscall did fail */
36 network_error=3, /**< error while sending/receiving data */
37 timeout_error=4, /**< not quick enough, dude */
38 thread_error=5, /**< error while [un]locking */
39 unknown_error=6, /**< unknown error */
41 /* remote errors: result of a RMI/RPC.
42 no_error(=0) is the same for both */
43 remote_mismatch_error=129,
51 const char *xbt_error_name(xbt_error_t errcode);
52 void xbt_abort(void) _XBT_GNUC_NORETURN;
53 void xbt_die(const char *msg) _XBT_GNUC_NORETURN;
57 /** @name 2. TRY macro family
59 * Those functions are used to launch a function call and react automatically
60 * to its return value. They expect such a variable to be declared in the scope:
61 * \verbatim xbt_error_t errcode;\endverbatim
65 /** @brief return the error code if != no_error
68 #define TRY(action) do { \
69 if ((errcode=action) != no_error) { \
70 ERROR1("'%s' error raising...", xbt_error_name(errcode)); \
74 /** @brief return the error code if != no_error and != \a catched
77 #define TRYCATCH(action,catched) if ((errcode=action) != no_error && errcode !=catched) return errcode
79 /** @brief xbt_abort if the error code != no_error
82 #define TRYFAIL(action) do { \
83 if ((errcode=action) != no_error) { \
84 ERROR1("Got '%s' error !", xbt_error_name(errcode)); \
90 /** @brief return the error code if != \a expected_error (no_error not ok)
93 #define TRYEXPECT(action,expected_error) do { \
95 if (errcode != expected_error) { \
96 ERROR2("Got error %s (instead of %s expected)\n", \
97 xbt_error_name(errcode), \
98 xbt_error_name(expected_error)); \
105 #define _XBT_ERR_PRE do {
106 #define _XBT_ERR_POST(code) \
111 /** @name 3. RAISE macro family
113 * Return a error code, doing some logs on stderr.
115 * @todo This should use the logging features, not stderr
120 /** @hideinitializer */
121 #define RAISE0(code,fmt) _XBT_ERR_PRE ERROR0(fmt); _XBT_ERR_POST(code)
122 /** @hideinitializer */
123 #define RAISE1(code,fmt,a1) _XBT_ERR_PRE ERROR1(fmt,a1); _XBT_ERR_POST(code)
124 /** @hideinitializer */
125 #define RAISE2(code,fmt,a1,a2) _XBT_ERR_PRE ERROR2(fmt,a1,a2); _XBT_ERR_POST(code)
126 /** @hideinitializer */
127 #define RAISE3(code,fmt,a1,a2,a3) _XBT_ERR_PRE ERROR3(fmt,a1,a2,a3); _XBT_ERR_POST(code)
128 /** @hideinitializer */
129 #define RAISE4(code,fmt,a1,a2,a3,a4) _XBT_ERR_PRE ERROR4(fmt,a1,a2,a3,a4); _XBT_ERR_POST(code)
130 /** @hideinitializer */
131 #define RAISE5(code,fmt,a1,a2,a3,a4,a5) _XBT_ERR_PRE ERROR5(fmt,a1,a2,a3,a4,a5); _XBT_ERR_POST(code)
132 /** @hideinitializer */
133 #define RAISE6(code,fmt,a1,a2,a3,a4,a5,a6) _XBT_ERR_PRE ERROR6(fmt,a1,a2,a3,a4,a5,a6); _XBT_ERR_POST(code)
137 * \name 4. assert macro familly
139 * Those are the GRAS version of the good ol' assert macro. You can pass them a format message and
140 * arguments, just as if it where a printf. It is converted to a CRITICALn logging request.
145 #define xbt_assert(cond)
146 #define xbt_assert0(cond,msg)
147 #define xbt_assert1(cond,msg,a)
148 #define xbt_assert2(cond,msg,a,b)
149 #define xbt_assert3(cond,msg,a,b,c)
150 #define xbt_assert4(cond,msg,a,b,c,d)
151 #define xbt_assert5(cond,msg,a,b,c,d,e)
152 #define xbt_assert6(cond,msg,a,b,c,d,e,f)
154 /** @brief The condition which failed will be displayed.
156 #define xbt_assert(cond) if (!(cond)) { CRITICAL1("Assertion %s failed", #cond); xbt_abort(); }
157 /** @hideinitializer */
158 #define xbt_assert0(cond,msg) if (!(cond)) { CRITICAL0(msg); xbt_abort(); }
159 /** @hideinitializer */
160 #define xbt_assert1(cond,msg,a) if (!(cond)) { CRITICAL1(msg,a); xbt_abort(); }
161 /** @hideinitializer */
162 #define xbt_assert2(cond,msg,a,b) if (!(cond)) { CRITICAL2(msg,a,b); xbt_abort(); }
163 /** @hideinitializer */
164 #define xbt_assert3(cond,msg,a,b,c) if (!(cond)) { CRITICAL3(msg,a,b,c); xbt_abort(); }
165 /** @hideinitializer */
166 #define xbt_assert4(cond,msg,a,b,c,d) if (!(cond)) { CRITICAL4(msg,a,b,c,d); xbt_abort(); }
167 /** @hideinitializer */
168 #define xbt_assert5(cond,msg,a,b,c,d,e) if (!(cond)) { CRITICAL5(msg,a,b,c,d,e); xbt_abort(); }
169 /** @hideinitializer */
170 #define xbt_assert6(cond,msg,a,b,c,d,e,f) if (!(cond)) { CRITICAL6(msg,a,b,c,d,e,f); xbt_abort(); }
175 /** @name 5. Useful predefined errors
179 #define RAISE_IMPOSSIBLE RAISE0(unknown_error,"The Impossible did happen")
180 #define RAISE_UNIMPLEMENTED RAISE1(unknown_error,"Function %s unimplemented",__FUNCTION__)
182 #define DIE_IMPOSSIBLE xbt_assert0(0,"The Impossible did happen (yet again)")
183 #define xbt_assert_error(a) xbt_assert1(errcode == (a), "Error %s unexpected",xbt_error_name(errcode))
190 #endif /* XBT_ERROR_H */