Logo AND Algorithmique Numérique Distribuée

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