3 /* gras/error.h - Error tracking support */
5 /* Authors: Martin Quinson */
6 /* Copyright (C) 2003,2004 da GRAS posse. */
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. */
15 #include <stdio.h> /* FIXME: Get rid of it */
17 #include "xbt/misc.h" /* BEGIN_DECL */
20 #ifdef HAVE_EXECINFO_H
21 #include <execinfo.h> /* to print the backtrace */
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 */
35 /* remote errors: result of a RMI/RPC.
36 no_error(=0) is the same for both */
37 remote_mismatch_error=129,
45 /*@observer@*/ const char *gras_error_name(gras_error_t errcode);
48 if ((errcode=a) != no_error) { \
49 fprintf (stderr, "%s:%d: '%s' error raising...\n", \
51 gras_error_name(errcode)); \
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", \
60 gras_error_name(errcode)); \
65 #define TRYEXPECT(action,expected_error) do { \
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)); \
75 /* FIXME TRYCLEAN should be avoided for readability */
76 #define TRYCLEAN(action,cleanup) do { \
77 if ((errcode=action) != no_error) { \
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); \
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]); \
98 #define _GRAS_ERR_PRE do {
99 #define _GRAS_ERR_POST(code) \
104 #define RAISE0(code,fmt) _GRAS_ERR_PRE \
105 fprintf(stderr,"%s:%d:%s: " fmt "\n", \
106 __FILE__,__LINE__,__FUNCTION__); \
108 #define RAISE1(code,fmt,a1) _GRAS_ERR_PRE \
109 fprintf(stderr,"%s:%d:%s: " fmt "\n", \
110 __FILE__,__LINE__,__FUNCTION__,a1); \
112 #define RAISE2(code,fmt,a1,a2) _GRAS_ERR_PRE \
113 fprintf(stderr,"%s:%d:%s: " fmt "\n", \
114 __FILE__,__LINE__,__FUNCTION__,a1,a2); \
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); \
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); \
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); \
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); \
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__)
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)
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(); }
157 void gras_die(const char *msg);
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))
164 #endif /* GRAS_ERROR_H */