Commit | Line | Data |
---|---|---|
dd6aa119 | 1 | /* |
0235b0db | 2 | * SPDX-License-Identifier: MIT |
dd6aa119 | 3 | * |
0235b0db | 4 | * Copyright 2019 Philippe Proulx <pproulx@efficios.com> |
dd6aa119 PP |
5 | */ |
6 | ||
0235b0db MJ |
7 | #ifndef BABELTRACE_LOGGING_COMP_LOGGING_H |
8 | #define BABELTRACE_LOGGING_COMP_LOGGING_H | |
9 | ||
dd6aa119 PP |
10 | #ifndef BT_LOG_TAG |
11 | # error Please define a tag with BT_LOG_TAG before including this file. | |
12 | #endif | |
13 | ||
dd6aa119 | 14 | #include <stdlib.h> |
4fa90f32 | 15 | #include <babeltrace2/babeltrace.h> |
dd6aa119 PP |
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, ...) \ | |
71436ae4 SM |
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, \ | |
dd6aa119 PP |
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 | ||
d23b766e SM |
32 | /* Logs with level `_lvl` for self component class `_self_comp_class` */ |
33 | #define BT_COMP_CLASS_LOG(_lvl, _self_comp_class, _fmt, ...) \ | |
71436ae4 SM |
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, \ | |
d23b766e SM |
37 | bt_component_class_get_name( \ |
38 | bt_self_component_class_as_component_class( \ | |
39 | _self_comp_class)), ##__VA_ARGS__) | |
40 | ||
dd6aa119 | 41 | #define BT_COMP_LOG_CUR_LVL(_lvl, _cur_lvl, _self_comp, _fmt, ...) \ |
71436ae4 SM |
42 | BT_LOG_WRITE_PRINTF_CUR_LVL((enum bt_log_level) (_lvl), \ |
43 | (enum bt_log_level) (_cur_lvl), BT_LOG_TAG, \ | |
dd6aa119 PP |
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, ...) \ | |
71436ae4 SM |
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, \ | |
dd6aa119 PP |
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, ...) \ | |
71436ae4 SM |
63 | BT_LOG_WRITE_ERRNO_PRINTF_CUR_LVL((enum bt_log_level) (_lvl), \ |
64 | (enum bt_log_level) (_cur_lvl), BT_LOG_TAG, _msg, \ | |
dd6aa119 PP |
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, ...) \ | |
71436ae4 SM |
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), \ | |
dd6aa119 PP |
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, ...) \ | |
770538dd | 89 | BT_COMP_LOG(BT_LOG_WARNING, (BT_COMP_LOG_SELF_COMP), _fmt, ##__VA_ARGS__) |
dd6aa119 PP |
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__) | |
ef267d12 PP |
94 | #define BT_COMP_LOGT(_fmt, ...) \ |
95 | BT_COMP_LOG(BT_LOG_TRACE, (BT_COMP_LOG_SELF_COMP), _fmt, ##__VA_ARGS__) | |
dd6aa119 PP |
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) \ | |
770538dd | 101 | BT_COMP_LOG(BT_LOG_WARNING, (BT_COMP_LOG_SELF_COMP), "%s", (_str)) |
dd6aa119 PP |
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)) | |
ef267d12 PP |
106 | #define BT_COMP_LOGT_STR(_str) \ |
107 | BT_COMP_LOG(BT_LOG_TRACE, (BT_COMP_LOG_SELF_COMP), "%s", (_str)) | |
dd6aa119 PP |
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, ...) \ | |
770538dd | 113 | BT_COMP_LOG_ERRNO(BT_LOG_WARNING, (BT_COMP_LOG_SELF_COMP), _msg, _fmt, ##__VA_ARGS__) |
dd6aa119 PP |
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__) | |
ef267d12 PP |
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__) | |
dd6aa119 PP |
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, ...) \ | |
770538dd | 125 | BT_COMP_LOG_MEM(BT_LOG_WARNING, (BT_COMP_LOG_SELF_COMP), (_data_ptr), (_data_sz), _fmt, ##__VA_ARGS__) |
dd6aa119 PP |
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__) | |
ef267d12 PP |
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__) | |
dd6aa119 | 132 | |
7151fb67 PP |
133 | #define BT_COMP_LOG_SUPPORTED |
134 | ||
d23b766e | 135 | /* Logs and appends error cause from component context. */ |
8eb59971 | 136 | #define BT_COMP_LOG_APPEND_CAUSE(_lvl, _self_comp, _fmt, ...) \ |
d23b766e SM |
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, ...) \ | |
8eb59971 | 145 | BT_COMP_LOG_APPEND_CAUSE(BT_LOG_ERROR, _self_comp, _fmt, ##__VA_ARGS__) |
d23b766e | 146 | |
ab90ee94 SM |
147 | /* |
148 | * Logs and appends error cause from component context - the errno edition. | |
149 | */ | |
8eb59971 | 150 | #define BT_COMP_LOG_APPEND_CAUSE_ERRNO(_lvl, _self_comp, _msg, _fmt, ...) \ |
ab90ee94 SM |
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 | */ | |
233bf25a | 163 | #define BT_COMP_LOGE_APPEND_CAUSE_ERRNO(_self_comp, _msg, _fmt, ...) \ |
8eb59971 | 164 | BT_COMP_LOG_APPEND_CAUSE_ERRNO(BT_LOG_ERROR, _self_comp, _msg, _fmt, ##__VA_ARGS__) |
ab90ee94 | 165 | |
1419db2b FD |
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 | ||
8eb59971 FD |
170 | /* Logs error and errno string from component class context. */ |
171 | #define BT_COMP_CLASS_LOG_ERRNO(_lvl, _self_comp_class, _msg, _fmt, ...) \ | |
71436ae4 SM |
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, \ | |
8eb59971 FD |
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 | ||
d23b766e | 179 | /* Logs and appends error cause from component class context. */ |
8eb59971 | 180 | #define BT_COMP_CLASS_LOG_APPEND_CAUSE(_lvl, _self_comp_class, _fmt, ...) \ |
d23b766e SM |
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, ...) \ | |
8eb59971 | 189 | BT_COMP_CLASS_LOG_APPEND_CAUSE(BT_LOG_ERROR, _self_comp_class, _fmt, ##__VA_ARGS__) |
d23b766e SM |
190 | |
191 | /* | |
192 | * Logs and appends error cause from component class context - the errno | |
193 | * edition. | |
194 | */ | |
8eb59971 | 195 | #define BT_COMP_CLASS_LOG_APPEND_CAUSE_ERRNO(_lvl, _self_comp_class, _msg, _fmt, ...) \ |
d23b766e SM |
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 | */ | |
233bf25a | 208 | #define BT_COMP_CLASS_LOGE_APPEND_CAUSE_ERRNO(_self_comp_class, _msg, _fmt, ...) \ |
8eb59971 | 209 | BT_COMP_CLASS_LOG_APPEND_CAUSE_ERRNO(BT_LOG_ERROR, _self_comp_class, _msg, _fmt, \ |
d23b766e SM |
210 | ##__VA_ARGS__) |
211 | ||
1419db2b FD |
212 | /* |
213 | * Logs error from component or component class context, depending on whichever | |
214 | * is set. | |
215 | */ | |
8eb59971 FD |
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 | ||
1419db2b | 227 | #define BT_COMP_OR_COMP_CLASS_LOGE(_self_comp, _self_comp_class, _fmt, ...) \ |
8eb59971 | 228 | BT_COMP_OR_COMP_CLASS_LOG(BT_LOG_ERROR,_self_comp, _self_comp_class, _fmt, ##__VA_ARGS__) |
ecb4ba8a FD |
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__) | |
8eb59971 FD |
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, ...) \ | |
1419db2b FD |
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) { \ | |
8eb59971 | 245 | BT_COMP_LOG_ERRNO(_lvl, _self_comp, _msg, _fmt, ##__VA_ARGS__); \ |
1419db2b | 246 | } else { \ |
8eb59971 | 247 | BT_COMP_CLASS_LOG_ERRNO(_lvl, _self_comp_class, _msg, _fmt, ##__VA_ARGS__); \ |
1419db2b FD |
248 | } \ |
249 | } while (0) | |
250 | ||
8eb59971 FD |
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 | ||
d23b766e SM |
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 | ||
f6e68e70 SM |
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 | ||
d9c39b0a | 282 | #endif /* BABELTRACE_LOGGING_COMP_LOGGING_H */ |