2 * SPDX-License-Identifier: MIT
4 * Copyright (c) 2018 EfficiOS Inc. and Linux Foundation
5 * Copyright (c) 2018-2020 Philippe Proulx <pproulx@efficios.com>
8 #ifndef BABELTRACE_ASSERT_COND_BASE_INTERNAL_H
9 #define BABELTRACE_ASSERT_COND_BASE_INTERNAL_H
12 * The macros in this header use macros defined in "lib/logging.h". We
13 * don't want this header to automatically include "lib/logging.h"
14 * because you need to manually define BT_LOG_TAG before including
15 * "lib/logging.h" and it is unexpected that you also need to define it
16 * before including this header.
18 * This is a reminder that in order to use "lib/assert-cond.h", you also
19 * need to use logging explicitly.
22 #ifndef BT_LIB_LOG_SUPPORTED
23 # error Include "lib/logging.h" before this header.
29 #include "common/common.h"
30 #include "common/macros.h"
33 * Prints the details of an unsatisfied precondition or postcondition
34 * without immediately aborting. You should use this within a function
35 * which checks preconditions or postconditions, but which is called
36 * from a BT_ASSERT_PRE() or BT_ASSERT_POST() context, so that the
37 * function can still return its result for
38 * BT_ASSERT_PRE()/BT_ASSERT_POST() to evaluate it.
42 * static inline bool check_complex_precond(...)
47 * BT_ASSERT_COND_MSG("Invalid object: ...", ...);
56 * BT_ASSERT_PRE(check_complex_precond(...),
57 * "Precondition is not satisfied: ...", ...);
59 #define BT_ASSERT_COND_MSG(_fmt, ...) \
61 bt_lib_log(__FILE__, __func__, \
62 __LINE__, BT_LOG_FATAL, BT_LOG_TAG, \
63 (_fmt), ##__VA_ARGS__); \
69 * 1. Generates a condition ID based on `cond_type`, `func`, and
72 * 2. Logs (FATAL level) the generated condition ID and function name
75 * 3. Logs (FATAL level) a message using `fmt` and the optional
76 * arguments (same usage as BT_LIB_LOGF()).
80 __attribute__((noreturn
))
81 void bt_lib_assert_cond_failed(const char *cond_type
, const char *func
,
82 const char *id_suffix
, const char *fmt
, ...);
85 * Internal to this file: asserts that the library condition `_cond` of
86 * type `_cond_type` (`pre` or `post`), related to function `_func`,
87 * and having the ID suffix `_id_suffix` is satisfied.
89 * If `_cond` is false, then this macro calls
90 * bt_lib_assert_cond_failed().
92 * To assert that an _internal_ precondition or postcondition is
93 * satisfied, use BT_ASSERT() or BT_ASSERT_DBG().
95 #define _BT_ASSERT_COND(_cond_type, _func, _id_suffix, _cond, _fmt, ...) \
98 bt_lib_assert_cond_failed(_cond_type, _func, \
99 _id_suffix, _fmt, ##__VA_ARGS__); \
104 * Asserts that the library precondition `_cond` is satisfied.
106 * See _BT_ASSERT_COND() for details about the `_func` and `_id_suffix`
109 #define BT_ASSERT_PRE_FROM_FUNC(_func, _id_suffix, _cond, _fmt, ...) \
110 _BT_ASSERT_COND("pre", _func, _id_suffix, (_cond), _fmt, ##__VA_ARGS__)
113 * Like BT_ASSERT_PRE_FROM_FUNC(), but uses `__func__` (current function
114 * name) as the `_func` parameter.
116 #define BT_ASSERT_PRE(_id_suffix, _cond, _fmt, ...) \
117 BT_ASSERT_PRE_FROM_FUNC(__func__, _id_suffix, (_cond), _fmt, ##__VA_ARGS__)
120 * Asserts that the library postcondition `_cond` is satisfied.
122 * See _BT_ASSERT_COND() for details about the `_func` and `_id_suffix`
125 #define BT_ASSERT_POST(_func, _id_suffix, _cond, _fmt, ...) \
126 _BT_ASSERT_COND("post", _func, _id_suffix, (_cond), _fmt, ##__VA_ARGS__);
129 /* Developer mode version of BT_ASSERT_PRE_FROM_FUNC(). */
130 # define BT_ASSERT_PRE_DEV_FROM_FUNC(_func, _id_suffix, _cond, _fmt, ...) \
131 BT_ASSERT_PRE_FROM_FUNC(_func, _id_suffix, (_cond), _fmt, ##__VA_ARGS__)
133 /* Developer mode version of BT_ASSERT_PRE(). */
134 # define BT_ASSERT_PRE_DEV(_id_suffix, _cond, _fmt, ...) \
135 BT_ASSERT_PRE(_id_suffix, (_cond), _fmt, ##__VA_ARGS__)
137 /* Developer mode version of BT_ASSERT_POST(). */
138 # define BT_ASSERT_POST_DEV(_func, _id_suffix, _cond, _fmt, ...) \
139 BT_ASSERT_POST(_func, _id_suffix, (_cond), _fmt, ##__VA_ARGS__)
141 /* Developer mode version of BT_ASSERT_COND_MSG(). */
142 # define BT_ASSERT_COND_DEV_MSG(_fmt, ...) \
143 BT_ASSERT_COND_MSG(_fmt, ##__VA_ARGS__)
146 * Marks a function as being only used within a BT_ASSERT_PRE_DEV() or
147 * BT_ASSERT_POST_DEV() context.
149 # define BT_ASSERT_COND_DEV_FUNC
151 # define BT_ASSERT_COND_DEV_MSG(_fmt, ...)
153 # define BT_ASSERT_PRE_DEV_FROM_FUNC(_func, _id_suffix, _cond, _fmt, ...) \
154 BT_USE_EXPR4(_func, _id_suffix, _cond, _fmt)
156 # define BT_ASSERT_PRE_DEV(_id_suffix, _cond, _fmt, ...) \
157 BT_USE_EXPR3(_id_suffix, _cond, _fmt)
159 # define BT_ASSERT_POST_DEV(_func, _id_suffix, _cond, _fmt, ...) \
160 BT_USE_EXPR4(_func, _id_suffix, _cond, _fmt)
162 # define BT_ASSERT_COND_DEV_FUNC __attribute__((unused))
163 #endif /* BT_DEV_MODE */
166 * Mark anything that includes this file as supporting precondition and
167 * postcondition assertion macros.
169 #define BT_ASSERT_COND_SUPPORTED
171 #endif /* BABELTRACE_ASSERT_COND_BASE_INTERNAL_H */
This page took 0.040797 seconds and 4 git commands to generate.