Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
c42728c01bd0a4ffd6d237a9c447e309c7ae3716
[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      fprintf (stderr, "%s:%d: '%s' error raising...\n", \
71              __FILE__,__LINE__,                         \
72              xbt_error_name(errcode));                  \
73      return errcode;                                    \
74   } } while (0)
75    
76 /** @brief return the error code if != no_error and != \a catched
77  *  @hideinitializer
78  */
79 #define TRYCATCH(action,catched) if ((errcode=action) != no_error && errcode !=catched) return errcode
80
81 /** @brief xbt_abort if the error code != no_error
82  *  @hideinitializer
83  */
84 #define TRYFAIL(action) do {                                   \
85   if ((errcode=action) != no_error) {                          \
86      fprintf(stderr,"%s:%d: Got '%s' error !\n",               \
87              __FILE__,__LINE__,                                \
88              xbt_error_name(errcode));                         \
89      fflush(stdout);                                           \
90      xbt_abort();                                              \
91   } } while(0)
92
93
94 /** @brief return the error code if != \a expected_error (no_error not ok)
95  *  @hideinitializer
96  */
97 #define TRYEXPECT(action,expected_error)  do {                 \
98   errcode=action;                                              \
99   if (errcode != expected_error) {                             \
100     fprintf(stderr,"Got error %s (instead of %s expected)\n",  \
101             xbt_error_name(errcode),                          \
102             xbt_error_name(expected_error));                  \
103     xbt_abort();                                              \
104   }                                                            \
105 } while(0)
106
107 /** @}*/
108
109 #define _XBT_ERR_PRE do {
110 #define _XBT_ERR_POST(code)                                    \
111   return code;                                                 \
112 } while (0)
113   
114
115 /** @name 3. RAISE macro family
116  *
117  *  Return a error code, doing some logs on stderr.
118  *
119  *  @todo This should use the logging features, not stderr
120  * 
121  *  @{
122  */
123
124 /** @hideinitializer  */
125 #define RAISE0(code,fmt)                   _XBT_ERR_PRE   ERROR0(fmt);                   _XBT_ERR_POST(code)
126 /** @hideinitializer  */
127 #define RAISE1(code,fmt,a1)                _XBT_ERR_PRE   ERROR1(fmt,a1);                _XBT_ERR_POST(code)
128 /** @hideinitializer  */
129 #define RAISE2(code,fmt,a1,a2)             _XBT_ERR_PRE   ERROR2(fmt,a1,a2);             _XBT_ERR_POST(code)
130 /** @hideinitializer  */
131 #define RAISE3(code,fmt,a1,a2,a3)          _XBT_ERR_PRE   ERROR3(fmt,a1,a2,a3);          _XBT_ERR_POST(code)
132 /** @hideinitializer  */
133 #define RAISE4(code,fmt,a1,a2,a3,a4)       _XBT_ERR_PRE   ERROR4(fmt,a1,a2,a3,a4);       _XBT_ERR_POST(code)
134 /** @hideinitializer  */
135 #define RAISE5(code,fmt,a1,a2,a3,a4,a5)    _XBT_ERR_PRE   ERROR5(fmt,a1,a2,a3,a4,a5);    _XBT_ERR_POST(code)
136 /** @hideinitializer  */
137 #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)
138
139 /** @} */
140 /** 
141  * \name 4. assert macro familly
142  *
143  * Those are the GRAS version of the good ol' assert macro. You can pass them a format message and 
144  * arguments, just as if it where a printf. It is converted to a CRITICALn logging request.
145  *
146  * @{
147  */
148 #ifdef NDEBUG
149 #define xbt_assert(cond)
150 #define xbt_assert0(cond,msg)
151 #define xbt_assert1(cond,msg,a)
152 #define xbt_assert2(cond,msg,a,b)
153 #define xbt_assert3(cond,msg,a,b,c)
154 #define xbt_assert4(cond,msg,a,b,c,d)
155 #define xbt_assert5(cond,msg,a,b,c,d,e)
156 #define xbt_assert6(cond,msg,a,b,c,d,e,f)
157 #else
158 /** @brief The condition which failed will be displayed.
159     @hideinitializer  */
160 #define xbt_assert(cond)                  if (!(cond)) { CRITICAL1("Assertion %s failed", #cond); xbt_abort(); }
161 /** @hideinitializer  */
162 #define xbt_assert0(cond,msg)             if (!(cond)) { CRITICAL0(msg); xbt_abort(); }
163 /** @hideinitializer  */
164 #define xbt_assert1(cond,msg,a)           if (!(cond)) { CRITICAL1(msg,a); xbt_abort(); }
165 /** @hideinitializer  */
166 #define xbt_assert2(cond,msg,a,b)         if (!(cond)) { CRITICAL2(msg,a,b); xbt_abort(); }
167 /** @hideinitializer  */
168 #define xbt_assert3(cond,msg,a,b,c)       if (!(cond)) { CRITICAL3(msg,a,b,c); xbt_abort(); }
169 /** @hideinitializer  */
170 #define xbt_assert4(cond,msg,a,b,c,d)     if (!(cond)) { CRITICAL4(msg,a,b,c,d); xbt_abort(); }
171 /** @hideinitializer  */
172 #define xbt_assert5(cond,msg,a,b,c,d,e)   if (!(cond)) { CRITICAL5(msg,a,b,c,d,e); xbt_abort(); }
173 /** @hideinitializer  */
174 #define xbt_assert6(cond,msg,a,b,c,d,e,f) if (!(cond)) { CRITICAL6(msg,a,b,c,d,e,f); xbt_abort(); }
175 #endif
176
177 /** @} */
178
179 /** @name 5. Useful predefined errors 
180  *
181  *  @{ 
182  */
183 #define RAISE_IMPOSSIBLE RAISE0(unknown_error,"The Impossible did happen")
184 #define RAISE_UNIMPLEMENTED RAISE1(unknown_error,"Function %s unimplemented",__FUNCTION__)
185
186 #define DIE_IMPOSSIBLE xbt_assert0(0,"The Impossible did happen (yet again)")
187 #define xbt_assert_error(a) xbt_assert1(errcode == (a), "Error %s unexpected",xbt_error_name(errcode))
188
189 /** @} */
190 /** @} */
191
192 END_DECL()
193
194 #endif /* XBT_ERROR_H */