X-Git-Url: http://git.efficios.com/?a=blobdiff_plain;f=src%2Flogging%2Fcomp-logging.h;h=00bce0473a5ac2ea5025bb30d6e4f7eebd0b2354;hb=HEAD;hp=aae775c579266e55d1d02013abe5b7da81436bde;hpb=d23b766e7b7542ddff2211264b4ece1f3a347773;p=babeltrace.git diff --git a/src/logging/comp-logging.h b/src/logging/comp-logging.h index aae775c5..00bce047 100644 --- a/src/logging/comp-logging.h +++ b/src/logging/comp-logging.h @@ -1,28 +1,12 @@ -#ifndef BABELTRACE_LOGGING_COMP_LOGGING_H -#define BABELTRACE_LOGGING_COMP_LOGGING_H - /* - * Copyright 2019 Philippe Proulx + * SPDX-License-Identifier: MIT * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. + * Copyright 2019 Philippe Proulx */ +#ifndef BABELTRACE_LOGGING_COMP_LOGGING_H +#define BABELTRACE_LOGGING_COMP_LOGGING_H + #ifndef BT_LOG_TAG # error Please define a tag with BT_LOG_TAG before including this file. #endif @@ -36,7 +20,9 @@ /* Logs with level `_lvl` for self component `_self_comp` */ #define BT_COMP_LOG(_lvl, _self_comp, _fmt, ...) \ - BT_LOG_WRITE((_lvl), BT_LOG_TAG, _BT_COMP_LOG_COMP_PREFIX _fmt, \ + BT_LOG_WRITE_PRINTF_CUR_LVL((enum bt_log_level) (_lvl), \ + (enum bt_log_level) (BT_LOG_OUTPUT_LEVEL), BT_LOG_TAG, \ + _BT_COMP_LOG_COMP_PREFIX _fmt, \ (_self_comp) ? \ bt_component_get_name( \ bt_self_component_as_component(_self_comp)) : \ @@ -45,13 +31,16 @@ /* Logs with level `_lvl` for self component class `_self_comp_class` */ #define BT_COMP_CLASS_LOG(_lvl, _self_comp_class, _fmt, ...) \ - BT_LOG_WRITE((_lvl), BT_LOG_TAG, _BT_COMP_LOG_COMP_PREFIX _fmt, \ + BT_LOG_WRITE_PRINTF_CUR_LVL((enum bt_log_level) (_lvl), \ + (enum bt_log_level) (BT_LOG_OUTPUT_LEVEL), BT_LOG_TAG, \ + _BT_COMP_LOG_COMP_PREFIX _fmt, \ bt_component_class_get_name( \ bt_self_component_class_as_component_class( \ _self_comp_class)), ##__VA_ARGS__) #define BT_COMP_LOG_CUR_LVL(_lvl, _cur_lvl, _self_comp, _fmt, ...) \ - BT_LOG_WRITE_CUR_LVL((_lvl), (_cur_lvl), BT_LOG_TAG, \ + BT_LOG_WRITE_PRINTF_CUR_LVL((enum bt_log_level) (_lvl), \ + (enum bt_log_level) (_cur_lvl), BT_LOG_TAG, \ _BT_COMP_LOG_COMP_PREFIX _fmt, \ (_self_comp) ? \ bt_component_get_name( \ @@ -60,7 +49,9 @@ ##__VA_ARGS__) #define BT_COMP_LOG_ERRNO(_lvl, _self_comp, _msg, _fmt, ...) \ - BT_LOG_WRITE_ERRNO((_lvl), BT_LOG_TAG, _msg, \ + BT_LOG_WRITE_ERRNO_PRINTF_CUR_LVL((enum bt_log_level) (_lvl), \ + (enum bt_log_level) (BT_LOG_OUTPUT_LEVEL), \ + BT_LOG_TAG, _msg, \ _BT_COMP_LOG_COMP_PREFIX _fmt, \ (_self_comp) ? \ bt_component_get_name( \ @@ -69,7 +60,8 @@ ##__VA_ARGS__) #define BT_COMP_LOG_ERRNO_CUR_LVL(_lvl, _cur_lvl, _self_comp, _msg, _fmt, ...) \ - BT_LOG_WRITE_ERRNO_CUR_LVL((_lvl), (_cur_lvl), BT_LOG_TAG, _msg, \ + BT_LOG_WRITE_ERRNO_PRINTF_CUR_LVL((enum bt_log_level) (_lvl), \ + (enum bt_log_level) (_cur_lvl), BT_LOG_TAG, _msg, \ _BT_COMP_LOG_COMP_PREFIX _fmt, \ (_self_comp) ? \ bt_component_get_name( \ @@ -78,7 +70,9 @@ ##__VA_ARGS__) #define BT_COMP_LOG_MEM(_lvl, _self_comp, _data_ptr, _data_sz, _fmt, ...) \ - BT_LOG_WRITE_MEM((_lvl), BT_LOG_TAG, (_data_ptr), (_data_sz), \ + BT_LOG_WRITE_MEM_PRINTF_CUR_LVL((enum bt_log_level) (_lvl), \ + (enum bt_log_level) (BT_LOG_OUTPUT_LEVEL), BT_LOG_TAG, \ + (_data_ptr), (_data_sz), \ _BT_COMP_LOG_COMP_PREFIX _fmt, \ (_self_comp) ? \ bt_component_get_name( \ @@ -139,7 +133,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,10 +142,42 @@ /* 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(_lvl, _self_comp, _msg, _fmt, ...) \ + do { \ + const char *error_str = g_strerror(errno); \ + BT_COMP_LOG(_lvl, _self_comp, _msg ": %s" _fmt, error_str, \ + ##__VA_ARGS__); \ + (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT( \ + _self_comp, _msg ": %s" _fmt, error_str, ##__VA_ARGS__); \ + } while (0) + +/* + * Logs error and appends error cause from component context - the errno + * edition. + */ +#define BT_COMP_LOGE_APPEND_CAUSE_ERRNO(_self_comp, _msg, _fmt, ...) \ + 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_PRINTF_CUR_LVL((enum bt_log_level) (_lvl), \ + (enum bt_log_level) (BT_LOG_OUTPUT_LEVEL), 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( \ @@ -160,13 +186,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, \ @@ -179,10 +205,52 @@ * Logs error and appends error cause from component class context - the errno * edition. */ -#define BT_COMP_CLASS_LOGE_APPEND_CAUSE_ERRNO(_self_comp_class, _fmt, ...) \ - BT_COMP_CLASS_LOG_APPEND_CAUSE_ERRNO(_self_comp_class, BT_LOG_ERROR, _fmt, \ +#define BT_COMP_CLASS_LOGE_APPEND_CAUSE_ERRNO(_self_comp_class, _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__) +#define BT_COMP_OR_COMP_CLASS_LOGW(_self_comp, _self_comp_class, _fmt, ...) \ + BT_COMP_OR_COMP_CLASS_LOG(BT_LOG_WARNING,_self_comp, _self_comp_class, _fmt, ##__VA_ARGS__) +#define BT_COMP_OR_COMP_CLASS_LOGI(_self_comp, _self_comp_class, _fmt, ...) \ + BT_COMP_OR_COMP_CLASS_LOG(BT_LOG_INFO,_self_comp, _self_comp_class, _fmt, ##__VA_ARGS__) +#define BT_COMP_OR_COMP_CLASS_LOGD(_self_comp, _self_comp_class, _fmt, ...) \ + BT_COMP_OR_COMP_CLASS_LOG(BT_LOG_DEBUG,_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_LOG_ERRNO(_lvl, _self_comp, _msg, _fmt, ##__VA_ARGS__); \ + } else { \ + 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. @@ -198,4 +266,17 @@ } \ } while (0) +/* + * Logs error and appends error cause from message iterator context. + * + * There is no BT_SELF_MSG_LOGE yet, so use BT_COMP_LOGE for now. + */ +#define BT_MSG_ITER_LOGE_APPEND_CAUSE(_self_msg_iter, _fmt, ...) \ + do { \ + BT_COMP_LOG(BT_LOG_ERROR, bt_self_message_iterator_borrow_component(_self_msg_iter), \ + _fmt, ##__VA_ARGS__); \ + (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR( \ + _self_msg_iter, _fmt, ##__VA_ARGS__); \ + } while (0) + #endif /* BABELTRACE_LOGGING_COMP_LOGGING_H */