OPIC
Object Persistence In C
op_assert.h
Go to the documentation of this file.
1 
8 /* This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or (at
11  * your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 /* Code: */
23 
24 #include <stdio.h>
25 #include <assert.h>
26 #include <execinfo.h>
27 #include <stdlib.h>
28 #include <stdarg.h>
29 #include "op_macros.h"
30 
31 #ifndef OPIC_COMMON_OP_ASSERT_H
32 #define OPIC_COMMON_OP_ASSERT_H 1
33 
34 #define op_stacktrace(stream) \
35  do { \
36  void* stack[OP_ASSERT_STACK_LIMIT]; \
37  size_t size; \
38  size = backtrace(stack, OP_ASSERT_STACK_LIMIT); \
39  backtrace_symbols_fd(stack,size,fileno(stream)); \
40  abort(); \
41  } while(0)
42 
43 #endif /* OP_ASSERT_H */
44 
45 /*
46  * Unlike other ANSI header files, <op_assert.h> may usefully be included
47  * multiple times, with and without NDEBUG defined.
48  * TODO: test the overhead of assert (though should have minimized by unlikely)
49  * TODO: Do we want to use log4c instead of stderr?
50  * TODO: integrate with cmocka to test assersions.
51  */
52 
53 #ifndef OP_ASSERT_STACK_LIMIT
54 #define OP_ASSERT_STACK_LIMIT 2048
55 #endif
56 
57 #ifndef NDEBUG
58 
77 #define op_assert(X, ...) \
78  do{ \
79  if (op_unlikely(!(X))) { \
80  fprintf(stderr,"Assertion failed: %s (%s:%d)\n", \
81  __func__, __FILE__, __LINE__); \
82  fprintf(stderr,"Error message: " __VA_ARGS__); \
83  op_stacktrace(stderr); \
84  } \
85  } while(0)
86 
87 
111 #define op_assert_diagnose(X,cb, ...) \
112  do { \
113  if (op_unlikely(!(X))) { \
114  fprintf(stderr,"Assertion failed: %s (%s:%d)\n", \
115  __func__, __FILE__, __LINE__); \
116  (cb)(__VA_ARGS__); \
117  op_stacktrace(stderr); \
118  } \
119  } while(0)
120 
121 #else /* NDEBUG = 1 */
122 #define op_assert(X, format,...) ((void)0)
123 #define op_assert_diagnose(X, cb, ...) ((void)0)
124 #endif /* NDEBUG */
125 
126 
127 /* op_assert.h ends here */