2 * SPDX-License-Identifier: MIT
4 * Copyright 2019 Philippe Proulx <pproulx@efficios.com>
7 #ifndef BABELTRACE_LOGGING_COMP_LOGGING_H
8 #define BABELTRACE_LOGGING_COMP_LOGGING_H
11 # error Please define a tag with BT_LOG_TAG before including this file.
15 #include <babeltrace2/babeltrace.h>
16 #include "logging/log.h"
18 #define _BT_COMP_LOG_COMP_PREFIX "[%s] "
19 #define _BT_COMP_LOG_COMP_NA_STR "N/A"
21 /* Logs with level `_lvl` for self component `_self_comp` */
22 #define BT_COMP_LOG(_lvl, _self_comp, _fmt, ...) \
23 BT_LOG_WRITE((_lvl), BT_LOG_TAG, _BT_COMP_LOG_COMP_PREFIX _fmt, \
25 bt_component_get_name( \
26 bt_self_component_as_component(_self_comp)) : \
27 _BT_COMP_LOG_COMP_NA_STR, \
30 /* Logs with level `_lvl` for self component class `_self_comp_class` */
31 #define BT_COMP_CLASS_LOG(_lvl, _self_comp_class, _fmt, ...) \
32 BT_LOG_WRITE((_lvl), BT_LOG_TAG, _BT_COMP_LOG_COMP_PREFIX _fmt, \
33 bt_component_class_get_name( \
34 bt_self_component_class_as_component_class( \
35 _self_comp_class)), ##__VA_ARGS__)
37 #define BT_COMP_LOG_CUR_LVL(_lvl, _cur_lvl, _self_comp, _fmt, ...) \
38 BT_LOG_WRITE_CUR_LVL((_lvl), (_cur_lvl), BT_LOG_TAG, \
39 _BT_COMP_LOG_COMP_PREFIX _fmt, \
41 bt_component_get_name( \
42 bt_self_component_as_component(_self_comp)) : \
43 _BT_COMP_LOG_COMP_NA_STR, \
46 #define BT_COMP_LOG_ERRNO(_lvl, _self_comp, _msg, _fmt, ...) \
47 BT_LOG_WRITE_ERRNO((_lvl), BT_LOG_TAG, _msg, \
48 _BT_COMP_LOG_COMP_PREFIX _fmt, \
50 bt_component_get_name( \
51 bt_self_component_as_component(_self_comp)) : \
52 _BT_COMP_LOG_COMP_NA_STR, \
55 #define BT_COMP_LOG_ERRNO_CUR_LVL(_lvl, _cur_lvl, _self_comp, _msg, _fmt, ...) \
56 BT_LOG_WRITE_ERRNO_CUR_LVL((_lvl), (_cur_lvl), BT_LOG_TAG, _msg, \
57 _BT_COMP_LOG_COMP_PREFIX _fmt, \
59 bt_component_get_name( \
60 bt_self_component_as_component(_self_comp)) : \
61 _BT_COMP_LOG_COMP_NA_STR, \
64 #define BT_COMP_LOG_MEM(_lvl, _self_comp, _data_ptr, _data_sz, _fmt, ...) \
65 BT_LOG_WRITE_MEM((_lvl), BT_LOG_TAG, (_data_ptr), (_data_sz), \
66 _BT_COMP_LOG_COMP_PREFIX _fmt, \
68 bt_component_get_name( \
69 bt_self_component_as_component(_self_comp)) : \
70 _BT_COMP_LOG_COMP_NA_STR, \
73 /* Specific per-level logging macros; they use `BT_COMP_LOG_SELF_COMP` */
74 #define BT_COMP_LOGF(_fmt, ...) \
75 BT_COMP_LOG(BT_LOG_FATAL, (BT_COMP_LOG_SELF_COMP), _fmt, ##__VA_ARGS__)
76 #define BT_COMP_LOGE(_fmt, ...) \
77 BT_COMP_LOG(BT_LOG_ERROR, (BT_COMP_LOG_SELF_COMP), _fmt, ##__VA_ARGS__)
78 #define BT_COMP_LOGW(_fmt, ...) \
79 BT_COMP_LOG(BT_LOG_WARNING, (BT_COMP_LOG_SELF_COMP), _fmt, ##__VA_ARGS__)
80 #define BT_COMP_LOGI(_fmt, ...) \
81 BT_COMP_LOG(BT_LOG_INFO, (BT_COMP_LOG_SELF_COMP), _fmt, ##__VA_ARGS__)
82 #define BT_COMP_LOGD(_fmt, ...) \
83 BT_COMP_LOG(BT_LOG_DEBUG, (BT_COMP_LOG_SELF_COMP), _fmt, ##__VA_ARGS__)
84 #define BT_COMP_LOGT(_fmt, ...) \
85 BT_COMP_LOG(BT_LOG_TRACE, (BT_COMP_LOG_SELF_COMP), _fmt, ##__VA_ARGS__)
86 #define BT_COMP_LOGF_STR(_str) \
87 BT_COMP_LOG(BT_LOG_FATAL, (BT_COMP_LOG_SELF_COMP), "%s", (_str))
88 #define BT_COMP_LOGE_STR(_str) \
89 BT_COMP_LOG(BT_LOG_ERROR, (BT_COMP_LOG_SELF_COMP), "%s", (_str))
90 #define BT_COMP_LOGW_STR(_str) \
91 BT_COMP_LOG(BT_LOG_WARNING, (BT_COMP_LOG_SELF_COMP), "%s", (_str))
92 #define BT_COMP_LOGI_STR(_str) \
93 BT_COMP_LOG(BT_LOG_INFO, (BT_COMP_LOG_SELF_COMP), "%s", (_str))
94 #define BT_COMP_LOGD_STR(_str) \
95 BT_COMP_LOG(BT_LOG_DEBUG, (BT_COMP_LOG_SELF_COMP), "%s", (_str))
96 #define BT_COMP_LOGT_STR(_str) \
97 BT_COMP_LOG(BT_LOG_TRACE, (BT_COMP_LOG_SELF_COMP), "%s", (_str))
98 #define BT_COMP_LOGF_ERRNO(_msg, _fmt, ...) \
99 BT_COMP_LOG_ERRNO(BT_LOG_FATAL, (BT_COMP_LOG_SELF_COMP), _msg, _fmt, ##__VA_ARGS__)
100 #define BT_COMP_LOGE_ERRNO(_msg, _fmt, ...) \
101 BT_COMP_LOG_ERRNO(BT_LOG_ERROR, (BT_COMP_LOG_SELF_COMP), _msg, _fmt, ##__VA_ARGS__)
102 #define BT_COMP_LOGW_ERRNO(_msg, _fmt, ...) \
103 BT_COMP_LOG_ERRNO(BT_LOG_WARNING, (BT_COMP_LOG_SELF_COMP), _msg, _fmt, ##__VA_ARGS__)
104 #define BT_COMP_LOGI_ERRNO(_msg, _fmt, ...) \
105 BT_COMP_LOG_ERRNO(BT_LOG_INFO, (BT_COMP_LOG_SELF_COMP), _msg, _fmt, ##__VA_ARGS__)
106 #define BT_COMP_LOGD_ERRNO(_msg, _fmt, ...) \
107 BT_COMP_LOG_ERRNO(BT_LOG_DEBUG, (BT_COMP_LOG_SELF_COMP), _msg, _fmt, ##__VA_ARGS__)
108 #define BT_COMP_LOGT_ERRNO(_msg, _fmt, ...) \
109 BT_COMP_LOG_ERRNO(BT_LOG_TRACE, (BT_COMP_LOG_SELF_COMP), _msg, _fmt, ##__VA_ARGS__)
110 #define BT_COMP_LOGF_MEM(_data_ptr, _data_sz, _fmt, ...) \
111 BT_COMP_LOG_MEM(BT_LOG_FATAL, (BT_COMP_LOG_SELF_COMP), (_data_ptr), (_data_sz), _fmt, ##__VA_ARGS__)
112 #define BT_COMP_LOGE_MEM(_data_ptr, _data_sz, _fmt, ...) \
113 BT_COMP_LOG_MEM(BT_LOG_ERROR, (BT_COMP_LOG_SELF_COMP), (_data_ptr), (_data_sz), _fmt, ##__VA_ARGS__)
114 #define BT_COMP_LOGW_MEM(_data_ptr, _data_sz, _fmt, ...) \
115 BT_COMP_LOG_MEM(BT_LOG_WARNING, (BT_COMP_LOG_SELF_COMP), (_data_ptr), (_data_sz), _fmt, ##__VA_ARGS__)
116 #define BT_COMP_LOGI_MEM(_data_ptr, _data_sz, _fmt, ...) \
117 BT_COMP_LOG_MEM(BT_LOG_INFO, (BT_COMP_LOG_SELF_COMP), (_data_ptr), (_data_sz), _fmt, ##__VA_ARGS__)
118 #define BT_COMP_LOGD_MEM(_data_ptr, _data_sz, _fmt, ...) \
119 BT_COMP_LOG_MEM(BT_LOG_DEBUG, (BT_COMP_LOG_SELF_COMP), (_data_ptr), (_data_sz), _fmt, ##__VA_ARGS__)
120 #define BT_COMP_LOGT_MEM(_data_ptr, _data_sz, _fmt, ...) \
121 BT_COMP_LOG_MEM(BT_LOG_TRACE, (BT_COMP_LOG_SELF_COMP), (_data_ptr), (_data_sz), _fmt, ##__VA_ARGS__)
123 #define BT_COMP_LOG_SUPPORTED
125 /* Logs and appends error cause from component context. */
126 #define BT_COMP_LOG_APPEND_CAUSE(_lvl, _self_comp, _fmt, ...) \
128 BT_COMP_LOG(_lvl, _self_comp, _fmt, ##__VA_ARGS__); \
129 (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT( \
130 _self_comp, _fmt, ##__VA_ARGS__); \
133 /* Logs error and appends error cause from component context. */
134 #define BT_COMP_LOGE_APPEND_CAUSE(_self_comp, _fmt, ...) \
135 BT_COMP_LOG_APPEND_CAUSE(BT_LOG_ERROR, _self_comp, _fmt, ##__VA_ARGS__)
138 * Logs and appends error cause from component context - the errno edition.
140 #define BT_COMP_LOG_APPEND_CAUSE_ERRNO(_lvl, _self_comp, _msg, _fmt, ...) \
142 const char *error_str = g_strerror(errno); \
143 BT_COMP_LOG(_lvl, _self_comp, _msg ": %s" _fmt, error_str, \
145 (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT( \
146 _self_comp, _msg ": %s" _fmt, error_str, ##__VA_ARGS__); \
150 * Logs error and appends error cause from component context - the errno
153 #define BT_COMP_LOGE_APPEND_CAUSE_ERRNO(_self_comp, _msg, _fmt, ...) \
154 BT_COMP_LOG_APPEND_CAUSE_ERRNO(BT_LOG_ERROR, _self_comp, _msg, _fmt, ##__VA_ARGS__)
156 /* Logs error from component class context. */
157 #define BT_COMP_CLASS_LOGE(_self_comp_class, _fmt, ...) \
158 BT_COMP_CLASS_LOG(BT_LOG_ERROR,_self_comp_class, _fmt, ##__VA_ARGS__)
160 /* Logs error and errno string from component class context. */
161 #define BT_COMP_CLASS_LOG_ERRNO(_lvl, _self_comp_class, _msg, _fmt, ...) \
162 BT_LOG_WRITE_ERRNO((_lvl), BT_LOG_TAG, _msg, \
163 _BT_COMP_LOG_COMP_PREFIX _fmt, \
164 bt_component_class_get_name( \
165 bt_self_component_class_as_component_class(_self_comp_class)) \
168 /* Logs and appends error cause from component class context. */
169 #define BT_COMP_CLASS_LOG_APPEND_CAUSE(_lvl, _self_comp_class, _fmt, ...) \
171 BT_COMP_CLASS_LOG(_lvl, _self_comp_class, _fmt, ##__VA_ARGS__); \
172 (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT_CLASS( \
173 _self_comp_class, _fmt, ##__VA_ARGS__); \
176 /* Logs error and appends error cause from component class context. */
177 #define BT_COMP_CLASS_LOGE_APPEND_CAUSE(_self_comp_class, _fmt, ...) \
178 BT_COMP_CLASS_LOG_APPEND_CAUSE(BT_LOG_ERROR, _self_comp_class, _fmt, ##__VA_ARGS__)
181 * Logs and appends error cause from component class context - the errno
184 #define BT_COMP_CLASS_LOG_APPEND_CAUSE_ERRNO(_lvl, _self_comp_class, _msg, _fmt, ...) \
186 const char *error_str = g_strerror(errno); \
187 BT_COMP_CLASS_LOG(_lvl, _self_comp_class, _msg ": %s" _fmt, error_str, \
189 (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT_CLASS( \
190 _self_comp_class, _msg ": %s" _fmt, error_str, ##__VA_ARGS__); \
194 * Logs error and appends error cause from component class context - the errno
197 #define BT_COMP_CLASS_LOGE_APPEND_CAUSE_ERRNO(_self_comp_class, _msg, _fmt, ...) \
198 BT_COMP_CLASS_LOG_APPEND_CAUSE_ERRNO(BT_LOG_ERROR, _self_comp_class, _msg, _fmt, \
202 * Logs error from component or component class context, depending on whichever
205 #define BT_COMP_OR_COMP_CLASS_LOG(_lvl, _self_comp, _self_comp_class, _fmt, ...) \
207 /* Only one of `_self_comp` and `_self_comp_class` must be set. */ \
208 BT_ASSERT((!!(_self_comp) != (!!_self_comp_class))); \
210 BT_COMP_LOG(_lvl, _self_comp, _fmt, ##__VA_ARGS__); \
212 BT_COMP_CLASS_LOG(_lvl, _self_comp_class, _fmt, ##__VA_ARGS__); \
216 #define BT_COMP_OR_COMP_CLASS_LOGE(_self_comp, _self_comp_class, _fmt, ...) \
217 BT_COMP_OR_COMP_CLASS_LOG(BT_LOG_ERROR,_self_comp, _self_comp_class, _fmt, ##__VA_ARGS__)
218 #define BT_COMP_OR_COMP_CLASS_LOGW(_self_comp, _self_comp_class, _fmt, ...) \
219 BT_COMP_OR_COMP_CLASS_LOG(BT_LOG_WARNING,_self_comp, _self_comp_class, _fmt, ##__VA_ARGS__)
220 #define BT_COMP_OR_COMP_CLASS_LOGI(_self_comp, _self_comp_class, _fmt, ...) \
221 BT_COMP_OR_COMP_CLASS_LOG(BT_LOG_INFO,_self_comp, _self_comp_class, _fmt, ##__VA_ARGS__)
222 #define BT_COMP_OR_COMP_CLASS_LOGD(_self_comp, _self_comp_class, _fmt, ...) \
223 BT_COMP_OR_COMP_CLASS_LOG(BT_LOG_DEBUG,_self_comp, _self_comp_class, _fmt, ##__VA_ARGS__)
226 * Logs error with errno string from component or component class context,
227 * depending on whichever is set.
229 #define BT_COMP_OR_COMP_CLASS_LOG_ERRNO(_lvl, _self_comp, _self_comp_class, _msg, _fmt, ...) \
231 /* Only one of `_self_comp` and `_self_comp_class` must be set. */ \
232 BT_ASSERT((!!(_self_comp) != (!!_self_comp_class))); \
234 BT_COMP_LOG_ERRNO(_lvl, _self_comp, _msg, _fmt, ##__VA_ARGS__); \
236 BT_COMP_CLASS_LOG_ERRNO(_lvl, _self_comp_class, _msg, _fmt, ##__VA_ARGS__); \
240 #define BT_COMP_OR_COMP_CLASS_LOGW_ERRNO(_self_comp, _self_comp_class, _msg, _fmt, ...) \
241 BT_COMP_OR_COMP_CLASS_LOG_ERRNO(BT_LOG_WARNING, _self_comp, _self_comp_class, _msg, _fmt, ##__VA_ARGS__)
244 * Logs error and appends error cause from component or component class context,
245 * depending on whichever is set.
247 #define BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(_self_comp, _self_comp_class, _fmt, ...) \
249 /* Only one of `_self_comp` and `_self_comp_class` must be set. */ \
250 BT_ASSERT((!!(_self_comp) != (!!_self_comp_class))); \
252 BT_COMP_LOGE_APPEND_CAUSE(_self_comp, _fmt, ##__VA_ARGS__); \
254 BT_COMP_CLASS_LOGE_APPEND_CAUSE(_self_comp_class, _fmt, ##__VA_ARGS__); \
259 * Logs error and appends error cause from message iterator context.
261 * There is no BT_SELF_MSG_LOGE yet, so use BT_COMP_LOGE for now.
263 #define BT_MSG_ITER_LOGE_APPEND_CAUSE(_self_msg_iter, _fmt, ...) \
265 BT_COMP_LOG(BT_LOG_ERROR, bt_self_message_iterator_borrow_component(_self_msg_iter), \
266 _fmt, ##__VA_ARGS__); \
267 (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR( \
268 _self_msg_iter, _fmt, ##__VA_ARGS__); \
271 #endif /* BABELTRACE_LOGGING_COMP_LOGGING_H */
This page took 0.056395 seconds and 4 git commands to generate.