Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Missing chunks
[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,2004 da GRAS posse.                                   */
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 <stdio.h> /* FIXME: Get rid of it */
16
17 #include "xbt/misc.h" /* BEGIN_DECL */
18 #include "xbt/log.h"
19
20 #ifdef HAVE_EXECINFO_H
21 #include <execinfo.h>  /* to print the backtrace */
22 #endif
23
24 BEGIN_DECL
25
26 typedef enum {
27   no_error=0,       /* succes */
28   mismatch_error=1, /* The provided ID does not match */
29   system_error=2,   /* a syscall did fail */
30   network_error=3,  /* error while sending/receiving data */
31   timeout_error=4,  /* not quick enough, dude */
32   thread_error=5,   /* error while [un]locking */
33   unknown_error=6,
34      
35   /* remote errors: result of a RMI/RPC.
36      no_error(=0) is the same for both */   
37   remote_mismatch_error=129,
38   remote_system_error,
39   remote_network_error,
40   remote_timeout_error,
41   remote_thread_error,
42   remote_unknown_error
43 } gras_error_t;
44
45 /*@observer@*/ const char *gras_error_name(gras_error_t errcode);
46
47 #define TRY(a) do {                                     \
48   if ((errcode=a) != no_error) {                        \
49      fprintf (stderr, "%s:%d: '%s' error raising...\n", \
50              __FILE__,__LINE__,                         \
51              gras_error_name(errcode));                 \
52      return errcode;                                    \
53   } } while (0)
54    
55 #define TRYCATCH(a,b) if ((errcode=a) != no_error && errcode !=b) return errcode
56 #define TRYFAIL(a) do {                                        \
57   if ((errcode=a) != no_error) {                               \
58      fprintf(stderr,"%s:%d: Got '%s' error !\n",               \
59              __FILE__,__LINE__,                                \
60              gras_error_name(errcode));                        \
61      fflush(stdout);                                           \
62      gras_abort();                                             \
63   } } while(0)
64
65 #define TRYEXPECT(action,expected_error)  do {                 \
66   errcode=action;                                              \
67   if (errcode != expected_error) {                             \
68     fprintf(stderr,"Got error %s (instead of %s expected)\n",  \
69             gras_error_name(errcode),                          \
70             gras_error_name(expected_error));                  \
71     gras_abort();                                              \
72   }                                                            \
73 } while(0)
74
75 /* FIXME TRYCLEAN should be avoided for readability */
76 #define TRYCLEAN(action,cleanup) do {                          \
77   if ((errcode=action) != no_error) {                          \
78     cleanup;                                                   \
79     return errcode;                                            \
80   }                                                            \
81 } while(0)
82
83 #if 0 /* FIXME: We don't use backtrace. Drop it? */
84 #define _GRAS_ERR_PRE do {                                     \
85  void *_gs_array[30];                                          \
86   size_t _gs_size= backtrace (_gs_array, 30);                  \
87   char **_gs_strings= backtrace_symbols (_gs_array, _gs_size); \
88   size_t _gs_i;
89
90 #define _GRAS_ERR_POST(code)                                   \
91   fprintf(stderr,"Backtrace follows\n");                       \
92   for (_gs_i = 0; _gs_i < _gs_size; _gs_i++)                   \
93      fprintf (stderr,"   %s\n", _gs_strings[_gs_i]);           \
94   return code;                                                 \
95 } while (0)
96
97 #else /* if 0 */
98 #define _GRAS_ERR_PRE do {
99 #define _GRAS_ERR_POST(code)                                   \
100   return code;                                                 \
101 } while (0)
102 #endif
103
104 #define RAISE0(code,fmt) _GRAS_ERR_PRE     \
105   fprintf(stderr,"%s:%d:%s: " fmt "\n",    \
106           __FILE__,__LINE__,__FUNCTION__); \
107   _GRAS_ERR_POST(code)
108 #define RAISE1(code,fmt,a1) _GRAS_ERR_PRE     \
109   fprintf(stderr,"%s:%d:%s: " fmt "\n",       \
110           __FILE__,__LINE__,__FUNCTION__,a1); \
111   _GRAS_ERR_POST(code)
112 #define RAISE2(code,fmt,a1,a2) _GRAS_ERR_PRE     \
113   fprintf(stderr,"%s:%d:%s: " fmt "\n",          \
114           __FILE__,__LINE__,__FUNCTION__,a1,a2); \
115   _GRAS_ERR_POST(code)
116 #define RAISE3(code,fmt,a1,a2,a3) _GRAS_ERR_PRE     \
117   fprintf(stderr,"%s:%d:%s: " fmt "\n",             \
118           __FILE__,__LINE__,__FUNCTION__,a1,a2,a3); \
119   _GRAS_ERR_POST(code)
120 #define RAISE4(code,fmt,a1,a2,a3,a4) _GRAS_ERR_PRE     \
121   fprintf(stderr,"%s:%d:%s: " fmt "\n",                \
122           __FILE__,__LINE__,__FUNCTION__,a1,a2,a3,a4); \
123   _GRAS_ERR_POST(code)
124 #define RAISE5(code,fmt,a1,a2,a3,a4,a5) _GRAS_ERR_PRE     \
125   fprintf(stderr,"%s:%d:%s: " fmt "\n",                   \
126           __FILE__,__LINE__,__FUNCTION__,a1,a2,a3,a4,a5); \
127   _GRAS_ERR_POST(code)
128 #define RAISE6(code,fmt,a1,a2,a3,a4,a5,a6) _GRAS_ERR_PRE     \
129   fprintf(stderr,"%s:%d:%s: " fmt "\n",                      \
130           __FILE__,__LINE__,__FUNCTION__,a1,a2,a3,a4,a5,a6); \
131   _GRAS_ERR_POST(code)
132
133 //#define RAISE_MALLOC     RAISE0(malloc_error,"Malloc error")
134 #define RAISE_IMPOSSIBLE RAISE0(unknown_error,"The Impossible did happen")
135 #define RAISE_UNIMPLEMENTED RAISE1(unknown_error,"Function %s unimplemented",__FUNCTION__)
136
137 #ifdef NDEBUG
138 #define gras_assert(cond)
139 #define gras_assert0(cond,msg)
140 #define gras_assert1(cond,msg,a)
141 #define gras_assert2(cond,msg,a,b)
142 #define gras_assert3(cond,msg,a,b,c)
143 #define gras_assert4(cond,msg,a,b,c,d)
144 #define gras_assert5(cond,msg,a,b,c,d,e)
145 #define gras_assert6(cond,msg,a,b,c,d,e,f)
146 #else
147 #define gras_assert(cond)                  if (!(cond)) { CRITICAL1("Assertion %s failed", #cond); gras_abort(); }
148 #define gras_assert0(cond,msg)             if (!(cond)) { CRITICAL0(msg); gras_abort(); }
149 #define gras_assert1(cond,msg,a)           if (!(cond)) { CRITICAL1(msg,a); gras_abort(); }
150 #define gras_assert2(cond,msg,a,b)         if (!(cond)) { CRITICAL2(msg,a,b); gras_abort(); }
151 #define gras_assert3(cond,msg,a,b,c)       if (!(cond)) { CRITICAL3(msg,a,b,c); gras_abort(); }
152 #define gras_assert4(cond,msg,a,b,c,d)     if (!(cond)) { CRITICAL4(msg,a,b,c,d); gras_abort(); }
153 #define gras_assert5(cond,msg,a,b,c,d,e)   if (!(cond)) { CRITICAL5(msg,a,b,c,d,e); gras_abort(); }
154 #define gras_assert6(cond,msg,a,b,c,d,e,f) if (!(cond)) { CRITICAL6(msg,a,b,c,d,e,f); gras_abort(); }
155 #endif
156
157 void gras_die(const char *msg);
158
159 #define DIE_IMPOSSIBLE gras_assert0(0,"The Impossible did happen (yet again)")
160 #define gras_assert_error(a) gras_assert1(errcode == (a), "Error %s unexpected",gras_error_name(errcode))
161
162 END_DECL
163
164 #endif /* GRAS_ERROR_H */
165