cpp-common/bt2c/fmt.hpp: use `wise_enum::string_type` in `EnableIfIsWiseEnum` definition
[babeltrace.git] / src / logging / comp-logging.h
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright 2019 Philippe Proulx <pproulx@efficios.com>
5 */
6
7 #ifndef BABELTRACE_LOGGING_COMP_LOGGING_H
8 #define BABELTRACE_LOGGING_COMP_LOGGING_H
9
10 #ifndef BT_LOG_TAG
11 # error Please define a tag with BT_LOG_TAG before including this file.
12 #endif
13
14 #include <stdlib.h>
15 #include <babeltrace2/babeltrace.h>
16 #include "logging/log.h"
17
18 #define _BT_COMP_LOG_COMP_PREFIX "[%s] "
19 #define _BT_COMP_LOG_COMP_NA_STR "N/A"
20
21 /* Logs with level `_lvl` for self component `_self_comp` */
22 #define BT_COMP_LOG(_lvl, _self_comp, _fmt, ...) \
23 BT_LOG_WRITE_PRINTF_CUR_LVL((enum bt_log_level) (_lvl), \
24 (enum bt_log_level) (BT_LOG_OUTPUT_LEVEL), BT_LOG_TAG, \
25 _BT_COMP_LOG_COMP_PREFIX _fmt, \
26 (_self_comp) ? \
27 bt_component_get_name( \
28 bt_self_component_as_component(_self_comp)) : \
29 _BT_COMP_LOG_COMP_NA_STR, \
30 ##__VA_ARGS__)
31
32 /* Logs with level `_lvl` for self component class `_self_comp_class` */
33 #define BT_COMP_CLASS_LOG(_lvl, _self_comp_class, _fmt, ...) \
34 BT_LOG_WRITE_PRINTF_CUR_LVL((enum bt_log_level) (_lvl), \
35 (enum bt_log_level) (BT_LOG_OUTPUT_LEVEL), BT_LOG_TAG, \
36 _BT_COMP_LOG_COMP_PREFIX _fmt, \
37 bt_component_class_get_name( \
38 bt_self_component_class_as_component_class( \
39 _self_comp_class)), ##__VA_ARGS__)
40
41 #define BT_COMP_LOG_CUR_LVL(_lvl, _cur_lvl, _self_comp, _fmt, ...) \
42 BT_LOG_WRITE_PRINTF_CUR_LVL((enum bt_log_level) (_lvl), \
43 (enum bt_log_level) (_cur_lvl), BT_LOG_TAG, \
44 _BT_COMP_LOG_COMP_PREFIX _fmt, \
45 (_self_comp) ? \
46 bt_component_get_name( \
47 bt_self_component_as_component(_self_comp)) : \
48 _BT_COMP_LOG_COMP_NA_STR, \
49 ##__VA_ARGS__)
50
51 #define BT_COMP_LOG_ERRNO(_lvl, _self_comp, _msg, _fmt, ...) \
52 BT_LOG_WRITE_ERRNO_PRINTF_CUR_LVL((enum bt_log_level) (_lvl), \
53 (enum bt_log_level) (BT_LOG_OUTPUT_LEVEL), \
54 BT_LOG_TAG, _msg, \
55 _BT_COMP_LOG_COMP_PREFIX _fmt, \
56 (_self_comp) ? \
57 bt_component_get_name( \
58 bt_self_component_as_component(_self_comp)) : \
59 _BT_COMP_LOG_COMP_NA_STR, \
60 ##__VA_ARGS__)
61
62 #define BT_COMP_LOG_ERRNO_CUR_LVL(_lvl, _cur_lvl, _self_comp, _msg, _fmt, ...) \
63 BT_LOG_WRITE_ERRNO_PRINTF_CUR_LVL((enum bt_log_level) (_lvl), \
64 (enum bt_log_level) (_cur_lvl), BT_LOG_TAG, _msg, \
65 _BT_COMP_LOG_COMP_PREFIX _fmt, \
66 (_self_comp) ? \
67 bt_component_get_name( \
68 bt_self_component_as_component(_self_comp)) : \
69 _BT_COMP_LOG_COMP_NA_STR, \
70 ##__VA_ARGS__)
71
72 #define BT_COMP_LOG_MEM(_lvl, _self_comp, _data_ptr, _data_sz, _fmt, ...) \
73 BT_LOG_WRITE_MEM_PRINTF_CUR_LVL((enum bt_log_level) (_lvl), \
74 (enum bt_log_level) (BT_LOG_OUTPUT_LEVEL), BT_LOG_TAG, \
75 (_data_ptr), (_data_sz), \
76 _BT_COMP_LOG_COMP_PREFIX _fmt, \
77 (_self_comp) ? \
78 bt_component_get_name( \
79 bt_self_component_as_component(_self_comp)) : \
80 _BT_COMP_LOG_COMP_NA_STR, \
81 ##__VA_ARGS__)
82
83 /* Specific per-level logging macros; they use `BT_COMP_LOG_SELF_COMP` */
84 #define BT_COMP_LOGF(_fmt, ...) \
85 BT_COMP_LOG(BT_LOG_FATAL, (BT_COMP_LOG_SELF_COMP), _fmt, ##__VA_ARGS__)
86 #define BT_COMP_LOGE(_fmt, ...) \
87 BT_COMP_LOG(BT_LOG_ERROR, (BT_COMP_LOG_SELF_COMP), _fmt, ##__VA_ARGS__)
88 #define BT_COMP_LOGW(_fmt, ...) \
89 BT_COMP_LOG(BT_LOG_WARNING, (BT_COMP_LOG_SELF_COMP), _fmt, ##__VA_ARGS__)
90 #define BT_COMP_LOGI(_fmt, ...) \
91 BT_COMP_LOG(BT_LOG_INFO, (BT_COMP_LOG_SELF_COMP), _fmt, ##__VA_ARGS__)
92 #define BT_COMP_LOGD(_fmt, ...) \
93 BT_COMP_LOG(BT_LOG_DEBUG, (BT_COMP_LOG_SELF_COMP), _fmt, ##__VA_ARGS__)
94 #define BT_COMP_LOGT(_fmt, ...) \
95 BT_COMP_LOG(BT_LOG_TRACE, (BT_COMP_LOG_SELF_COMP), _fmt, ##__VA_ARGS__)
96 #define BT_COMP_LOGF_STR(_str) \
97 BT_COMP_LOG(BT_LOG_FATAL, (BT_COMP_LOG_SELF_COMP), "%s", (_str))
98 #define BT_COMP_LOGE_STR(_str) \
99 BT_COMP_LOG(BT_LOG_ERROR, (BT_COMP_LOG_SELF_COMP), "%s", (_str))
100 #define BT_COMP_LOGW_STR(_str) \
101 BT_COMP_LOG(BT_LOG_WARNING, (BT_COMP_LOG_SELF_COMP), "%s", (_str))
102 #define BT_COMP_LOGI_STR(_str) \
103 BT_COMP_LOG(BT_LOG_INFO, (BT_COMP_LOG_SELF_COMP), "%s", (_str))
104 #define BT_COMP_LOGD_STR(_str) \
105 BT_COMP_LOG(BT_LOG_DEBUG, (BT_COMP_LOG_SELF_COMP), "%s", (_str))
106 #define BT_COMP_LOGT_STR(_str) \
107 BT_COMP_LOG(BT_LOG_TRACE, (BT_COMP_LOG_SELF_COMP), "%s", (_str))
108 #define BT_COMP_LOGF_ERRNO(_msg, _fmt, ...) \
109 BT_COMP_LOG_ERRNO(BT_LOG_FATAL, (BT_COMP_LOG_SELF_COMP), _msg, _fmt, ##__VA_ARGS__)
110 #define BT_COMP_LOGE_ERRNO(_msg, _fmt, ...) \
111 BT_COMP_LOG_ERRNO(BT_LOG_ERROR, (BT_COMP_LOG_SELF_COMP), _msg, _fmt, ##__VA_ARGS__)
112 #define BT_COMP_LOGW_ERRNO(_msg, _fmt, ...) \
113 BT_COMP_LOG_ERRNO(BT_LOG_WARNING, (BT_COMP_LOG_SELF_COMP), _msg, _fmt, ##__VA_ARGS__)
114 #define BT_COMP_LOGI_ERRNO(_msg, _fmt, ...) \
115 BT_COMP_LOG_ERRNO(BT_LOG_INFO, (BT_COMP_LOG_SELF_COMP), _msg, _fmt, ##__VA_ARGS__)
116 #define BT_COMP_LOGD_ERRNO(_msg, _fmt, ...) \
117 BT_COMP_LOG_ERRNO(BT_LOG_DEBUG, (BT_COMP_LOG_SELF_COMP), _msg, _fmt, ##__VA_ARGS__)
118 #define BT_COMP_LOGT_ERRNO(_msg, _fmt, ...) \
119 BT_COMP_LOG_ERRNO(BT_LOG_TRACE, (BT_COMP_LOG_SELF_COMP), _msg, _fmt, ##__VA_ARGS__)
120 #define BT_COMP_LOGF_MEM(_data_ptr, _data_sz, _fmt, ...) \
121 BT_COMP_LOG_MEM(BT_LOG_FATAL, (BT_COMP_LOG_SELF_COMP), (_data_ptr), (_data_sz), _fmt, ##__VA_ARGS__)
122 #define BT_COMP_LOGE_MEM(_data_ptr, _data_sz, _fmt, ...) \
123 BT_COMP_LOG_MEM(BT_LOG_ERROR, (BT_COMP_LOG_SELF_COMP), (_data_ptr), (_data_sz), _fmt, ##__VA_ARGS__)
124 #define BT_COMP_LOGW_MEM(_data_ptr, _data_sz, _fmt, ...) \
125 BT_COMP_LOG_MEM(BT_LOG_WARNING, (BT_COMP_LOG_SELF_COMP), (_data_ptr), (_data_sz), _fmt, ##__VA_ARGS__)
126 #define BT_COMP_LOGI_MEM(_data_ptr, _data_sz, _fmt, ...) \
127 BT_COMP_LOG_MEM(BT_LOG_INFO, (BT_COMP_LOG_SELF_COMP), (_data_ptr), (_data_sz), _fmt, ##__VA_ARGS__)
128 #define BT_COMP_LOGD_MEM(_data_ptr, _data_sz, _fmt, ...) \
129 BT_COMP_LOG_MEM(BT_LOG_DEBUG, (BT_COMP_LOG_SELF_COMP), (_data_ptr), (_data_sz), _fmt, ##__VA_ARGS__)
130 #define BT_COMP_LOGT_MEM(_data_ptr, _data_sz, _fmt, ...) \
131 BT_COMP_LOG_MEM(BT_LOG_TRACE, (BT_COMP_LOG_SELF_COMP), (_data_ptr), (_data_sz), _fmt, ##__VA_ARGS__)
132
133 #define BT_COMP_LOG_SUPPORTED
134
135 /* Logs and appends error cause from component context. */
136 #define BT_COMP_LOG_APPEND_CAUSE(_lvl, _self_comp, _fmt, ...) \
137 do { \
138 BT_COMP_LOG(_lvl, _self_comp, _fmt, ##__VA_ARGS__); \
139 (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT( \
140 _self_comp, _fmt, ##__VA_ARGS__); \
141 } while (0)
142
143 /* Logs error and appends error cause from component context. */
144 #define BT_COMP_LOGE_APPEND_CAUSE(_self_comp, _fmt, ...) \
145 BT_COMP_LOG_APPEND_CAUSE(BT_LOG_ERROR, _self_comp, _fmt, ##__VA_ARGS__)
146
147 /*
148 * Logs and appends error cause from component context - the errno edition.
149 */
150 #define BT_COMP_LOG_APPEND_CAUSE_ERRNO(_lvl, _self_comp, _msg, _fmt, ...) \
151 do { \
152 const char *error_str = g_strerror(errno); \
153 BT_COMP_LOG(_lvl, _self_comp, _msg ": %s" _fmt, error_str, \
154 ##__VA_ARGS__); \
155 (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT( \
156 _self_comp, _msg ": %s" _fmt, error_str, ##__VA_ARGS__); \
157 } while (0)
158
159 /*
160 * Logs error and appends error cause from component context - the errno
161 * edition.
162 */
163 #define BT_COMP_LOGE_APPEND_CAUSE_ERRNO(_self_comp, _msg, _fmt, ...) \
164 BT_COMP_LOG_APPEND_CAUSE_ERRNO(BT_LOG_ERROR, _self_comp, _msg, _fmt, ##__VA_ARGS__)
165
166 /* Logs error from component class context. */
167 #define BT_COMP_CLASS_LOGE(_self_comp_class, _fmt, ...) \
168 BT_COMP_CLASS_LOG(BT_LOG_ERROR,_self_comp_class, _fmt, ##__VA_ARGS__)
169
170 /* Logs error and errno string from component class context. */
171 #define BT_COMP_CLASS_LOG_ERRNO(_lvl, _self_comp_class, _msg, _fmt, ...) \
172 BT_LOG_WRITE_ERRNO_PRINTF_CUR_LVL((enum bt_log_level) (_lvl), \
173 (enum bt_log_level) (BT_LOG_OUTPUT_LEVEL), BT_LOG_TAG, _msg, \
174 _BT_COMP_LOG_COMP_PREFIX _fmt, \
175 bt_component_class_get_name( \
176 bt_self_component_class_as_component_class(_self_comp_class)) \
177 ##__VA_ARGS__)
178
179 /* Logs and appends error cause from component class context. */
180 #define BT_COMP_CLASS_LOG_APPEND_CAUSE(_lvl, _self_comp_class, _fmt, ...) \
181 do { \
182 BT_COMP_CLASS_LOG(_lvl, _self_comp_class, _fmt, ##__VA_ARGS__); \
183 (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT_CLASS( \
184 _self_comp_class, _fmt, ##__VA_ARGS__); \
185 } while (0)
186
187 /* Logs error and appends error cause from component class context. */
188 #define BT_COMP_CLASS_LOGE_APPEND_CAUSE(_self_comp_class, _fmt, ...) \
189 BT_COMP_CLASS_LOG_APPEND_CAUSE(BT_LOG_ERROR, _self_comp_class, _fmt, ##__VA_ARGS__)
190
191 /*
192 * Logs and appends error cause from component class context - the errno
193 * edition.
194 */
195 #define BT_COMP_CLASS_LOG_APPEND_CAUSE_ERRNO(_lvl, _self_comp_class, _msg, _fmt, ...) \
196 do { \
197 const char *error_str = g_strerror(errno); \
198 BT_COMP_CLASS_LOG(_lvl, _self_comp_class, _msg ": %s" _fmt, error_str, \
199 ##__VA_ARGS__); \
200 (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT_CLASS( \
201 _self_comp_class, _msg ": %s" _fmt, error_str, ##__VA_ARGS__); \
202 } while (0)
203
204 /*
205 * Logs error and appends error cause from component class context - the errno
206 * edition.
207 */
208 #define BT_COMP_CLASS_LOGE_APPEND_CAUSE_ERRNO(_self_comp_class, _msg, _fmt, ...) \
209 BT_COMP_CLASS_LOG_APPEND_CAUSE_ERRNO(BT_LOG_ERROR, _self_comp_class, _msg, _fmt, \
210 ##__VA_ARGS__)
211
212 /*
213 * Logs error from component or component class context, depending on whichever
214 * is set.
215 */
216 #define BT_COMP_OR_COMP_CLASS_LOG(_lvl, _self_comp, _self_comp_class, _fmt, ...) \
217 do { \
218 /* Only one of `_self_comp` and `_self_comp_class` must be set. */ \
219 BT_ASSERT((!!(_self_comp) != (!!_self_comp_class))); \
220 if (_self_comp) { \
221 BT_COMP_LOG(_lvl, _self_comp, _fmt, ##__VA_ARGS__); \
222 } else { \
223 BT_COMP_CLASS_LOG(_lvl, _self_comp_class, _fmt, ##__VA_ARGS__); \
224 } \
225 } while (0)
226
227 #define BT_COMP_OR_COMP_CLASS_LOGE(_self_comp, _self_comp_class, _fmt, ...) \
228 BT_COMP_OR_COMP_CLASS_LOG(BT_LOG_ERROR,_self_comp, _self_comp_class, _fmt, ##__VA_ARGS__)
229 #define BT_COMP_OR_COMP_CLASS_LOGW(_self_comp, _self_comp_class, _fmt, ...) \
230 BT_COMP_OR_COMP_CLASS_LOG(BT_LOG_WARNING,_self_comp, _self_comp_class, _fmt, ##__VA_ARGS__)
231 #define BT_COMP_OR_COMP_CLASS_LOGI(_self_comp, _self_comp_class, _fmt, ...) \
232 BT_COMP_OR_COMP_CLASS_LOG(BT_LOG_INFO,_self_comp, _self_comp_class, _fmt, ##__VA_ARGS__)
233 #define BT_COMP_OR_COMP_CLASS_LOGD(_self_comp, _self_comp_class, _fmt, ...) \
234 BT_COMP_OR_COMP_CLASS_LOG(BT_LOG_DEBUG,_self_comp, _self_comp_class, _fmt, ##__VA_ARGS__)
235
236 /*
237 * Logs error with errno string from component or component class context,
238 * depending on whichever is set.
239 */
240 #define BT_COMP_OR_COMP_CLASS_LOG_ERRNO(_lvl, _self_comp, _self_comp_class, _msg, _fmt, ...) \
241 do { \
242 /* Only one of `_self_comp` and `_self_comp_class` must be set. */ \
243 BT_ASSERT((!!(_self_comp) != (!!_self_comp_class))); \
244 if (_self_comp) { \
245 BT_COMP_LOG_ERRNO(_lvl, _self_comp, _msg, _fmt, ##__VA_ARGS__); \
246 } else { \
247 BT_COMP_CLASS_LOG_ERRNO(_lvl, _self_comp_class, _msg, _fmt, ##__VA_ARGS__); \
248 } \
249 } while (0)
250
251 #define BT_COMP_OR_COMP_CLASS_LOGW_ERRNO(_self_comp, _self_comp_class, _msg, _fmt, ...) \
252 BT_COMP_OR_COMP_CLASS_LOG_ERRNO(BT_LOG_WARNING, _self_comp, _self_comp_class, _msg, _fmt, ##__VA_ARGS__)
253
254 /*
255 * Logs error and appends error cause from component or component class context,
256 * depending on whichever is set.
257 */
258 #define BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(_self_comp, _self_comp_class, _fmt, ...) \
259 do { \
260 /* Only one of `_self_comp` and `_self_comp_class` must be set. */ \
261 BT_ASSERT((!!(_self_comp) != (!!_self_comp_class))); \
262 if (_self_comp) { \
263 BT_COMP_LOGE_APPEND_CAUSE(_self_comp, _fmt, ##__VA_ARGS__); \
264 } else { \
265 BT_COMP_CLASS_LOGE_APPEND_CAUSE(_self_comp_class, _fmt, ##__VA_ARGS__); \
266 } \
267 } while (0)
268
269 /*
270 * Logs error and appends error cause from message iterator context.
271 *
272 * There is no BT_SELF_MSG_LOGE yet, so use BT_COMP_LOGE for now.
273 */
274 #define BT_MSG_ITER_LOGE_APPEND_CAUSE(_self_msg_iter, _fmt, ...) \
275 do { \
276 BT_COMP_LOG(BT_LOG_ERROR, bt_self_message_iterator_borrow_component(_self_msg_iter), \
277 _fmt, ##__VA_ARGS__); \
278 (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR( \
279 _self_msg_iter, _fmt, ##__VA_ARGS__); \
280 } while (0)
281
282 #endif /* BABELTRACE_LOGGING_COMP_LOGGING_H */
This page took 0.034508 seconds and 4 git commands to generate.