Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
4c283f7b79c9a7884df99698711b0df105b76dcf
[simgrid.git] / include / xbt / error.h
1 /* $Id$ */
2
3 /* gras/error.h - Error tracking support                                    */
4
5 /* Authors: Martin Quinson                                                  */
6 /* Copyright (C) 2003 the OURAGAN project.                                  */
7
8 /* This program is free software; you can redistribute it and/or modify it
9    under the terms of the license (GNU LGPL) which comes with this package. */
10
11
12 #ifndef GRAS_ERROR_H
13 #define GRAS_ERROR_H
14
15 #include <stddef.h>    /* offsetof() */
16 #include <sys/types.h>  /* size_t */
17 #include <stdarg.h>
18
19 #include <stdio.h> /* FIXME: Get rid of it */
20
21 #ifdef HAVE_EXECINFO_H
22 #include <execinfo.h>  /* to print the backtrace */
23 #endif
24
25 /* C++ users need love */
26 #ifndef BEGIN_DECL
27 # ifdef __cplusplus
28 #  define BEGIN_DECL extern "C" {
29 # else
30 #  define BEGIN_DECL 
31 # endif
32 #endif
33
34 /*! C++ users need love */
35 #ifndef END_DECL
36 # ifdef __cplusplus
37 #  define END_DECL }
38 # else
39 #  define END_DECL 
40 # endif
41 #endif
42 /* End of cruft for C++ */
43
44 BEGIN_DECL
45
46 typedef enum {
47   no_error=0,     /* succes */
48 //  malloc_error,   /* Well known error */
49   mismatch_error, /* The provided ID does not match */
50   system_error,   /* a syscall did fail */
51   network_error,  /* error while sending/receiving data */
52   timeout_error,  /* not quick enough, dude */
53   thread_error,   /* error while [un]locking */
54   unknown_error 
55 } gras_error_t;
56
57 /*@observer@*/ const char *gras_error_name(gras_error_t errcode);
58
59 #define TRY(a) do {                                     \
60   if ((errcode=a) != no_error) {                        \
61      fprintf (stderr, "%s:%d: '%s' error raising...\n", \
62              __FILE__,__LINE__,                         \
63              gras_error_name(errcode));                 \
64      return errcode;                                    \
65   } } while (0)
66    
67 #define TRYCATCH(a,b) if ((errcode=a) != no_error && errcode !=b) return errcode
68 #define TRYFAIL(a) do {                                        \
69   if ((errcode=a) != no_error) {                               \
70      fprintf(stderr,"%s:%d: Got '%s' error !\n",               \
71              __FILE__,__LINE__,                                \
72              gras_error_name(errcode));                        \
73      fflush(stdout);                                           \
74      gras_abort();                                             \
75   } } while(0)
76
77 #define TRYEXPECT(action,expected_error)  do {                 \
78   errcode=action;                                              \
79   if (errcode != expected_error) {                             \
80     fprintf(stderr,"Got error %s (instead of %s expected)\n",  \
81             gras_error_name(errcode),                          \
82             gras_error_name(expected_error));                  \
83     gras_abort();                                              \
84   }                                                            \
85 } while(0)
86
87 /* FIXME TRYCLEAN should be avoided for readability */
88 #define TRYCLEAN(action,cleanup) do {                          \
89   if ((errcode=action) != no_error) {                          \
90     cleanup;                                                   \
91     return errcode;                                            \
92   }                                                            \
93 } while(0)
94
95 #if 0 /* FIXME: We don't use backtrace. Drop it? */
96 #define _GRAS_ERR_PRE do {                                     \
97  void *_gs_array[30];                                          \
98   size_t _gs_size= backtrace (_gs_array, 30);                  \
99   char **_gs_strings= backtrace_symbols (_gs_array, _gs_size); \
100   size_t _gs_i;
101
102 #define _GRAS_ERR_POST(code)                                   \
103   fprintf(stderr,"Backtrace follows\n");                       \
104   for (_gs_i = 0; _gs_i < _gs_size; _gs_i++)                   \
105      fprintf (stderr,"   %s\n", _gs_strings[_gs_i]);           \
106   return code;                                                 \
107 } while (0)
108
109 #else
110 #define _GRAS_ERR_PRE do {
111 #define _GRAS_ERR_POST(code)                                   \
112   return code;                                                 \
113 } while (0)
114 #endif
115
116 #define RAISE0(code,fmt) _GRAS_ERR_PRE     \
117   fprintf(stderr,"%s:%d:%s: " fmt "\n",    \
118           __FILE__,__LINE__,__FUNCTION__); \
119   _GRAS_ERR_POST(code)
120 #define RAISE1(code,fmt,a1) _GRAS_ERR_PRE     \
121   fprintf(stderr,"%s:%d:%s: " fmt "\n",       \
122           __FILE__,__LINE__,__FUNCTION__,a1); \
123   _GRAS_ERR_POST(code)
124 #define RAISE2(code,fmt,a1,a2) _GRAS_ERR_PRE     \
125   fprintf(stderr,"%s:%d:%s: " fmt "\n",          \
126           __FILE__,__LINE__,__FUNCTION__,a1,a2); \
127   _GRAS_ERR_POST(code)
128 #define RAISE3(code,fmt,a1,a2,a3) _GRAS_ERR_PRE     \
129   fprintf(stderr,"%s:%d:%s: " fmt "\n",             \
130           __FILE__,__LINE__,__FUNCTION__,a1,a2,a3); \
131   _GRAS_ERR_POST(code)
132 #define RAISE4(code,fmt,a1,a2,a3,a4) _GRAS_ERR_PRE     \
133   fprintf(stderr,"%s:%d:%s: " fmt "\n",                \
134           __FILE__,__LINE__,__FUNCTION__,a1,a2,a3,a4); \
135   _GRAS_ERR_POST(code)
136 #define RAISE5(code,fmt,a1,a2,a3,a4,a5) _GRAS_ERR_PRE     \
137   fprintf(stderr,"%s:%d:%s: " fmt "\n",                   \
138           __FILE__,__LINE__,__FUNCTION__,a1,a2,a3,a4,a5); \
139   _GRAS_ERR_POST(code)
140 #define RAISE6(code,fmt,a1,a2,a3,a4,a5,a6) _GRAS_ERR_PRE     \
141   fprintf(stderr,"%s:%d:%s: " fmt "\n",                      \
142           __FILE__,__LINE__,__FUNCTION__,a1,a2,a3,a4,a5,a6); \
143   _GRAS_ERR_POST(code)
144
145 //#define RAISE_MALLOC     RAISE0(malloc_error,"Malloc error")
146 #define RAISE_IMPOSSIBLE RAISE0(unknown_error,"The Impossible did happen")
147 #define RAISE_UNIMPLEMENTED RAISE1(unknown_error,"Function %s unimplemented",__FUNCTION__)
148
149 #ifdef NDEBUG
150 #define gras_assert(cond)
151 #define gras_assert0(cond,msg)
152 #define gras_assert1(cond,msg,a)
153 #define gras_assert2(cond,msg,a,b)
154 #define gras_assert3(cond,msg,a,b,c)
155 #define gras_assert4(cond,msg,a,b,c,d)
156 #define gras_assert5(cond,msg,a,b,c,d,e)
157 #define gras_assert6(cond,msg,a,b,c,d,e,f)
158 #else
159 #define gras_assert(cond)                  if (!(cond)) { CRITICAL1("Assertion %s failed", #cond); gras_abort(); }
160 #define gras_assert0(cond,msg)             if (!(cond)) { CRITICAL0(msg); gras_abort(); }
161 #define gras_assert1(cond,msg,a)           if (!(cond)) { CRITICAL1(msg,a); gras_abort(); }
162 #define gras_assert2(cond,msg,a,b)         if (!(cond)) { CRITICAL2(msg,a,b); gras_abort(); }
163 #define gras_assert3(cond,msg,a,b,c)       if (!(cond)) { CRITICAL3(msg,a,b,c); gras_abort(); }
164 #define gras_assert4(cond,msg,a,b,c,d)     if (!(cond)) { CRITICAL4(msg,a,b,c,d); gras_abort(); }
165 #define gras_assert5(cond,msg,a,b,c,d,e)   if (!(cond)) { CRITICAL5(msg,a,b,c,d,e); gras_abort(); }
166 #define gras_assert6(cond,msg,a,b,c,d,e,f) if (!(cond)) { CRITICAL6(msg,a,b,c,d,e,f); gras_abort(); }
167 #endif
168
169 #define DIE_IMPOSSIBLE gras_assert0(0,"The Impossible did happen (yet again)")
170 #define gras_assert_error(a) gras_assert1(errcode == (a), "Error %s unexpected",gras_error_name(errcode))
171
172 END_DECL
173
174 #endif /* GRAS_ERROR_H */
175