Cleanup: comp-logging.h: template `BT_COMP_OR_COMP_CLASS_LOG*` macros
[babeltrace.git] / src / logging / comp-logging.h
CommitLineData
d9c39b0a
PP
1#ifndef BABELTRACE_LOGGING_COMP_LOGGING_H
2#define BABELTRACE_LOGGING_COMP_LOGGING_H
dd6aa119
PP
3
4/*
5 * Copyright 2019 Philippe Proulx <pproulx@efficios.com>
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 */
25
26#ifndef BT_LOG_TAG
27# error Please define a tag with BT_LOG_TAG before including this file.
28#endif
29
dd6aa119 30#include <stdlib.h>
4fa90f32 31#include <babeltrace2/babeltrace.h>
dd6aa119
PP
32#include "logging/log.h"
33
34#define _BT_COMP_LOG_COMP_PREFIX "[%s] "
35#define _BT_COMP_LOG_COMP_NA_STR "N/A"
36
37/* Logs with level `_lvl` for self component `_self_comp` */
38#define BT_COMP_LOG(_lvl, _self_comp, _fmt, ...) \
39 BT_LOG_WRITE((_lvl), BT_LOG_TAG, _BT_COMP_LOG_COMP_PREFIX _fmt, \
40 (_self_comp) ? \
41 bt_component_get_name( \
42 bt_self_component_as_component(_self_comp)) : \
43 _BT_COMP_LOG_COMP_NA_STR, \
44 ##__VA_ARGS__)
45
d23b766e
SM
46/* Logs with level `_lvl` for self component class `_self_comp_class` */
47#define BT_COMP_CLASS_LOG(_lvl, _self_comp_class, _fmt, ...) \
48 BT_LOG_WRITE((_lvl), BT_LOG_TAG, _BT_COMP_LOG_COMP_PREFIX _fmt, \
49 bt_component_class_get_name( \
50 bt_self_component_class_as_component_class( \
51 _self_comp_class)), ##__VA_ARGS__)
52
dd6aa119
PP
53#define BT_COMP_LOG_CUR_LVL(_lvl, _cur_lvl, _self_comp, _fmt, ...) \
54 BT_LOG_WRITE_CUR_LVL((_lvl), (_cur_lvl), BT_LOG_TAG, \
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(_lvl, _self_comp, _msg, _fmt, ...) \
63 BT_LOG_WRITE_ERRNO((_lvl), BT_LOG_TAG, _msg, \
64 _BT_COMP_LOG_COMP_PREFIX _fmt, \
65 (_self_comp) ? \
66 bt_component_get_name( \
67 bt_self_component_as_component(_self_comp)) : \
68 _BT_COMP_LOG_COMP_NA_STR, \
69 ##__VA_ARGS__)
70
71#define BT_COMP_LOG_ERRNO_CUR_LVL(_lvl, _cur_lvl, _self_comp, _msg, _fmt, ...) \
72 BT_LOG_WRITE_ERRNO_CUR_LVL((_lvl), (_cur_lvl), BT_LOG_TAG, _msg, \
73 _BT_COMP_LOG_COMP_PREFIX _fmt, \
74 (_self_comp) ? \
75 bt_component_get_name( \
76 bt_self_component_as_component(_self_comp)) : \
77 _BT_COMP_LOG_COMP_NA_STR, \
78 ##__VA_ARGS__)
79
80#define BT_COMP_LOG_MEM(_lvl, _self_comp, _data_ptr, _data_sz, _fmt, ...) \
81 BT_LOG_WRITE_MEM((_lvl), BT_LOG_TAG, (_data_ptr), (_data_sz), \
82 _BT_COMP_LOG_COMP_PREFIX _fmt, \
83 (_self_comp) ? \
84 bt_component_get_name( \
85 bt_self_component_as_component(_self_comp)) : \
86 _BT_COMP_LOG_COMP_NA_STR, \
87 ##__VA_ARGS__)
88
89/* Specific per-level logging macros; they use `BT_COMP_LOG_SELF_COMP` */
90#define BT_COMP_LOGF(_fmt, ...) \
91 BT_COMP_LOG(BT_LOG_FATAL, (BT_COMP_LOG_SELF_COMP), _fmt, ##__VA_ARGS__)
92#define BT_COMP_LOGE(_fmt, ...) \
93 BT_COMP_LOG(BT_LOG_ERROR, (BT_COMP_LOG_SELF_COMP), _fmt, ##__VA_ARGS__)
94#define BT_COMP_LOGW(_fmt, ...) \
770538dd 95 BT_COMP_LOG(BT_LOG_WARNING, (BT_COMP_LOG_SELF_COMP), _fmt, ##__VA_ARGS__)
dd6aa119
PP
96#define BT_COMP_LOGI(_fmt, ...) \
97 BT_COMP_LOG(BT_LOG_INFO, (BT_COMP_LOG_SELF_COMP), _fmt, ##__VA_ARGS__)
98#define BT_COMP_LOGD(_fmt, ...) \
99 BT_COMP_LOG(BT_LOG_DEBUG, (BT_COMP_LOG_SELF_COMP), _fmt, ##__VA_ARGS__)
ef267d12
PP
100#define BT_COMP_LOGT(_fmt, ...) \
101 BT_COMP_LOG(BT_LOG_TRACE, (BT_COMP_LOG_SELF_COMP), _fmt, ##__VA_ARGS__)
dd6aa119
PP
102#define BT_COMP_LOGF_STR(_str) \
103 BT_COMP_LOG(BT_LOG_FATAL, (BT_COMP_LOG_SELF_COMP), "%s", (_str))
104#define BT_COMP_LOGE_STR(_str) \
105 BT_COMP_LOG(BT_LOG_ERROR, (BT_COMP_LOG_SELF_COMP), "%s", (_str))
106#define BT_COMP_LOGW_STR(_str) \
770538dd 107 BT_COMP_LOG(BT_LOG_WARNING, (BT_COMP_LOG_SELF_COMP), "%s", (_str))
dd6aa119
PP
108#define BT_COMP_LOGI_STR(_str) \
109 BT_COMP_LOG(BT_LOG_INFO, (BT_COMP_LOG_SELF_COMP), "%s", (_str))
110#define BT_COMP_LOGD_STR(_str) \
111 BT_COMP_LOG(BT_LOG_DEBUG, (BT_COMP_LOG_SELF_COMP), "%s", (_str))
ef267d12
PP
112#define BT_COMP_LOGT_STR(_str) \
113 BT_COMP_LOG(BT_LOG_TRACE, (BT_COMP_LOG_SELF_COMP), "%s", (_str))
dd6aa119
PP
114#define BT_COMP_LOGF_ERRNO(_msg, _fmt, ...) \
115 BT_COMP_LOG_ERRNO(BT_LOG_FATAL, (BT_COMP_LOG_SELF_COMP), _msg, _fmt, ##__VA_ARGS__)
116#define BT_COMP_LOGE_ERRNO(_msg, _fmt, ...) \
117 BT_COMP_LOG_ERRNO(BT_LOG_ERROR, (BT_COMP_LOG_SELF_COMP), _msg, _fmt, ##__VA_ARGS__)
118#define BT_COMP_LOGW_ERRNO(_msg, _fmt, ...) \
770538dd 119 BT_COMP_LOG_ERRNO(BT_LOG_WARNING, (BT_COMP_LOG_SELF_COMP), _msg, _fmt, ##__VA_ARGS__)
dd6aa119
PP
120#define BT_COMP_LOGI_ERRNO(_msg, _fmt, ...) \
121 BT_COMP_LOG_ERRNO(BT_LOG_INFO, (BT_COMP_LOG_SELF_COMP), _msg, _fmt, ##__VA_ARGS__)
122#define BT_COMP_LOGD_ERRNO(_msg, _fmt, ...) \
123 BT_COMP_LOG_ERRNO(BT_LOG_DEBUG, (BT_COMP_LOG_SELF_COMP), _msg, _fmt, ##__VA_ARGS__)
ef267d12
PP
124#define BT_COMP_LOGT_ERRNO(_msg, _fmt, ...) \
125 BT_COMP_LOG_ERRNO(BT_LOG_TRACE, (BT_COMP_LOG_SELF_COMP), _msg, _fmt, ##__VA_ARGS__)
dd6aa119
PP
126#define BT_COMP_LOGF_MEM(_data_ptr, _data_sz, _fmt, ...) \
127 BT_COMP_LOG_MEM(BT_LOG_FATAL, (BT_COMP_LOG_SELF_COMP), (_data_ptr), (_data_sz), _fmt, ##__VA_ARGS__)
128#define BT_COMP_LOGE_MEM(_data_ptr, _data_sz, _fmt, ...) \
129 BT_COMP_LOG_MEM(BT_LOG_ERROR, (BT_COMP_LOG_SELF_COMP), (_data_ptr), (_data_sz), _fmt, ##__VA_ARGS__)
130#define BT_COMP_LOGW_MEM(_data_ptr, _data_sz, _fmt, ...) \
770538dd 131 BT_COMP_LOG_MEM(BT_LOG_WARNING, (BT_COMP_LOG_SELF_COMP), (_data_ptr), (_data_sz), _fmt, ##__VA_ARGS__)
dd6aa119
PP
132#define BT_COMP_LOGI_MEM(_data_ptr, _data_sz, _fmt, ...) \
133 BT_COMP_LOG_MEM(BT_LOG_INFO, (BT_COMP_LOG_SELF_COMP), (_data_ptr), (_data_sz), _fmt, ##__VA_ARGS__)
134#define BT_COMP_LOGD_MEM(_data_ptr, _data_sz, _fmt, ...) \
135 BT_COMP_LOG_MEM(BT_LOG_DEBUG, (BT_COMP_LOG_SELF_COMP), (_data_ptr), (_data_sz), _fmt, ##__VA_ARGS__)
ef267d12
PP
136#define BT_COMP_LOGT_MEM(_data_ptr, _data_sz, _fmt, ...) \
137 BT_COMP_LOG_MEM(BT_LOG_TRACE, (BT_COMP_LOG_SELF_COMP), (_data_ptr), (_data_sz), _fmt, ##__VA_ARGS__)
dd6aa119 138
7151fb67
PP
139#define BT_COMP_LOG_SUPPORTED
140
d23b766e 141/* Logs and appends error cause from component context. */
8eb59971 142#define BT_COMP_LOG_APPEND_CAUSE(_lvl, _self_comp, _fmt, ...) \
d23b766e
SM
143 do { \
144 BT_COMP_LOG(_lvl, _self_comp, _fmt, ##__VA_ARGS__); \
145 (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT( \
146 _self_comp, _fmt, ##__VA_ARGS__); \
147 } while (0)
148
149/* Logs error and appends error cause from component context. */
150#define BT_COMP_LOGE_APPEND_CAUSE(_self_comp, _fmt, ...) \
8eb59971 151 BT_COMP_LOG_APPEND_CAUSE(BT_LOG_ERROR, _self_comp, _fmt, ##__VA_ARGS__)
d23b766e 152
ab90ee94
SM
153/*
154 * Logs and appends error cause from component context - the errno edition.
155 */
8eb59971 156#define BT_COMP_LOG_APPEND_CAUSE_ERRNO(_lvl, _self_comp, _msg, _fmt, ...) \
ab90ee94
SM
157 do { \
158 const char *error_str = g_strerror(errno); \
159 BT_COMP_LOG(_lvl, _self_comp, _msg ": %s" _fmt, error_str, \
160 ##__VA_ARGS__); \
161 (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT( \
162 _self_comp, _msg ": %s" _fmt, error_str, ##__VA_ARGS__); \
163 } while (0)
164
165/*
166 * Logs error and appends error cause from component context - the errno
167 * edition.
168 */
233bf25a 169#define BT_COMP_LOGE_APPEND_CAUSE_ERRNO(_self_comp, _msg, _fmt, ...) \
8eb59971 170 BT_COMP_LOG_APPEND_CAUSE_ERRNO(BT_LOG_ERROR, _self_comp, _msg, _fmt, ##__VA_ARGS__)
ab90ee94 171
1419db2b
FD
172/* Logs error from component class context. */
173#define BT_COMP_CLASS_LOGE(_self_comp_class, _fmt, ...) \
174 BT_COMP_CLASS_LOG(BT_LOG_ERROR,_self_comp_class, _fmt, ##__VA_ARGS__)
175
8eb59971
FD
176/* Logs error and errno string from component class context. */
177#define BT_COMP_CLASS_LOG_ERRNO(_lvl, _self_comp_class, _msg, _fmt, ...) \
178 BT_LOG_WRITE_ERRNO((_lvl), BT_LOG_TAG, _msg, \
179 _BT_COMP_LOG_COMP_PREFIX _fmt, \
180 bt_component_class_get_name( \
181 bt_self_component_class_as_component_class(_self_comp_class)) \
182 ##__VA_ARGS__)
183
d23b766e 184/* Logs and appends error cause from component class context. */
8eb59971 185#define BT_COMP_CLASS_LOG_APPEND_CAUSE(_lvl, _self_comp_class, _fmt, ...) \
d23b766e
SM
186 do { \
187 BT_COMP_CLASS_LOG(_lvl, _self_comp_class, _fmt, ##__VA_ARGS__); \
188 (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT_CLASS( \
189 _self_comp_class, _fmt, ##__VA_ARGS__); \
190 } while (0)
191
192/* Logs error and appends error cause from component class context. */
193#define BT_COMP_CLASS_LOGE_APPEND_CAUSE(_self_comp_class, _fmt, ...) \
8eb59971 194 BT_COMP_CLASS_LOG_APPEND_CAUSE(BT_LOG_ERROR, _self_comp_class, _fmt, ##__VA_ARGS__)
d23b766e
SM
195
196/*
197 * Logs and appends error cause from component class context - the errno
198 * edition.
199 */
8eb59971 200#define BT_COMP_CLASS_LOG_APPEND_CAUSE_ERRNO(_lvl, _self_comp_class, _msg, _fmt, ...) \
d23b766e
SM
201 do { \
202 const char *error_str = g_strerror(errno); \
203 BT_COMP_CLASS_LOG(_lvl, _self_comp_class, _msg ": %s" _fmt, error_str, \
204 ##__VA_ARGS__); \
205 (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT_CLASS( \
206 _self_comp_class, _msg ": %s" _fmt, error_str, ##__VA_ARGS__); \
207 } while (0)
208
209/*
210 * Logs error and appends error cause from component class context - the errno
211 * edition.
212 */
233bf25a 213#define BT_COMP_CLASS_LOGE_APPEND_CAUSE_ERRNO(_self_comp_class, _msg, _fmt, ...) \
8eb59971 214 BT_COMP_CLASS_LOG_APPEND_CAUSE_ERRNO(BT_LOG_ERROR, _self_comp_class, _msg, _fmt, \
d23b766e
SM
215 ##__VA_ARGS__)
216
1419db2b
FD
217/*
218 * Logs error from component or component class context, depending on whichever
219 * is set.
220 */
8eb59971
FD
221#define BT_COMP_OR_COMP_CLASS_LOG(_lvl, _self_comp, _self_comp_class, _fmt, ...) \
222 do { \
223 /* Only one of `_self_comp` and `_self_comp_class` must be set. */ \
224 BT_ASSERT((!!(_self_comp) != (!!_self_comp_class))); \
225 if (_self_comp) { \
226 BT_COMP_LOG(_lvl, _self_comp, _fmt, ##__VA_ARGS__); \
227 } else { \
228 BT_COMP_CLASS_LOG(_lvl, _self_comp_class, _fmt, ##__VA_ARGS__); \
229 } \
230 } while (0)
231
1419db2b 232#define BT_COMP_OR_COMP_CLASS_LOGE(_self_comp, _self_comp_class, _fmt, ...) \
8eb59971
FD
233 BT_COMP_OR_COMP_CLASS_LOG(BT_LOG_ERROR,_self_comp, _self_comp_class, _fmt, ##__VA_ARGS__)
234
235/*
236 * Logs error with errno string from component or component class context,
237 * depending on whichever is set.
238 */
239#define BT_COMP_OR_COMP_CLASS_LOG_ERRNO(_lvl, _self_comp, _self_comp_class, _msg, _fmt, ...) \
1419db2b
FD
240 do { \
241 /* Only one of `_self_comp` and `_self_comp_class` must be set. */ \
242 BT_ASSERT((!!(_self_comp) != (!!_self_comp_class))); \
243 if (_self_comp) { \
8eb59971 244 BT_COMP_LOG_ERRNO(_lvl, _self_comp, _msg, _fmt, ##__VA_ARGS__); \
1419db2b 245 } else { \
8eb59971 246 BT_COMP_CLASS_LOG_ERRNO(_lvl, _self_comp_class, _msg, _fmt, ##__VA_ARGS__); \
1419db2b
FD
247 } \
248 } while (0)
249
8eb59971
FD
250#define BT_COMP_OR_COMP_CLASS_LOGW_ERRNO(_self_comp, _self_comp_class, _msg, _fmt, ...) \
251 BT_COMP_OR_COMP_CLASS_LOG_ERRNO(BT_LOG_WARNING, _self_comp, _self_comp_class, _msg, _fmt, ##__VA_ARGS__)
252
d23b766e
SM
253/*
254 * Logs error and appends error cause from component or component class context,
255 * depending on whichever is set.
256 */
257#define BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(_self_comp, _self_comp_class, _fmt, ...) \
258 do { \
259 /* Only one of `_self_comp` and `_self_comp_class` must be set. */ \
260 BT_ASSERT((!!(_self_comp) != (!!_self_comp_class))); \
261 if (_self_comp) { \
262 BT_COMP_LOGE_APPEND_CAUSE(_self_comp, _fmt, ##__VA_ARGS__); \
263 } else { \
264 BT_COMP_CLASS_LOGE_APPEND_CAUSE(_self_comp_class, _fmt, ##__VA_ARGS__); \
265 } \
266 } while (0)
267
d9c39b0a 268#endif /* BABELTRACE_LOGGING_COMP_LOGGING_H */
This page took 0.047169 seconds and 4 git commands to generate.