summaryrefslogtreecommitdiff
path: root/lib/dbg.h
blob: ed5f2b9d93ee092b4f72d6089a234309c624d672 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#ifndef dbg_h
#define dbg_h
/* Maria project {{{
 *
 * Copyright (C) 2007 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    dbg.h
 * \brief   Debug functions.
 * \ingroup lib
 */

/** \todo take this from config. */
#define DEBUG 1

/**
 * Check that the provided expression is true.
 * \param  expr  the assumed truth
 *
 * \warning \a expr is not evaluated when asserts are disabled.
 */
#define dbg_assert(expr) \
    dbg_assert_ (expr, #expr)

/**
 * Check that the provided expression is true and provide more information on
 * failure.
 * \param  expr  the assumed truth
 * \param  fmt  printf-like format string
 * \param  rest  printf-like arguments
 *
 * \warning \a expr is not evaluated when asserts are disabled.
 */
#define dbg_assert_print(expr, fmt, rest...) \
    dbg_assert_print_ (expr, fmt, ## rest)

/**
 * Check that the provided expression is true and print additional errno
 * information on failure.
 * \param  expr  the assumed truth
 *
 * \warning \a expr is not evaluated when asserts are disabled.
 */
#define dbg_assert_perror(expr) \
    dbg_assert_perror_ (expr, #expr)

/**
 * Check that the provided expression is true, evaluate the expression even
 * when asserts are disabled.
 * \param  expr  the assumed truth
 *
 * \warning \a expr is always evaluated.
 */
#define dbg_check(expr) \
    dbg_check_ (expr, #expr)

/**
 * Check that the provided expression is true, evaluate the expression even
 * when asserts are disabled and provide more information on failure.
 * \param  expr  the assumed truth
 * \param  fmt  printf-like format string
 * \param  rest  printf-like arguments
 *
 * \warning \a expr is always evaluated.
 */
#define dbg_check_print(expr, fmt, rest...) \
    dbg_check_print_ (expr, fmt, ## rest)

/**
 * Check that the provided expression is true and print additional errno
 * information on failure, evaluate the expression even when asserts are
 * disabled.
 * \param  expr  the assumed truth
 *
 * \warning \a expr is always evaluated.
 */
#define dbg_check_perror(expr) \
    dbg_check_perror_ (expr, #expr)

/* Asserts definitions. */
#if DEBUG
#  define dbg_assert_ dbg_assert__
#  define dbg_assert_print_ dbg_assert_print__
#  define dbg_assert_perror_ dbg_assert_perror__
#  define dbg_check_ dbg_assert__
#  define dbg_check_print_ dbg_assert_print__
#  define dbg_check_perror_ dbg_assert_perror__
#  define dbg_assert__(expr, exprs) \
    ((void) ((expr) ? 0 : (dbg_assert_fail (exprs, __FILE__, __LINE__, \
                                            __PRETTY_FUNCTION__), 0)))
#  define dbg_assert_print__(expr, fmt, rest...) \
    ((void) ((expr) ? 0 : (dbg_assert_print_fail (fmt, __FILE__, __LINE__, \
                                                  __PRETTY_FUNCTION__, \
                                                  ##rest), 0)))
#  define dbg_assert_perror__(expr, exprs) \
    ((void) ((expr) ? 0 : (dbg_assert_perror_fail (exprs, __FILE__, __LINE__, \
                                                   __PRETTY_FUNCTION__), 0)))
#else
#  define dbg_assert_ dbg_void__
#  define dbg_assert_print_ dbg_void_print__
#  define dbg_assert_perror_ dbg_void__
#  define dbg_check_ dbg_eval__
#  define dbg_check_print_ dbg_eval_print__
#  define dbg_check_perror_ dbg_eval__
#  define dbg_void__(expr, exprs) ((void) 0)
#  define dbg_eval__(expr, exprs) ((void) (expr, 0))
#  define dbg_void_print__(expr, fmt, rest...) ((void) 0)
#  define dbg_eval_print__(expr, fmt, rest...) ((void) (expr, 0))
#endif

/**
 * Called on assertion failure.
 * \param  assertion  assertion text
 * \param  file  source file name
 * \param  line  source file line
 * \param  function  source function name
 */
void
dbg_assert_fail (const char *assertion, const char *file, uint line,
                 const char *function) __attribute__ ((__noreturn__));

/**
 * Called on assertion failure with more information on failure.
 * \param  file  source file name
 * \param  line  source file line
 * \param  function  source function name
 * \param  fmt  printf-like format string
 */
void
dbg_assert_print_fail (const char *fmt, const char *file, uint line,
                       const char *function, ...)
    __attribute__ ((__noreturn__, format (printf, 1, 5)));

/**
 * Called on errno assertion failure.
 * \param  assertion  assertion text
 * \param  file  source file name
 * \param  line  source file line
 * \param  function  source function name
 *
 * \todo implement
 */
void
dbg_assert_perror_fail (const char *assertion, const char *file, uint line,
                        const char *function) __attribute__ ((__noreturn__));

/**
 * Stop the program with a fatal error.
 * \param  fmt  printf-like format string
 *
 * \warning  interface may change in near future.
 */
void
dbg_fatal (const char *fmt, ...)
    __attribute__ ((__noreturn__, format (printf, 1, 2)));

#endif /* dbg_h */