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 "xbt/misc.h" /* BEGIN_DECL */
18 /** @addtogroup XBT_error
20 * This is how the errors get reported in the SimGrid toolkit. This mechanism is not
21 * as powerful as exceptions, but since we're using C, there is not much we can do.
25 /** @name 1. Type definition and basic operations
29 /** \brief Error types */
31 no_error=0, /**< succes */
32 old_mismatch_error=1, /**< The provided ID does not match */
33 old_system_error=2, /**< a syscall did fail */
34 old_network_error=3, /**< error while sending/receiving data */
35 old_timeout_error=4, /**< not quick enough, dude */
36 old_thread_error=5, /**< error while [un]locking */
37 old_unknown_error=6 /**< unknown error */
41 const char *xbt_error_name(xbt_error_t errcode);
42 void xbt_abort(void) _XBT_GNUC_NORETURN;
43 void xbt_die(const char *msg) _XBT_GNUC_NORETURN;
47 /** @name 2. TRY macro family
49 * Those functions are used to launch a function call and react automatically
50 * to its return value. They expect such a variable to be declared in the scope:
51 * \verbatim xbt_error_t errcode;\endverbatim
55 /** @brief return the error code if != no_error
58 #define TRYOLD(action) do { \
59 if ((errcode=action) != no_error) { \
60 ERROR1("'%s' error raising...", xbt_error_name(errcode)); \
64 /** @brief return the error code if != no_error and != \a catched
67 #define TRYCATCH(action,catched) if ((errcode=action) != no_error && errcode !=catched) return errcode
69 /** @brief xbt_abort if the error code != no_error
72 #define TRYFAIL(action) do { \
73 if ((errcode=action) != no_error) { \
74 ERROR1("Got '%s' error !", xbt_error_name(errcode)); \
80 /** @brief return the error code if != \a expected_error (no_error not ok)
83 #define TRYEXPECT(action,expected_error) do { \
85 if (errcode != expected_error) { \
86 ERROR2("Got error %s (instead of %s expected)\n", \
87 xbt_error_name(errcode), \
88 xbt_error_name(expected_error)); \
95 #define _XBT_ERR_PRE do {
96 #define _XBT_ERR_POST(code) \
101 /** @name 3. RAISE macro family
103 * Return a error code, doing some logs on stderr.
105 * @todo This should use the logging features, not stderr
110 /** @hideinitializer */
111 #define OLDRAISE0(code,fmt) _XBT_ERR_PRE ERROR0(fmt); _XBT_ERR_POST(code)
112 /** @hideinitializer */
113 #define OLDRAISE1(code,fmt,a1) _XBT_ERR_PRE ERROR1(fmt,a1); _XBT_ERR_POST(code)
114 /** @hideinitializer */
115 #define OLDRAISE2(code,fmt,a1,a2) _XBT_ERR_PRE ERROR2(fmt,a1,a2); _XBT_ERR_POST(code)
116 /** @hideinitializer */
117 #define OLDRAISE3(code,fmt,a1,a2,a3) _XBT_ERR_PRE ERROR3(fmt,a1,a2,a3); _XBT_ERR_POST(code)
118 /** @hideinitializer */
119 #define OLDRAISE4(code,fmt,a1,a2,a3,a4) _XBT_ERR_PRE ERROR4(fmt,a1,a2,a3,a4); _XBT_ERR_POST(code)
120 /** @hideinitializer */
121 #define OLDRAISE5(code,fmt,a1,a2,a3,a4,a5) _XBT_ERR_PRE ERROR5(fmt,a1,a2,a3,a4,a5); _XBT_ERR_POST(code)
122 /** @hideinitializer */
123 #define OLDRAISE6(code,fmt,a1,a2,a3,a4,a5,a6) _XBT_ERR_PRE ERROR6(fmt,a1,a2,a3,a4,a5,a6); _XBT_ERR_POST(code)
127 * \name 4. assert macro familly
129 * Those are the GRAS version of the good ol' assert macro. You can pass them a format message and
130 * arguments, just as if it where a printf. It is converted to a CRITICALn logging request.
135 #define xbt_assert(cond)
136 #define xbt_assert0(cond,msg)
137 #define xbt_assert1(cond,msg,a)
138 #define xbt_assert2(cond,msg,a,b)
139 #define xbt_assert3(cond,msg,a,b,c)
140 #define xbt_assert4(cond,msg,a,b,c,d)
141 #define xbt_assert5(cond,msg,a,b,c,d,e)
142 #define xbt_assert6(cond,msg,a,b,c,d,e,f)
144 /** @brief The condition which failed will be displayed.
146 #define xbt_assert(cond) if (!(cond)) { CRITICAL1("Assertion %s failed", #cond); xbt_abort(); }
147 /** @hideinitializer */
148 #define xbt_assert0(cond,msg) if (!(cond)) { CRITICAL0(msg); xbt_abort(); }
149 /** @hideinitializer */
150 #define xbt_assert1(cond,msg,a) if (!(cond)) { CRITICAL1(msg,a); xbt_abort(); }
151 /** @hideinitializer */
152 #define xbt_assert2(cond,msg,a,b) if (!(cond)) { CRITICAL2(msg,a,b); xbt_abort(); }
153 /** @hideinitializer */
154 #define xbt_assert3(cond,msg,a,b,c) if (!(cond)) { CRITICAL3(msg,a,b,c); xbt_abort(); }
155 /** @hideinitializer */
156 #define xbt_assert4(cond,msg,a,b,c,d) if (!(cond)) { CRITICAL4(msg,a,b,c,d); xbt_abort(); }
157 /** @hideinitializer */
158 #define xbt_assert5(cond,msg,a,b,c,d,e) if (!(cond)) { CRITICAL5(msg,a,b,c,d,e); xbt_abort(); }
159 /** @hideinitializer */
160 #define xbt_assert6(cond,msg,a,b,c,d,e,f) if (!(cond)) { CRITICAL6(msg,a,b,c,d,e,f); xbt_abort(); }
165 /** @name 5. Useful predefined errors
169 #define RAISE_IMPOSSIBLE RAISE0(old_unknown_error,"The Impossible did happen (yet again)")
170 #define RAISE_UNIMPLEMENTED RAISE1(old_unknown_error,"Function %s unimplemented",__FUNCTION__)
172 #define OLDDIE_IMPOSSIBLE xbt_assert0(0,"The Impossible did happen (yet again)")
173 #define OLDxbt_assert_error(a) xbt_assert1(errcode == (a), "Error %s unexpected",xbt_error_name(errcode))
180 #endif /* XBT_ERROR_H */