Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
97edd88db17e08f8692f78b575bdc6f49d6114e4
[simgrid.git] / include / xbt / error.h
1 /* $Id$ */
2
3 /* xbt/error.h - Error tracking support                                     */
4
5 /* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved.            */
6
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. */
9
10 #ifndef XBT_ERROR_H
11 #define XBT_ERROR_H
12
13 #include "xbt/misc.h" /* BEGIN_DECL */
14 #include "xbt/log.h"
15
16 BEGIN_DECL()
17
18 /** @addtogroup XBT_error 
19  *
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.
22  *
23  *  @{*/
24
25 /** @name 1. Type definition and basic operations
26  *
27  *  @{
28  */
29 /** \brief Error types */
30 typedef enum {
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 */
38      
39 } xbt_error_t;
40
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;
44
45 /** @} */
46
47 /** @name 2. TRY macro family
48  * 
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
52  * @{
53  */
54
55 /** @brief return the error code if != no_error
56  *  @hideinitializer
57  */
58 #define TRYOLD(action) do {                                       \
59   if ((errcode=action) != no_error) {                          \
60      ERROR1("'%s' error raising...", xbt_error_name(errcode)); \
61      return errcode;                                           \
62   } } while (0)
63    
64 /** @brief return the error code if != no_error and != \a catched
65  *  @hideinitializer
66  */
67 #define TRYCATCH(action,catched) if ((errcode=action) != no_error && errcode !=catched) return errcode
68
69 /** @brief xbt_abort if the error code != no_error
70  *  @hideinitializer
71  */
72 #define TRYFAIL(action) do {                                   \
73   if ((errcode=action) != no_error) {                          \
74      ERROR1("Got '%s' error !", xbt_error_name(errcode));      \
75      fflush(stdout);                                           \
76      xbt_abort();                                              \
77   } } while(0)
78
79
80 /** @brief return the error code if != \a expected_error (no_error not ok)
81  *  @hideinitializer
82  */
83 #define TRYEXPECT(action,expected_error)  do {                 \
84   errcode=action;                                              \
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));                  \
89     xbt_abort();                                              \
90   }                                                            \
91 } while(0)
92
93 /** @}*/
94
95 #define _XBT_ERR_PRE do {
96 #define _XBT_ERR_POST(code)                                    \
97   return code;                                                 \
98 } while (0)
99   
100
101 /** @name 3. RAISE macro family
102  *
103  *  Return a error code, doing some logs on stderr.
104  *
105  *  @todo This should use the logging features, not stderr
106  * 
107  *  @{
108  */
109
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)
124
125 /** @} */
126 /** 
127  * \name 4. assert macro familly
128  *
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.
131  *
132  * @{
133  */
134 #ifdef NDEBUG
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)
143 #else
144 /** @brief The condition which failed will be displayed.
145     @hideinitializer  */
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(); }
161 #endif
162
163 /** @} */
164
165 /** @name 5. Useful predefined errors 
166  *
167  *  @{ 
168  */
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__)
171
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))
174
175 /** @} */
176 /** @} */
177
178 END_DECL()
179
180 #endif /* XBT_ERROR_H */