Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
9fc9f8c964947cd1efea7c11875f105503c844d5
[simgrid.git] / include / 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 #include <execinfo.h>  /* to print the backtrace */
19
20
21 /* C++ users need love */
22 #ifndef BEGIN_DECL
23 # ifdef __cplusplus
24 #  define BEGIN_DECL extern "C" {
25 # else
26 #  define BEGIN_DECL 
27 # endif
28 #endif
29
30 /*! C++ users need love */
31 #ifndef END_DECL
32 # ifdef __cplusplus
33 #  define END_DECL }
34 # else
35 #  define END_DECL 
36 # endif
37 #endif
38 /* End of cruft for C++ */
39
40 BEGIN_DECL
41
42 typedef enum {
43   no_error=0,     /* succes */
44   malloc_error,   /* Well known error */
45   mismatch_error, /* The provided ID does not match */
46   system_error,   /* a syscall did fail */
47   network_error,  /* error while sending/receiving data */
48   timeout_error,  /* not quick enough, dude */
49   thread_error,   /* error while [un]locking */
50   unknown_error 
51 } gras_error_t;
52
53 /*@observer@*/ const char *gras_error_name(gras_error_t errcode);
54
55 #define TRY(a) do {                                     \
56   if ((errcode=a) != no_error) {                        \
57      fprintf (stderr, "%s:%d: '%s' error raising...\n", \
58              __FILE__,__LINE__,                         \
59              gras_error_name(errcode));                 \
60      return errcode;                                    \
61   } } while (0)
62    
63 #define TRYCATCH(a,b) if ((errcode=a) != no_error && errcode !=b) return errcode
64 #define TRYFAIL(a) do {                                        \
65   if ((errcode=a) != no_error) {                               \
66      fprintf(stderr,"%s:%d: Got '%s' error !\n",               \
67              __FILE__,__LINE__,                                \
68              gras_error_name(errcode));                        \
69      fflush(stdout);                                           \
70      abort();                                                  \
71   } } while(0)
72
73 #define TRYEXPECT(action,expected_error)  do {                 \
74   errcode=action;                                              \
75   if (errcode != expected_error) {                             \
76     fprintf(stderr,"Got error %s (instead of %s expected)\n",  \
77             gras_error_name(errcode),                          \
78             gras_error_name(expected_error));                  \
79     abort();                                                   \
80   }                                                            \
81 } while(0)
82
83 /* FIXME TRYCLEAN should be avoided for readability */
84 #define TRYCLEAN(action,cleanup) do {                          \
85   if ((errcode=action) != no_error) {                          \
86     cleanup;                                                   \
87     return errcode;                                            \
88   }                                                            \
89 } while(0)
90
91 #if 0
92 #define _GRAS_ERR_PRE do {                                     \
93  void *_gs_array[30];                                          \
94   size_t _gs_size= backtrace (_gs_array, 30);                  \
95   char **_gs_strings= backtrace_symbols (_gs_array, _gs_size); \
96   size_t _gs_i;
97
98 #define _GRAS_ERR_POST(code)                                   \
99   fprintf(stderr,"Backtrace follows\n");                       \
100   for (_gs_i = 0; _gs_i < _gs_size; _gs_i++)                   \
101      fprintf (stderr,"   %s\n", _gs_strings[_gs_i]);           \
102   return code;                                                 \
103 } while (0)
104
105 #else
106 #define _GRAS_ERR_PRE do {
107 #define _GRAS_ERR_POST(code)                                   \
108   return code;                                                 \
109 } while (0)
110 #endif
111
112 #define RAISE0(code,fmt) _GRAS_ERR_PRE     \
113   fprintf(stderr,"%s:%d:%s: " fmt "\n",    \
114           __FILE__,__LINE__,__FUNCTION__); \
115   _GRAS_ERR_POST(code)
116 #define RAISE1(code,fmt,a1) _GRAS_ERR_PRE     \
117   fprintf(stderr,"%s:%d:%s: " fmt "\n",       \
118           __FILE__,__LINE__,__FUNCTION__,a1); \
119   _GRAS_ERR_POST(code)
120 #define RAISE2(code,fmt,a1,a2) _GRAS_ERR_PRE     \
121   fprintf(stderr,"%s:%d:%s: " fmt "\n",          \
122           __FILE__,__LINE__,__FUNCTION__,a1,a2); \
123   _GRAS_ERR_POST(code)
124 #define RAISE3(code,fmt,a1,a2,a3) _GRAS_ERR_PRE     \
125   fprintf(stderr,"%s:%d:%s: " fmt "\n",             \
126           __FILE__,__LINE__,__FUNCTION__,a1,a2,a3); \
127   _GRAS_ERR_POST(code)
128 #define RAISE4(code,fmt,a1,a2,a3,a4) _GRAS_ERR_PRE     \
129   fprintf(stderr,"%s:%d:%s: " fmt "\n",                \
130           __FILE__,__LINE__,__FUNCTION__,a1,a2,a3,a4); \
131   _GRAS_ERR_POST(code)
132 #define RAISE5(code,fmt,a1,a2,a3,a4,a5) _GRAS_ERR_PRE     \
133   fprintf(stderr,"%s:%d:%s: " fmt "\n",                   \
134           __FILE__,__LINE__,__FUNCTION__,a1,a2,a3,a4,a5); \
135   _GRAS_ERR_POST(code)
136 #define RAISE6(code,fmt,a1,a2,a3,a4,a5,a6) _GRAS_ERR_PRE     \
137   fprintf(stderr,"%s:%d:%s: " fmt "\n",                      \
138           __FILE__,__LINE__,__FUNCTION__,a1,a2,a3,a4,a5,a6); \
139   _GRAS_ERR_POST(code)
140
141 #define RAISE_MALLOC     RAISE0(malloc_error,"Malloc error")
142 #define RAISE_IMPOSSIBLE RAISE0(unknown_error,"The Impossible did happen")
143 #define RAISE_UNIMPLEMENTED RAISE1(unknown_error,"Function %s unimplemented",__FUNCTION__)
144
145 #define gras_abort abort
146
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
156 END_DECL
157
158 #endif /* GRAS_ERROR_H */
159