Cleanup: comp-logging.h: template `BT_COMP_OR_COMP_CLASS_LOG*` macros
authorFrancis Deslauriers <francis.deslauriers@efficios.com>
Fri, 8 Nov 2019 16:08:10 +0000 (11:08 -0500)
committerSimon Marchi <simon.marchi@efficios.com>
Fri, 15 Nov 2019 15:44:52 +0000 (10:44 -0500)
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 <francis.deslauriers@efficios.com>
Change-Id: Ifbce021eecc08f8fc994a8ac0f4174ae3e1df853
Reviewed-on: https://review.lttng.org/c/babeltrace/+/2354
Tested-by: jenkins <jenkins@lttng.org>
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
src/logging/comp-logging.h

index 8d429ed1650d911177f1771b5e490dc933d92650..d51dab4e0f9b0d4a77ab7a1f77b781ab43d65a2f 100644 (file)
 #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(     \
 
 /* 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,              \
  * 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(       \
 
 /* 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,  \
  * 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.
This page took 0.027559 seconds and 4 git commands to generate.