From 8eb59971bc7d2d813604009ceef37a7d68eca824 Mon Sep 17 00:00:00 2001 From: Francis Deslauriers Date: Fri, 8 Nov 2019 11:08:10 -0500 Subject: [PATCH] Cleanup: comp-logging.h: template `BT_COMP_OR_COMP_CLASS_LOG*` macros This commit is motivated by the need to have the `BT_COMP_OR_COMP_CLASS_LOGW_ERRNO()` logging macro in a future commit. It adds the following template macros to simplify add logging macros for other logging levels: BT_COMP_OR_COMP_CLASS_LOG BT_COMP_OR_COMP_CLASS_LOG_ERRNO I didn't add the `BT_COMP_OR_COMP_CLASS_LOG_APPEND_CAUSE` template as it's less likely that we will need it for logging levels other than `BT_LOG_ERROR`. Also, to be uniform across the file, I moved all the `_lvl` parameter to the first position for all macros in this file. Signed-off-by: Francis Deslauriers Change-Id: Ifbce021eecc08f8fc994a8ac0f4174ae3e1df853 Reviewed-on: https://review.lttng.org/c/babeltrace/+/2354 Tested-by: jenkins Reviewed-by: Simon Marchi --- src/logging/comp-logging.h | 49 ++++++++++++++++++++++++++++++-------- 1 file changed, 39 insertions(+), 10 deletions(-) diff --git a/src/logging/comp-logging.h b/src/logging/comp-logging.h index 8d429ed1..d51dab4e 100644 --- a/src/logging/comp-logging.h +++ b/src/logging/comp-logging.h @@ -139,7 +139,7 @@ #define BT_COMP_LOG_SUPPORTED /* Logs and appends error cause from component context. */ -#define BT_COMP_LOG_APPEND_CAUSE(_self_comp, _lvl, _fmt, ...) \ +#define BT_COMP_LOG_APPEND_CAUSE(_lvl, _self_comp, _fmt, ...) \ do { \ BT_COMP_LOG(_lvl, _self_comp, _fmt, ##__VA_ARGS__); \ (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT( \ @@ -148,12 +148,12 @@ /* Logs error and appends error cause from component context. */ #define BT_COMP_LOGE_APPEND_CAUSE(_self_comp, _fmt, ...) \ - BT_COMP_LOG_APPEND_CAUSE(_self_comp, BT_LOG_ERROR, _fmt, ##__VA_ARGS__) + BT_COMP_LOG_APPEND_CAUSE(BT_LOG_ERROR, _self_comp, _fmt, ##__VA_ARGS__) /* * Logs and appends error cause from component context - the errno edition. */ -#define BT_COMP_LOG_APPEND_CAUSE_ERRNO(_self_comp, _lvl, _msg, _fmt, ...) \ +#define BT_COMP_LOG_APPEND_CAUSE_ERRNO(_lvl, _self_comp, _msg, _fmt, ...) \ do { \ const char *error_str = g_strerror(errno); \ BT_COMP_LOG(_lvl, _self_comp, _msg ": %s" _fmt, error_str, \ @@ -167,14 +167,22 @@ * edition. */ #define BT_COMP_LOGE_APPEND_CAUSE_ERRNO(_self_comp, _msg, _fmt, ...) \ - BT_COMP_LOG_APPEND_CAUSE_ERRNO(_self_comp, BT_LOG_ERROR, _msg, _fmt, ##__VA_ARGS__) + BT_COMP_LOG_APPEND_CAUSE_ERRNO(BT_LOG_ERROR, _self_comp, _msg, _fmt, ##__VA_ARGS__) /* Logs error from component class context. */ #define BT_COMP_CLASS_LOGE(_self_comp_class, _fmt, ...) \ BT_COMP_CLASS_LOG(BT_LOG_ERROR,_self_comp_class, _fmt, ##__VA_ARGS__) +/* Logs error and errno string from component class context. */ +#define BT_COMP_CLASS_LOG_ERRNO(_lvl, _self_comp_class, _msg, _fmt, ...) \ + BT_LOG_WRITE_ERRNO((_lvl), BT_LOG_TAG, _msg, \ + _BT_COMP_LOG_COMP_PREFIX _fmt, \ + bt_component_class_get_name( \ + bt_self_component_class_as_component_class(_self_comp_class)) \ + ##__VA_ARGS__) + /* Logs and appends error cause from component class context. */ -#define BT_COMP_CLASS_LOG_APPEND_CAUSE(_self_comp_class, _lvl, _fmt, ...) \ +#define BT_COMP_CLASS_LOG_APPEND_CAUSE(_lvl, _self_comp_class, _fmt, ...) \ do { \ BT_COMP_CLASS_LOG(_lvl, _self_comp_class, _fmt, ##__VA_ARGS__); \ (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT_CLASS( \ @@ -183,13 +191,13 @@ /* Logs error and appends error cause from component class context. */ #define BT_COMP_CLASS_LOGE_APPEND_CAUSE(_self_comp_class, _fmt, ...) \ - BT_COMP_CLASS_LOG_APPEND_CAUSE(_self_comp_class, BT_LOG_ERROR, _fmt, ##__VA_ARGS__) + BT_COMP_CLASS_LOG_APPEND_CAUSE(BT_LOG_ERROR, _self_comp_class, _fmt, ##__VA_ARGS__) /* * Logs and appends error cause from component class context - the errno * edition. */ -#define BT_COMP_CLASS_LOG_APPEND_CAUSE_ERRNO(_self_comp_class, _lvl, _msg, _fmt, ...) \ +#define BT_COMP_CLASS_LOG_APPEND_CAUSE_ERRNO(_lvl, _self_comp_class, _msg, _fmt, ...) \ do { \ const char *error_str = g_strerror(errno); \ BT_COMP_CLASS_LOG(_lvl, _self_comp_class, _msg ": %s" _fmt, error_str, \ @@ -203,24 +211,45 @@ * edition. */ #define BT_COMP_CLASS_LOGE_APPEND_CAUSE_ERRNO(_self_comp_class, _msg, _fmt, ...) \ - BT_COMP_CLASS_LOG_APPEND_CAUSE_ERRNO(_self_comp_class, BT_LOG_ERROR, _msg, _fmt, \ + BT_COMP_CLASS_LOG_APPEND_CAUSE_ERRNO(BT_LOG_ERROR, _self_comp_class, _msg, _fmt, \ ##__VA_ARGS__) /* * Logs error from component or component class context, depending on whichever * is set. */ +#define BT_COMP_OR_COMP_CLASS_LOG(_lvl, _self_comp, _self_comp_class, _fmt, ...) \ + do { \ + /* Only one of `_self_comp` and `_self_comp_class` must be set. */ \ + BT_ASSERT((!!(_self_comp) != (!!_self_comp_class))); \ + if (_self_comp) { \ + BT_COMP_LOG(_lvl, _self_comp, _fmt, ##__VA_ARGS__); \ + } else { \ + BT_COMP_CLASS_LOG(_lvl, _self_comp_class, _fmt, ##__VA_ARGS__); \ + } \ + } while (0) + #define BT_COMP_OR_COMP_CLASS_LOGE(_self_comp, _self_comp_class, _fmt, ...) \ + BT_COMP_OR_COMP_CLASS_LOG(BT_LOG_ERROR,_self_comp, _self_comp_class, _fmt, ##__VA_ARGS__) + +/* + * Logs error with errno string from component or component class context, + * depending on whichever is set. + */ +#define BT_COMP_OR_COMP_CLASS_LOG_ERRNO(_lvl, _self_comp, _self_comp_class, _msg, _fmt, ...) \ do { \ /* Only one of `_self_comp` and `_self_comp_class` must be set. */ \ BT_ASSERT((!!(_self_comp) != (!!_self_comp_class))); \ if (_self_comp) { \ - BT_COMP_LOGE(_fmt, ##__VA_ARGS__); \ + BT_COMP_LOG_ERRNO(_lvl, _self_comp, _msg, _fmt, ##__VA_ARGS__); \ } else { \ - BT_COMP_CLASS_LOGE(_self_comp_class, _fmt, ##__VA_ARGS__); \ + BT_COMP_CLASS_LOG_ERRNO(_lvl, _self_comp_class, _msg, _fmt, ##__VA_ARGS__); \ } \ } while (0) +#define BT_COMP_OR_COMP_CLASS_LOGW_ERRNO(_self_comp, _self_comp_class, _msg, _fmt, ...) \ + BT_COMP_OR_COMP_CLASS_LOG_ERRNO(BT_LOG_WARNING, _self_comp, _self_comp_class, _msg, _fmt, ##__VA_ARGS__) + /* * Logs error and appends error cause from component or component class context, * depending on whichever is set. -- 2.34.1