Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
08647392a0205b3628e9440702f20e8535f095ab
[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 #ifdef HAVE_EXECINFO_H
19 #include <execinfo.h>  /* to print the backtrace */
20 #endif
21
22 BEGIN_DECL()
23
24 #define _XBT_ERR_PRE do {
25 #define _XBT_ERR_POST(code)                                    \
26   return code;                                                 \
27 } while (0)
28   
29 /** @addtogroup XBT_error 
30  *  @{*/
31
32 /** \brief Kill the programm in silence */
33 void xbt_abort(void) _XBT_GNUC_NORETURN;
34
35 /** \brief Kill the programm with an error message */
36 void xbt_die(const char *msg) _XBT_GNUC_NORETURN;
37
38
39 /** \brief Error types */
40 typedef enum {
41   no_error=0,       /**< succes */
42   mismatch_error=1, /**< The provided ID does not match */
43   system_error=2,   /**< a syscall did fail */
44   network_error=3,  /**< error while sending/receiving data */
45   timeout_error=4,  /**< not quick enough, dude */
46   thread_error=5,   /**< error while [un]locking */
47   unknown_error=6,  /**< unknown error */
48      
49   /* remote errors: result of a RMI/RPC.
50      no_error(=0) is the same for both */   
51   remote_mismatch_error=129,
52   remote_system_error,
53   remote_network_error,
54   remote_timeout_error,
55   remote_thread_error,
56   remote_unknown_error
57 } xbt_error_t;
58
59 const char *xbt_error_name(xbt_error_t errcode);
60
61 /** @name TRY macro family
62  * 
63  * Those functions are used to launch a function call and react automatically 
64  * to its return value. They expect such a variable to be declared in the scope:
65  * \verbatim xbt_error_t errcode;\endverbatim
66  * @{
67  */
68
69 /** @brief return the error code if != no_error
70  *  @hideinitializer
71  */
72 #define TRY(action) do {                                \
73   if ((errcode=action) != no_error) {                   \
74      fprintf (stderr, "%s:%d: '%s' error raising...\n", \
75              __FILE__,__LINE__,                         \
76              xbt_error_name(errcode));                  \
77      return errcode;                                    \
78   } } while (0)
79    
80 /** @brief return the error code if != no_error and != \a catched
81  *  @hideinitializer
82  */
83 #define TRYCATCH(action,catched) if ((errcode=action) != no_error && errcode !=catched) return errcode
84
85 /** @brief xbt_abort if the error code != no_error
86  *  @hideinitializer
87  */
88 #define TRYFAIL(action) do {                                   \
89   if ((errcode=action) != no_error) {                          \
90      fprintf(stderr,"%s:%d: Got '%s' error !\n",               \
91              __FILE__,__LINE__,                                \
92              xbt_error_name(errcode));                         \
93      fflush(stdout);                                           \
94      xbt_abort();                                              \
95   } } while(0)
96
97
98 /** @brief return the error code if != \a expected_error (no_error not ok)
99  *  @hideinitializer
100  */
101 #define TRYEXPECT(action,expected_error)  do {                 \
102   errcode=action;                                              \
103   if (errcode != expected_error) {                             \
104     fprintf(stderr,"Got error %s (instead of %s expected)\n",  \
105             xbt_error_name(errcode),                          \
106             xbt_error_name(expected_error));                  \
107     xbt_abort();                                              \
108   }                                                            \
109 } while(0)
110
111 /** @}*/
112 /** @name RAISE macro family
113  *
114  *  Return a error code, doing some logs on stderr.
115  * 
116  *  @{
117  */
118
119 /** @hideinitializer  */
120 #define RAISE0(code,fmt) _XBT_ERR_PRE     \
121   fprintf(stderr,"%s:%d:%s: " fmt "\n",    \
122           __FILE__,__LINE__,__FUNCTION__); \
123   _XBT_ERR_POST(code)
124 /** @hideinitializer  */
125 #define RAISE1(code,fmt,a1) _XBT_ERR_PRE     \
126   fprintf(stderr,"%s:%d:%s: " fmt "\n",       \
127           __FILE__,__LINE__,__FUNCTION__,a1); \
128   _XBT_ERR_POST(code)
129 /** @hideinitializer  */
130 #define RAISE2(code,fmt,a1,a2) _XBT_ERR_PRE     \
131   fprintf(stderr,"%s:%d:%s: " fmt "\n",          \
132           __FILE__,__LINE__,__FUNCTION__,a1,a2); \
133   _XBT_ERR_POST(code)
134 /** @hideinitializer  */
135 #define RAISE3(code,fmt,a1,a2,a3) _XBT_ERR_PRE     \
136   fprintf(stderr,"%s:%d:%s: " fmt "\n",             \
137           __FILE__,__LINE__,__FUNCTION__,a1,a2,a3); \
138   _XBT_ERR_POST(code)
139 /** @hideinitializer  */
140 #define RAISE4(code,fmt,a1,a2,a3,a4) _XBT_ERR_PRE     \
141   fprintf(stderr,"%s:%d:%s: " fmt "\n",                \
142           __FILE__,__LINE__,__FUNCTION__,a1,a2,a3,a4); \
143   _XBT_ERR_POST(code)
144 /** @hideinitializer  */
145 #define RAISE5(code,fmt,a1,a2,a3,a4,a5) _XBT_ERR_PRE     \
146   fprintf(stderr,"%s:%d:%s: " fmt "\n",                   \
147           __FILE__,__LINE__,__FUNCTION__,a1,a2,a3,a4,a5); \
148   _XBT_ERR_POST(code)
149 /** @hideinitializer  */
150 #define RAISE6(code,fmt,a1,a2,a3,a4,a5,a6) _XBT_ERR_PRE     \
151   fprintf(stderr,"%s:%d:%s: " fmt "\n",                      \
152           __FILE__,__LINE__,__FUNCTION__,a1,a2,a3,a4,a5,a6); \
153   _XBT_ERR_POST(code)
154
155 /**@}*/
156 /** 
157  * \name assert macro familly
158  *
159  * Those are the GRAS version of the good ol' assert macro. You can pass them a format message and 
160  * arguments, just as if it where a printf.
161  *
162  * @{
163  */
164 #ifdef NDEBUG
165 #define xbt_assert(cond)
166 #define xbt_assert0(cond,msg)
167 #define xbt_assert1(cond,msg,a)
168 #define xbt_assert2(cond,msg,a,b)
169 #define xbt_assert3(cond,msg,a,b,c)
170 #define xbt_assert4(cond,msg,a,b,c,d)
171 #define xbt_assert5(cond,msg,a,b,c,d,e)
172 #define xbt_assert6(cond,msg,a,b,c,d,e,f)
173 #else
174 /** @hideinitializer  */
175 #define xbt_assert(cond)                  if (!(cond)) { CRITICAL1("Assertion %s failed", #cond); xbt_abort(); }
176 /** @hideinitializer  */
177 #define xbt_assert0(cond,msg)             if (!(cond)) { CRITICAL0(msg); xbt_abort(); }
178 /** @hideinitializer  */
179 #define xbt_assert1(cond,msg,a)           if (!(cond)) { CRITICAL1(msg,a); xbt_abort(); }
180 /** @hideinitializer  */
181 #define xbt_assert2(cond,msg,a,b)         if (!(cond)) { CRITICAL2(msg,a,b); xbt_abort(); }
182 /** @hideinitializer  */
183 #define xbt_assert3(cond,msg,a,b,c)       if (!(cond)) { CRITICAL3(msg,a,b,c); xbt_abort(); }
184 /** @hideinitializer  */
185 #define xbt_assert4(cond,msg,a,b,c,d)     if (!(cond)) { CRITICAL4(msg,a,b,c,d); xbt_abort(); }
186 /** @hideinitializer  */
187 #define xbt_assert5(cond,msg,a,b,c,d,e)   if (!(cond)) { CRITICAL5(msg,a,b,c,d,e); xbt_abort(); }
188 /** @hideinitializer  */
189 #define xbt_assert6(cond,msg,a,b,c,d,e,f) if (!(cond)) { CRITICAL6(msg,a,b,c,d,e,f); xbt_abort(); }
190 #endif
191
192 /** @}*/
193
194 /** @name Useful predefined errors 
195  *
196  *  @{ 
197  */
198 #define RAISE_IMPOSSIBLE RAISE0(unknown_error,"The Impossible did happen")
199 #define RAISE_UNIMPLEMENTED RAISE1(unknown_error,"Function %s unimplemented",__FUNCTION__)
200
201 #define DIE_IMPOSSIBLE xbt_assert0(0,"The Impossible did happen (yet again)")
202 #define xbt_assert_error(a) xbt_assert1(errcode == (a), "Error %s unexpected",xbt_error_name(errcode))
203
204 /** @}*/
205 /**@}*/
206
207 END_DECL()
208
209 #endif /* XBT_ERROR_H */