| 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((_lvl), BT_LOG_TAG, _BT_COMP_LOG_COMP_PREFIX _fmt, \ |
| 24 | (_self_comp) ? \ |
| 25 | bt_component_get_name( \ |
| 26 | bt_self_component_as_component(_self_comp)) : \ |
| 27 | _BT_COMP_LOG_COMP_NA_STR, \ |
| 28 | ##__VA_ARGS__) |
| 29 | |
| 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__) |
| 36 | |
| 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, \ |
| 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 | #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, \ |
| 49 | (_self_comp) ? \ |
| 50 | bt_component_get_name( \ |
| 51 | bt_self_component_as_component(_self_comp)) : \ |
| 52 | _BT_COMP_LOG_COMP_NA_STR, \ |
| 53 | ##__VA_ARGS__) |
| 54 | |
| 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, \ |
| 58 | (_self_comp) ? \ |
| 59 | bt_component_get_name( \ |
| 60 | bt_self_component_as_component(_self_comp)) : \ |
| 61 | _BT_COMP_LOG_COMP_NA_STR, \ |
| 62 | ##__VA_ARGS__) |
| 63 | |
| 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, \ |
| 67 | (_self_comp) ? \ |
| 68 | bt_component_get_name( \ |
| 69 | bt_self_component_as_component(_self_comp)) : \ |
| 70 | _BT_COMP_LOG_COMP_NA_STR, \ |
| 71 | ##__VA_ARGS__) |
| 72 | |
| 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__) |
| 122 | |
| 123 | #define BT_COMP_LOG_SUPPORTED |
| 124 | |
| 125 | /* Logs and appends error cause from component context. */ |
| 126 | #define BT_COMP_LOG_APPEND_CAUSE(_lvl, _self_comp, _fmt, ...) \ |
| 127 | do { \ |
| 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__); \ |
| 131 | } while (0) |
| 132 | |
| 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__) |
| 136 | |
| 137 | /* |
| 138 | * Logs and appends error cause from component context - the errno edition. |
| 139 | */ |
| 140 | #define BT_COMP_LOG_APPEND_CAUSE_ERRNO(_lvl, _self_comp, _msg, _fmt, ...) \ |
| 141 | do { \ |
| 142 | const char *error_str = g_strerror(errno); \ |
| 143 | BT_COMP_LOG(_lvl, _self_comp, _msg ": %s" _fmt, error_str, \ |
| 144 | ##__VA_ARGS__); \ |
| 145 | (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT( \ |
| 146 | _self_comp, _msg ": %s" _fmt, error_str, ##__VA_ARGS__); \ |
| 147 | } while (0) |
| 148 | |
| 149 | /* |
| 150 | * Logs error and appends error cause from component context - the errno |
| 151 | * edition. |
| 152 | */ |
| 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__) |
| 155 | |
| 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__) |
| 159 | |
| 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)) \ |
| 166 | ##__VA_ARGS__) |
| 167 | |
| 168 | /* Logs and appends error cause from component class context. */ |
| 169 | #define BT_COMP_CLASS_LOG_APPEND_CAUSE(_lvl, _self_comp_class, _fmt, ...) \ |
| 170 | do { \ |
| 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__); \ |
| 174 | } while (0) |
| 175 | |
| 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__) |
| 179 | |
| 180 | /* |
| 181 | * Logs and appends error cause from component class context - the errno |
| 182 | * edition. |
| 183 | */ |
| 184 | #define BT_COMP_CLASS_LOG_APPEND_CAUSE_ERRNO(_lvl, _self_comp_class, _msg, _fmt, ...) \ |
| 185 | do { \ |
| 186 | const char *error_str = g_strerror(errno); \ |
| 187 | BT_COMP_CLASS_LOG(_lvl, _self_comp_class, _msg ": %s" _fmt, error_str, \ |
| 188 | ##__VA_ARGS__); \ |
| 189 | (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT_CLASS( \ |
| 190 | _self_comp_class, _msg ": %s" _fmt, error_str, ##__VA_ARGS__); \ |
| 191 | } while (0) |
| 192 | |
| 193 | /* |
| 194 | * Logs error and appends error cause from component class context - the errno |
| 195 | * edition. |
| 196 | */ |
| 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, \ |
| 199 | ##__VA_ARGS__) |
| 200 | |
| 201 | /* |
| 202 | * Logs error from component or component class context, depending on whichever |
| 203 | * is set. |
| 204 | */ |
| 205 | #define BT_COMP_OR_COMP_CLASS_LOG(_lvl, _self_comp, _self_comp_class, _fmt, ...) \ |
| 206 | do { \ |
| 207 | /* Only one of `_self_comp` and `_self_comp_class` must be set. */ \ |
| 208 | BT_ASSERT((!!(_self_comp) != (!!_self_comp_class))); \ |
| 209 | if (_self_comp) { \ |
| 210 | BT_COMP_LOG(_lvl, _self_comp, _fmt, ##__VA_ARGS__); \ |
| 211 | } else { \ |
| 212 | BT_COMP_CLASS_LOG(_lvl, _self_comp_class, _fmt, ##__VA_ARGS__); \ |
| 213 | } \ |
| 214 | } while (0) |
| 215 | |
| 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__) |
| 224 | |
| 225 | /* |
| 226 | * Logs error with errno string from component or component class context, |
| 227 | * depending on whichever is set. |
| 228 | */ |
| 229 | #define BT_COMP_OR_COMP_CLASS_LOG_ERRNO(_lvl, _self_comp, _self_comp_class, _msg, _fmt, ...) \ |
| 230 | do { \ |
| 231 | /* Only one of `_self_comp` and `_self_comp_class` must be set. */ \ |
| 232 | BT_ASSERT((!!(_self_comp) != (!!_self_comp_class))); \ |
| 233 | if (_self_comp) { \ |
| 234 | BT_COMP_LOG_ERRNO(_lvl, _self_comp, _msg, _fmt, ##__VA_ARGS__); \ |
| 235 | } else { \ |
| 236 | BT_COMP_CLASS_LOG_ERRNO(_lvl, _self_comp_class, _msg, _fmt, ##__VA_ARGS__); \ |
| 237 | } \ |
| 238 | } while (0) |
| 239 | |
| 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__) |
| 242 | |
| 243 | /* |
| 244 | * Logs error and appends error cause from component or component class context, |
| 245 | * depending on whichever is set. |
| 246 | */ |
| 247 | #define BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(_self_comp, _self_comp_class, _fmt, ...) \ |
| 248 | do { \ |
| 249 | /* Only one of `_self_comp` and `_self_comp_class` must be set. */ \ |
| 250 | BT_ASSERT((!!(_self_comp) != (!!_self_comp_class))); \ |
| 251 | if (_self_comp) { \ |
| 252 | BT_COMP_LOGE_APPEND_CAUSE(_self_comp, _fmt, ##__VA_ARGS__); \ |
| 253 | } else { \ |
| 254 | BT_COMP_CLASS_LOGE_APPEND_CAUSE(_self_comp_class, _fmt, ##__VA_ARGS__); \ |
| 255 | } \ |
| 256 | } while (0) |
| 257 | |
| 258 | /* |
| 259 | * Logs error and appends error cause from message iterator context. |
| 260 | * |
| 261 | * There is no BT_SELF_MSG_LOGE yet, so use BT_COMP_LOGE for now. |
| 262 | */ |
| 263 | #define BT_MSG_ITER_LOGE_APPEND_CAUSE(_self_msg_iter, _fmt, ...) \ |
| 264 | do { \ |
| 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__); \ |
| 269 | } while (0) |
| 270 | |
| 271 | #endif /* BABELTRACE_LOGGING_COMP_LOGGING_H */ |