Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
06a58aa3c083f65bdf531738e1cb567fb86ede23
[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 <stdio.h> /* FIXME: Get rid of it */
14
15 #include "xbt/misc.h" /* BEGIN_DECL */
16 #include "xbt/log.h"
17
18 BEGIN_DECL()
19
20 #define _XBT_ERR_PRE do {
21 #define _XBT_ERR_POST(code)                                    \
22   return code;                                                 \
23 } while (0)
24   
25 /** @addtogroup XBT_error 
26  *
27  *  This is how the errors get reported in the SimGrid toolkit. This mecanism is not 
28  *  as powerful as exceptions, but since we're using C, there is not much we can do.
29  *
30  *  @{*/
31
32 /** @name 1. Type definition and basic operations
33  *
34  *  @{
35  */
36 /** \brief Error types */
37 typedef enum {
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 */
45      
46   /* remote errors: result of a RMI/RPC.
47      no_error(=0) is the same for both */   
48   remote_mismatch_error=129,
49   remote_system_error,
50   remote_network_error,
51   remote_timeout_error,
52   remote_thread_error,
53   remote_unknown_error
54 } xbt_error_t;
55
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;
59
60 /** @} */
61
62 /** @name 2. TRY macro family
63  * 
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
67  * @{
68  */
69
70 /** @brief return the error code if != no_error
71  *  @hideinitializer
72  */
73 #define TRY(action) do {                                \
74   if ((errcode=action) != no_error) {                   \
75      fprintf (stderr, "%s:%d: '%s' error raising...\n", \
76              __FILE__,__LINE__,                         \
77              xbt_error_name(errcode));                  \
78      return errcode;                                    \
79   } } while (0)
80    
81 /** @brief return the error code if != no_error and != \a catched
82  *  @hideinitializer
83  */
84 #define TRYCATCH(action,catched) if ((errcode=action) != no_error && errcode !=catched) return errcode
85
86 /** @brief xbt_abort if the error code != no_error
87  *  @hideinitializer
88  */
89 #define TRYFAIL(action) do {                                   \
90   if ((errcode=action) != no_error) {                          \
91      fprintf(stderr,"%s:%d: Got '%s' error !\n",               \
92              __FILE__,__LINE__,                                \
93              xbt_error_name(errcode));                         \
94      fflush(stdout);                                           \
95      xbt_abort();                                              \
96   } } while(0)
97
98
99 /** @brief return the error code if != \a expected_error (no_error not ok)
100  *  @hideinitializer
101  */
102 #define TRYEXPECT(action,expected_error)  do {                 \
103   errcode=action;                                              \
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));                  \
108     xbt_abort();                                              \
109   }                                                            \
110 } while(0)
111
112 /** @}*/
113 /** @name 3. RAISE macro family
114  *
115  *  Return a error code, doing some logs on stderr.
116  *
117  *  @todo This should use the logging features, not stderr
118  * 
119  *  @{
120  */
121
122 /** @hideinitializer  */
123 #define RAISE0(code,fmt) _XBT_ERR_PRE     \
124   fprintf(stderr,"%s:%d:%s: " fmt "\n",    \
125           __FILE__,__LINE__,__FUNCTION__); \
126   _XBT_ERR_POST(code)
127 /** @hideinitializer  */
128 #define RAISE1(code,fmt,a1) _XBT_ERR_PRE     \
129   fprintf(stderr,"%s:%d:%s: " fmt "\n",       \
130           __FILE__,__LINE__,__FUNCTION__,a1); \
131   _XBT_ERR_POST(code)
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); \
136   _XBT_ERR_POST(code)
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); \
141   _XBT_ERR_POST(code)
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); \
146   _XBT_ERR_POST(code)
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); \
151   _XBT_ERR_POST(code)
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); \
156   _XBT_ERR_POST(code)
157
158 /**@}*/
159 /** 
160  * \name 4. assert macro familly
161  *
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.
164  *
165  * @{
166  */
167 #ifdef NDEBUG
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)
176 #else
177 /** @brief The condition which failed will be displayed.
178     @hideinitializer  */
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(); }
194 #endif
195
196 /** @}*/
197
198 /** @name 5. Useful predefined errors 
199  *
200  *  @{ 
201  */
202 #define RAISE_IMPOSSIBLE RAISE0(unknown_error,"The Impossible did happen")
203 #define RAISE_UNIMPLEMENTED RAISE1(unknown_error,"Function %s unimplemented",__FUNCTION__)
204
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))
207
208 /** @}*/
209 /**@}*/
210
211 END_DECL()
212
213 #endif /* XBT_ERROR_H */