8d429ed1650d911177f1771b5e490dc933d92650
[babeltrace.git] / src / logging / comp-logging.h
1 #ifndef BABELTRACE_LOGGING_COMP_LOGGING_H
2 #define BABELTRACE_LOGGING_COMP_LOGGING_H
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
30 #include <stdlib.h>
31 #include <babeltrace2/babeltrace.h>
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
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
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, ...) \
95 BT_COMP_LOG(BT_LOG_WARNING, (BT_COMP_LOG_SELF_COMP), _fmt, ##__VA_ARGS__)
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__)
100 #define BT_COMP_LOGT(_fmt, ...) \
101 BT_COMP_LOG(BT_LOG_TRACE, (BT_COMP_LOG_SELF_COMP), _fmt, ##__VA_ARGS__)
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) \
107 BT_COMP_LOG(BT_LOG_WARNING, (BT_COMP_LOG_SELF_COMP), "%s", (_str))
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))
112 #define BT_COMP_LOGT_STR(_str) \
113 BT_COMP_LOG(BT_LOG_TRACE, (BT_COMP_LOG_SELF_COMP), "%s", (_str))
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, ...) \
119 BT_COMP_LOG_ERRNO(BT_LOG_WARNING, (BT_COMP_LOG_SELF_COMP), _msg, _fmt, ##__VA_ARGS__)
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__)
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__)
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, ...) \
131 BT_COMP_LOG_MEM(BT_LOG_WARNING, (BT_COMP_LOG_SELF_COMP), (_data_ptr), (_data_sz), _fmt, ##__VA_ARGS__)
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__)
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__)
138
139 #define BT_COMP_LOG_SUPPORTED
140
141 /* Logs and appends error cause from component context. */
142 #define BT_COMP_LOG_APPEND_CAUSE(_self_comp, _lvl, _fmt, ...) \
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, ...) \
151 BT_COMP_LOG_APPEND_CAUSE(_self_comp, BT_LOG_ERROR, _fmt, ##__VA_ARGS__)
152
153 /*
154 * Logs and appends error cause from component context - the errno edition.
155 */
156 #define BT_COMP_LOG_APPEND_CAUSE_ERRNO(_self_comp, _lvl, _msg, _fmt, ...) \
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 */
169 #define BT_COMP_LOGE_APPEND_CAUSE_ERRNO(_self_comp, _msg, _fmt, ...) \
170 BT_COMP_LOG_APPEND_CAUSE_ERRNO(_self_comp, BT_LOG_ERROR, _msg, _fmt, ##__VA_ARGS__)
171
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
176 /* Logs and appends error cause from component class context. */
177 #define BT_COMP_CLASS_LOG_APPEND_CAUSE(_self_comp_class, _lvl, _fmt, ...) \
178 do { \
179 BT_COMP_CLASS_LOG(_lvl, _self_comp_class, _fmt, ##__VA_ARGS__); \
180 (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT_CLASS( \
181 _self_comp_class, _fmt, ##__VA_ARGS__); \
182 } while (0)
183
184 /* Logs error and appends error cause from component class context. */
185 #define BT_COMP_CLASS_LOGE_APPEND_CAUSE(_self_comp_class, _fmt, ...) \
186 BT_COMP_CLASS_LOG_APPEND_CAUSE(_self_comp_class, BT_LOG_ERROR, _fmt, ##__VA_ARGS__)
187
188 /*
189 * Logs and appends error cause from component class context - the errno
190 * edition.
191 */
192 #define BT_COMP_CLASS_LOG_APPEND_CAUSE_ERRNO(_self_comp_class, _lvl, _msg, _fmt, ...) \
193 do { \
194 const char *error_str = g_strerror(errno); \
195 BT_COMP_CLASS_LOG(_lvl, _self_comp_class, _msg ": %s" _fmt, error_str, \
196 ##__VA_ARGS__); \
197 (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT_CLASS( \
198 _self_comp_class, _msg ": %s" _fmt, error_str, ##__VA_ARGS__); \
199 } while (0)
200
201 /*
202 * Logs error and appends error cause from component class context - the errno
203 * edition.
204 */
205 #define BT_COMP_CLASS_LOGE_APPEND_CAUSE_ERRNO(_self_comp_class, _msg, _fmt, ...) \
206 BT_COMP_CLASS_LOG_APPEND_CAUSE_ERRNO(_self_comp_class, BT_LOG_ERROR, _msg, _fmt, \
207 ##__VA_ARGS__)
208
209 /*
210 * Logs error from component or component class context, depending on whichever
211 * is set.
212 */
213 #define BT_COMP_OR_COMP_CLASS_LOGE(_self_comp, _self_comp_class, _fmt, ...) \
214 do { \
215 /* Only one of `_self_comp` and `_self_comp_class` must be set. */ \
216 BT_ASSERT((!!(_self_comp) != (!!_self_comp_class))); \
217 if (_self_comp) { \
218 BT_COMP_LOGE(_fmt, ##__VA_ARGS__); \
219 } else { \
220 BT_COMP_CLASS_LOGE(_self_comp_class, _fmt, ##__VA_ARGS__); \
221 } \
222 } while (0)
223
224 /*
225 * Logs error and appends error cause from component or component class context,
226 * depending on whichever is set.
227 */
228 #define BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(_self_comp, _self_comp_class, _fmt, ...) \
229 do { \
230 /* Only one of `_self_comp` and `_self_comp_class` must be set. */ \
231 BT_ASSERT((!!(_self_comp) != (!!_self_comp_class))); \
232 if (_self_comp) { \
233 BT_COMP_LOGE_APPEND_CAUSE(_self_comp, _fmt, ##__VA_ARGS__); \
234 } else { \
235 BT_COMP_CLASS_LOGE_APPEND_CAUSE(_self_comp_class, _fmt, ##__VA_ARGS__); \
236 } \
237 } while (0)
238
239 #endif /* BABELTRACE_LOGGING_COMP_LOGGING_H */
This page took 0.034573 seconds and 3 git commands to generate.