Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
of course, xlC does not like gcc attributes. And since this is a leftover, don't...
[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 #error do not use this crap anymore
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   old_mismatch_error=1, /**< The provided ID does not match */
35   old_system_error=2,   /**< a syscall did fail */
36   old_network_error=3,  /**< error while sending/receiving data */
37   old_timeout_error=4,  /**< not quick enough, dude */
38   old_thread_error=5,   /**< error while [un]locking */
39   old_unknown_error=6   /**< unknown error */
40      
41 } xbt_error_t;
42
43  const char *xbt_error_name(xbt_error_t errcode);
44  void xbt_abort(void) _XBT_GNUC_NORETURN;
45  void xbt_die(const char *msg) _XBT_GNUC_NORETURN;
46
47 /** @} */
48
49 /** @name 2. TRY macro family
50  * 
51  * Those functions are used to launch a function call and react automatically 
52  * to its return value. They expect such a variable to be declared in the scope:
53  * \verbatim xbt_error_t errcode;\endverbatim
54  * @{
55  */
56
57 /** @brief return the error code if != no_error
58  *  @hideinitializer
59  */
60 #define TRYOLD(action) do {                                       \
61   if ((errcode=action) != no_error) {                          \
62      ERROR1("'%s' error raising...", xbt_error_name(errcode)); \
63      return errcode;                                           \
64   } } while (0)
65    
66 /** @brief return the error code if != no_error and != \a catched
67  *  @hideinitializer
68  */
69 #define TRYCATCH(action,catched) if ((errcode=action) != no_error && errcode !=catched) return errcode
70
71 /** @brief xbt_abort if the error code != no_error
72  *  @hideinitializer
73  */
74 #define TRYFAIL(action) do {                                   \
75   if ((errcode=action) != no_error) {                          \
76      ERROR1("Got '%s' error !", xbt_error_name(errcode));      \
77      fflush(stdout);                                           \
78      xbt_abort();                                              \
79   } } while(0)
80
81
82 /** @brief return the error code if != \a expected_error (no_error not ok)
83  *  @hideinitializer
84  */
85 #define TRYEXPECT(action,expected_error)  do {                 \
86   errcode=action;                                              \
87   if (errcode != expected_error) {                             \
88     ERROR2("Got error %s (instead of %s expected)\n",          \
89             xbt_error_name(errcode),                          \
90             xbt_error_name(expected_error));                  \
91     xbt_abort();                                              \
92   }                                                            \
93 } while(0)
94
95 /** @}*/
96
97 #define _XBT_ERR_PRE do {
98 #define _XBT_ERR_POST(code)                                    \
99   return code;                                                 \
100 } while (0)
101   
102
103 /** @name 3. RAISE macro family
104  *
105  *  Return a error code, doing some logs on stderr.
106  *
107  *  @todo This should use the logging features, not stderr
108  * 
109  *  @{
110  */
111
112 /** @hideinitializer  */
113 #define OLDRAISE0(code,fmt)                   _XBT_ERR_PRE   ERROR0(fmt);                   _XBT_ERR_POST(code)
114 /** @hideinitializer  */
115 #define OLDRAISE1(code,fmt,a1)                _XBT_ERR_PRE   ERROR1(fmt,a1);                _XBT_ERR_POST(code)
116 /** @hideinitializer  */
117 #define OLDRAISE2(code,fmt,a1,a2)             _XBT_ERR_PRE   ERROR2(fmt,a1,a2);             _XBT_ERR_POST(code)
118 /** @hideinitializer  */
119 #define OLDRAISE3(code,fmt,a1,a2,a3)          _XBT_ERR_PRE   ERROR3(fmt,a1,a2,a3);          _XBT_ERR_POST(code)
120 /** @hideinitializer  */
121 #define OLDRAISE4(code,fmt,a1,a2,a3,a4)       _XBT_ERR_PRE   ERROR4(fmt,a1,a2,a3,a4);       _XBT_ERR_POST(code)
122 /** @hideinitializer  */
123 #define OLDRAISE5(code,fmt,a1,a2,a3,a4,a5)    _XBT_ERR_PRE   ERROR5(fmt,a1,a2,a3,a4,a5);    _XBT_ERR_POST(code)
124 /** @hideinitializer  */
125 #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)
126
127 /** @} */
128
129 /** @name 5. Useful predefined errors 
130  *
131  *  @{ 
132  */
133 #define RAISE_IMPOSSIBLE RAISE0(old_unknown_error,"The Impossible did happen (yet again)")
134 #define RAISE_UNIMPLEMENTED RAISE1(old_unknown_error,"Function %s unimplemented",__FUNCTION__)
135
136 #define OLDDIE_IMPOSSIBLE xbt_assert0(0,"The Impossible did happen (yet again)")
137 #define OLDxbt_assert_error(a) xbt_assert1(errcode == (a), "Error %s unexpected",xbt_error_name(errcode))
138
139 /** @} */
140 /** @} */
141
142 END_DECL()
143
144 #endif /* XBT_ERROR_H */