3 /* gras/error.h - Error tracking support */
5 /* Authors: Martin Quinson */
6 /* Copyright (C) 2003 the OURAGAN project. */
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 <stddef.h> /* offsetof() */
16 #include <sys/types.h> /* size_t */
19 #include <stdio.h> /* FIXME: Get rid of it */
23 #ifdef HAVE_EXECINFO_H
24 #include <execinfo.h> /* to print the backtrace */
27 /* C++ users need love */
30 # define BEGIN_DECL extern "C" {
36 /*! C++ users need love */
44 /* End of cruft for C++ */
49 no_error=0, /* succes */
50 // malloc_error, /* Well known error */
51 mismatch_error, /* The provided ID does not match */
52 system_error, /* a syscall did fail */
53 network_error, /* error while sending/receiving data */
54 timeout_error, /* not quick enough, dude */
55 thread_error, /* error while [un]locking */
59 /*@observer@*/ const char *gras_error_name(gras_error_t errcode);
62 if ((errcode=a) != no_error) { \
63 fprintf (stderr, "%s:%d: '%s' error raising...\n", \
65 gras_error_name(errcode)); \
69 #define TRYCATCH(a,b) if ((errcode=a) != no_error && errcode !=b) return errcode
70 #define TRYFAIL(a) do { \
71 if ((errcode=a) != no_error) { \
72 fprintf(stderr,"%s:%d: Got '%s' error !\n", \
74 gras_error_name(errcode)); \
79 #define TRYEXPECT(action,expected_error) do { \
81 if (errcode != expected_error) { \
82 fprintf(stderr,"Got error %s (instead of %s expected)\n", \
83 gras_error_name(errcode), \
84 gras_error_name(expected_error)); \
89 /* FIXME TRYCLEAN should be avoided for readability */
90 #define TRYCLEAN(action,cleanup) do { \
91 if ((errcode=action) != no_error) { \
97 #if 0 /* FIXME: We don't use backtrace. Drop it? */
98 #define _GRAS_ERR_PRE do { \
99 void *_gs_array[30]; \
100 size_t _gs_size= backtrace (_gs_array, 30); \
101 char **_gs_strings= backtrace_symbols (_gs_array, _gs_size); \
104 #define _GRAS_ERR_POST(code) \
105 fprintf(stderr,"Backtrace follows\n"); \
106 for (_gs_i = 0; _gs_i < _gs_size; _gs_i++) \
107 fprintf (stderr," %s\n", _gs_strings[_gs_i]); \
112 #define _GRAS_ERR_PRE do {
113 #define _GRAS_ERR_POST(code) \
118 #define RAISE0(code,fmt) _GRAS_ERR_PRE \
119 fprintf(stderr,"%s:%d:%s: " fmt "\n", \
120 __FILE__,__LINE__,__FUNCTION__); \
122 #define RAISE1(code,fmt,a1) _GRAS_ERR_PRE \
123 fprintf(stderr,"%s:%d:%s: " fmt "\n", \
124 __FILE__,__LINE__,__FUNCTION__,a1); \
126 #define RAISE2(code,fmt,a1,a2) _GRAS_ERR_PRE \
127 fprintf(stderr,"%s:%d:%s: " fmt "\n", \
128 __FILE__,__LINE__,__FUNCTION__,a1,a2); \
130 #define RAISE3(code,fmt,a1,a2,a3) _GRAS_ERR_PRE \
131 fprintf(stderr,"%s:%d:%s: " fmt "\n", \
132 __FILE__,__LINE__,__FUNCTION__,a1,a2,a3); \
134 #define RAISE4(code,fmt,a1,a2,a3,a4) _GRAS_ERR_PRE \
135 fprintf(stderr,"%s:%d:%s: " fmt "\n", \
136 __FILE__,__LINE__,__FUNCTION__,a1,a2,a3,a4); \
138 #define RAISE5(code,fmt,a1,a2,a3,a4,a5) _GRAS_ERR_PRE \
139 fprintf(stderr,"%s:%d:%s: " fmt "\n", \
140 __FILE__,__LINE__,__FUNCTION__,a1,a2,a3,a4,a5); \
142 #define RAISE6(code,fmt,a1,a2,a3,a4,a5,a6) _GRAS_ERR_PRE \
143 fprintf(stderr,"%s:%d:%s: " fmt "\n", \
144 __FILE__,__LINE__,__FUNCTION__,a1,a2,a3,a4,a5,a6); \
147 //#define RAISE_MALLOC RAISE0(malloc_error,"Malloc error")
148 #define RAISE_IMPOSSIBLE RAISE0(unknown_error,"The Impossible did happen")
149 #define RAISE_UNIMPLEMENTED RAISE1(unknown_error,"Function %s unimplemented",__FUNCTION__)
152 #define gras_assert(cond)
153 #define gras_assert0(cond,msg)
154 #define gras_assert1(cond,msg,a)
155 #define gras_assert2(cond,msg,a,b)
156 #define gras_assert3(cond,msg,a,b,c)
157 #define gras_assert4(cond,msg,a,b,c,d)
158 #define gras_assert5(cond,msg,a,b,c,d,e)
159 #define gras_assert6(cond,msg,a,b,c,d,e,f)
161 #define gras_assert(cond) if (!(cond)) { CRITICAL1("Assertion %s failed", #cond); gras_abort(); }
162 #define gras_assert0(cond,msg) if (!(cond)) { CRITICAL0(msg); gras_abort(); }
163 #define gras_assert1(cond,msg,a) if (!(cond)) { CRITICAL1(msg,a); gras_abort(); }
164 #define gras_assert2(cond,msg,a,b) if (!(cond)) { CRITICAL2(msg,a,b); gras_abort(); }
165 #define gras_assert3(cond,msg,a,b,c) if (!(cond)) { CRITICAL3(msg,a,b,c); gras_abort(); }
166 #define gras_assert4(cond,msg,a,b,c,d) if (!(cond)) { CRITICAL4(msg,a,b,c,d); gras_abort(); }
167 #define gras_assert5(cond,msg,a,b,c,d,e) if (!(cond)) { CRITICAL5(msg,a,b,c,d,e); gras_abort(); }
168 #define gras_assert6(cond,msg,a,b,c,d,e,f) if (!(cond)) { CRITICAL6(msg,a,b,c,d,e,f); gras_abort(); }
171 #define DIE_IMPOSSIBLE gras_assert0(0,"The Impossible did happen (yet again)")
172 #define gras_assert_error(a) gras_assert1(errcode == (a), "Error %s unexpected",gras_error_name(errcode))
176 #endif /* GRAS_ERROR_H */