Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
s/_XBT_GNUC_FUNCTION/_XBT_FUNCTION/ (that's how to get __FUNCTION__ portably)
[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 /** @addtogroup XBT_error 
21  *
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.
24  *
25  *  @{*/
26
27 /** @name 1. Type definition and basic operations
28  *
29  *  @{
30  */
31 /** \brief Error types */
32 typedef enum {
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 */
40      
41   /* remote errors: result of a RMI/RPC.
42      no_error(=0) is the same for both */   
43   remote_mismatch_error=129,
44   remote_system_error,
45   remote_network_error,
46   remote_timeout_error,
47   remote_thread_error,
48   remote_unknown_error
49 } xbt_error_t;
50
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;
54
55 /** @} */
56
57 /** @name 2. TRY macro family
58  * 
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
62  * @{
63  */
64
65 /** @brief return the error code if != no_error
66  *  @hideinitializer
67  */
68 #define TRY(action) do {                                       \
69   if ((errcode=action) != no_error) {                          \
70      ERROR1("'%s' error raising...", xbt_error_name(errcode)); \
71      return errcode;                                           \
72   } } while (0)
73    
74 /** @brief return the error code if != no_error and != \a catched
75  *  @hideinitializer
76  */
77 #define TRYCATCH(action,catched) if ((errcode=action) != no_error && errcode !=catched) return errcode
78
79 /** @brief xbt_abort if the error code != no_error
80  *  @hideinitializer
81  */
82 #define TRYFAIL(action) do {                                   \
83   if ((errcode=action) != no_error) {                          \
84      ERROR1("Got '%s' error !", xbt_error_name(errcode));      \
85      fflush(stdout);                                           \
86      xbt_abort();                                              \
87   } } while(0)
88
89
90 /** @brief return the error code if != \a expected_error (no_error not ok)
91  *  @hideinitializer
92  */
93 #define TRYEXPECT(action,expected_error)  do {                 \
94   errcode=action;                                              \
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));                  \
99     xbt_abort();                                              \
100   }                                                            \
101 } while(0)
102
103 /** @}*/
104
105 #define _XBT_ERR_PRE do {
106 #define _XBT_ERR_POST(code)                                    \
107   return code;                                                 \
108 } while (0)
109   
110
111 /** @name 3. RAISE macro family
112  *
113  *  Return a error code, doing some logs on stderr.
114  *
115  *  @todo This should use the logging features, not stderr
116  * 
117  *  @{
118  */
119
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)
134
135 /** @} */
136 /** 
137  * \name 4. assert macro familly
138  *
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.
141  *
142  * @{
143  */
144 #ifdef NDEBUG
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)
153 #else
154 /** @brief The condition which failed will be displayed.
155     @hideinitializer  */
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(); }
171 #endif
172
173 /** @} */
174
175 /** @name 5. Useful predefined errors 
176  *
177  *  @{ 
178  */
179 #define RAISE_IMPOSSIBLE RAISE0(unknown_error,"The Impossible did happen")
180 #define RAISE_UNIMPLEMENTED RAISE1(unknown_error,"Function %s unimplemented",__FUNCTION__)
181
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))
184
185 /** @} */
186 /** @} */
187
188 END_DECL()
189
190 #endif /* XBT_ERROR_H */