From: Simon Marchi Date: Wed, 27 Jul 2022 13:20:50 +0000 (-0400) Subject: src/cpp-common: add BT_APPEND_CAUSE*() and BT_CLOG_*() macros X-Git-Url: http://git.efficios.com/?p=babeltrace.git;a=commitdiff_plain;h=b49534620adcfe1f1dd38bf8ad3e8d9f088bbc8e src/cpp-common: add BT_APPEND_CAUSE*() and BT_CLOG_*() macros This patch adds the following files: `src/cpp-common/cfg-logging.hpp`: Offers the following macros: BT_CLOG*_EX(): Logs using a given logging configuration reference. BT_CLOG*_STR_EX(): Logs a literal string using a given logging configuration reference. BT_CLOG*_ERRNO_EX(): Logs an errno message using a given logging configuration reference. BT_CLOG*_MEM_EX(): Logs memory bytes using a given logging configuration reference. All the macros above also have their variant without the `_EX` suffix which uses the logging configuration reference `BT_CLOG_CFG`. Those macros decide, depending on what's available within the provided logging configuration object, how to prefix the complete log message. `src/cpp-common/cfg-error-reporting.hpp`: Offers the following macros: BT_APPEND_CAUSE_EX(): BT_APPEND_CAUSE_STR_EX(): BT_APPEND_CAUSE_ERRNO_EX(): Appends a cause to the error of the current thread using a given logging configuration reference. All the macros above also have their variant without the `_EX` suffix which uses the logging configuration reference `BT_CLOG_CFG`. Those macros decide, depending on what's available within the provided logging configuration object, which BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_*() macro to use. `src/cpp-common/cfg-error-reporting-throw.hpp`: Offers the following macros: BT_APPEND_CAUSE_AND_THROW_EX(): Appends a cause to the error of the current thread using a given logging configuration reference, and throws an exception. BT_APPEND_CAUSE_AND_RETHROW_EX(): Appends a cause to the error of the current thread using a given logging configuration reference, and rethrows. BT_STR_APPEND_CAUSE_AND_THROW_EX(): Appends a cause to the error of the current thread using a given logging configuration reference, and throws an exception. BT_STR_APPEND_CAUSE_AND_RETHROW_EX(): Appends a cause to the error of the current thread using a given logging configuration reference, and rethrows. BT_ERRNO_APPEND_CAUSE_AND_THROW_EX(): Appends a cause with an errno message to the error of the current thread using a given logging configuration reference, and throws an exception. BT_ERRNO_APPEND_CAUSE_AND_RETHROW_EX(): Appends a cause with an errno message to the error of the current thread using a given logging configuration reference, and rethrows. All the macros above also have their variant without the `_EX` suffix which uses the logging configuration reference `BT_CLOG_CFG`. `src/cpp-common/cfg-logging-error-reporting.hpp`: Offers the following macros: BT_CLOGE_APPEND_CAUSE_EX(): Logs using a given logging configuration reference and appends a cause to the error of the current thread. BT_CLOGE_STR_APPEND_CAUSE_EX(): Logs a literal string using a given logging configuration reference and appends a cause to the error of the current thread. BT_CLOGE_ERRNO_APPEND_CAUSE_EX(): Logs an errno message using a given logging configuration reference and appends a cause to the error of the current thread. BT_CLOGE_MEM_APPEND_CAUSE_EX(): Logs memory bytes using a given logging configuration reference and appends a cause to the error of the current thread. All the macros above also have their variant without the `_EX` suffix which uses the logging configuration reference `BT_CLOG_CFG`. Those macros decide, depending on what's available within the provided logging configuration object, which BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_*() macro to use. `src/cpp-common/cfg-logging-error-reporting-throw.hpp`: Offers the following macros: BT_CLOGE_APPEND_CAUSE_AND_THROW_EX(): Logs using a given logging configuration reference, appends a cause to the error of the current thread, and throws an exception. BT_CLOGE_APPEND_CAUSE_AND_RETHROW_EX(): Logs using a given logging configuration reference, appends a cause to the error of the current thread, and rethrows. BT_CLOGE_STR_APPEND_CAUSE_AND_THROW_EX(): Logs a literal string using a given logging configuration reference, appends a cause to the error of the current thread, and throws an exception. BT_CLOGE_STR_APPEND_CAUSE_AND_RETHROW_EX(): Logs a literal string using a given logging configuration reference, appends a cause to the error of the current thread, and rethrows. BT_CLOGE_ERRNO_APPEND_CAUSE_AND_THROW_EX(): Logs an errno message using a given logging configuration reference, appends a cause to the error of the current thread, and throws an exception. BT_CLOGE_ERRNO_APPEND_CAUSE_AND_RETHROW_EX(): Logs an errno message using a given logging configuration reference, appends a cause to the error of the current thread, and rethrows. BT_CLOGE_MEM_APPEND_CAUSE_AND_THROW_EX(): Logs memory bytes using a given logging configuration reference, appends a cause to the error of the current thread, and throws an exception. BT_CLOGE_MEM_APPEND_CAUSE_AND_RETHROW_EX(): Logs memory bytes using a given logging configuration reference, appends a cause to the error of the current thread, and rethrows. All the macros above also have their variant without the `_EX` suffix which uses the logging configuration reference `BT_CLOG_CFG`. The intended usage is as such: 1. In a header file defining a class, have a logging configuration member: bt2_common::LogCfg _mLogCfg; Make the constructor of the class accept a logging configuration reference and copy it to `_mLogCfg`. 2. In the source file which implements the methods of the class, at the top of the file, write: #define BT_LOG_TAG "MY/TAG" #define BT_CLOG_CFG _mLogCfg #include "cpp-common/cfg-logging-error-reporting-throw.hpp" 3. In any method of the class, when there's an error, call: BT_CLOGE_APPEND_CAUSE_AND_THROW(bt2_common::Error, "Cannot do this and that: code=%d", code); Signed-off-by: Philippe Proulx Change-Id: I3e689fe612b90be2f8f69288bc7947c727434d5a Reviewed-on: https://review.lttng.org/c/babeltrace/+/10810 Tested-by: jenkins --- diff --git a/src/cpp-common/Makefile.am b/src/cpp-common/Makefile.am index bcf43c22..64bc8d34 100644 --- a/src/cpp-common/Makefile.am +++ b/src/cpp-common/Makefile.am @@ -1,6 +1,11 @@ # SPDX-License-Identifier: MIT EXTRA_DIST = bt2 \ + cfg-error-reporting-throw.hpp \ + cfg-error-reporting.hpp \ + cfg-logging-error-reporting-throw.hpp \ + cfg-logging-error-reporting.hpp \ + cfg-logging.hpp \ exc.hpp \ log-cfg.hpp \ nlohmann/json.hpp \ diff --git a/src/cpp-common/cfg-error-reporting-throw.hpp b/src/cpp-common/cfg-error-reporting-throw.hpp new file mode 100644 index 00000000..2d1400ed --- /dev/null +++ b/src/cpp-common/cfg-error-reporting-throw.hpp @@ -0,0 +1,96 @@ +/* + * SPDX-License-Identifier: MIT + * + * Copyright (c) 2022 Francis Deslauriers + * Copyright (c) 2022 Simon Marchi + * Copyright (c) 2022 Philippe Proulx + */ + +#ifndef BABELTRACE_CPP_COMMON_CFG_ERROR_REPORTING_THROW_HPP +#define BABELTRACE_CPP_COMMON_CFG_ERROR_REPORTING_THROW_HPP + +#include +#include "cfg-error-reporting.hpp" + +/* + * Appends a cause to the error of the current thread using the + * configuration `_log_cfg` and throws an instance of `_exc_cls`. + */ +#define BT_APPEND_CAUSE_AND_THROW_EX(_log_cfg, _exc_cls, _fmt, ...) \ + do { \ + BT_APPEND_CAUSE_EX((_log_cfg), _fmt, ##__VA_ARGS__); \ + throw _exc_cls {}; \ + } while (false) + +#define BT_APPEND_CAUSE_AND_THROW(_exc_cls, _fmt, ...) \ + BT_APPEND_CAUSE_AND_THROW_EX((BT_CLOG_CFG), _exc_cls, _fmt, ##__VA_ARGS__) + +/* + * Appends a cause to the error of the current thread using the + * configuration `_log_cfg` and rethrows. + */ +#define BT_APPEND_CAUSE_AND_RETHROW_EX(_log_cfg, _fmt, ...) \ + do { \ + BT_APPEND_CAUSE_EX((_log_cfg), _fmt, ##__VA_ARGS__); \ + throw; +} +while (false) + +#define BT_APPEND_CAUSE_AND_RETHROW(_fmt, ...) \ + BT_APPEND_CAUSE_AND_RETHROW_EX((BT_CLOG_CFG), _fmt, ##__VA_ARGS__) + +/* + * Appends a cause to the error of the current thread using the + * configuration `_log_cfg` and throws an instance of `_exc_cls`. + */ +#define BT_APPEND_CAUSE_STR_AND_THROW_EX(_log_cfg, _exc_cls, _str) \ + do { \ + BT_APPEND_CAUSE_STR_EX((_log_cfg), _str); \ + throw _exc_cls {}; \ + } while (false) + +#define BT_APPEND_CAUSE_STR_AND_THROW(_exc_cls, _str) \ + BT_APPEND_CAUSE_STR_AND_THROW_EX((BT_CLOG_CFG), _exc_cls, _str) + +/* + * Appends a cause to the error of the current thread using the + * configuration `_log_cfg` and rethrows. + */ +#define BT_APPEND_CAUSE_STR_AND_RETHROW_EX(_log_cfg, _str) \ + do { \ + BT_APPEND_CAUSE_STR_EX((_log_cfg), _str); \ + throw; \ + } while (false) + +#define BT_APPEND_CAUSE_STR_AND_RETHROW(_str) \ + BT_APPEND_CAUSE_STR_AND_RETHROW_EX((BT_CLOG_CFG), _str) + +/* + * Appends a cause with an errno message to the error of the current + * thread, using the initial message `_init_msg` and the configuration + * `_log_cfg`, and throws an instance of `_exc_cls`. + */ +#define BT_APPEND_CAUSE_ERRNO_AND_THROW_EX(_log_cfg, _exc_cls, _init_msg, _fmt, ...) \ + do { \ + BT_APPEND_CAUSE_ERRNO_EX((_log_cfg), _init_msg, _fmt, ##__VA_ARGS__); \ + throw _exc_cls {}; \ + } while (false) + +#define BT_APPEND_CAUSE_ERRNO_AND_THROW(_exc_cls, _init_msg, _fmt, ...) \ + BT_APPEND_CAUSE_ERRNO_AND_THROW_EX((BT_CLOG_CFG), _exc_cls, _init_msg, _fmt, ##__VA_ARGS__) + +/* + * Appends a cause with an errno message to the error of the current + * thread, using the initial message `_init_msg` and the configuration + * `_log_cfg`, and rethrows. + */ +#define BT_APPEND_CAUSE_ERRNO_AND_RETHROW_EX(_log_cfg, _init_msg, _fmt, ...) \ + do { \ + BT_APPEND_CAUSE_ERRNO_EX((_log_cfg), _init_msg, _fmt, ##__VA_ARGS__); \ + throw; \ + } while (false) + +#define BT_APPEND_CAUSE_ERRNO_AND_RETHROW(_init_msg, _fmt, ...) \ + BT_APPEND_CAUSE_ERRNO_AND_RETHROW_EX((BT_CLOG_CFG), _init_msg, _fmt, ##__VA_ARGS__) + +#endif /* BABELTRACE_CPP_COMMON_CFG_ERROR_REPORTING_THROW_HPP */ diff --git a/src/cpp-common/cfg-error-reporting.hpp b/src/cpp-common/cfg-error-reporting.hpp new file mode 100644 index 00000000..5951a865 --- /dev/null +++ b/src/cpp-common/cfg-error-reporting.hpp @@ -0,0 +1,58 @@ +/* + * SPDX-License-Identifier: MIT + * + * Copyright (c) 2022 Simon Marchi + * Copyright (c) 2022 Philippe Proulx + */ + +#ifndef BABELTRACE_CPP_COMMON_CFG_ERROR_REPORTING_HPP +#define BABELTRACE_CPP_COMMON_CFG_ERROR_REPORTING_HPP + +#include +#include "cfg-logging.hpp" + +/* + * Appends a cause to the error of the current thread using the logging + * configuration `_log_cfg`. + */ +#define BT_APPEND_CAUSE_EX(_log_cfg, _fmt, ...) \ + do { \ + if ((_log_cfg).selfMsgIter()) { \ + BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR((_log_cfg).selfMsgIter(), \ + _fmt, ##__VA_ARGS__); \ + } else if ((_log_cfg).selfComp()) { \ + BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT((_log_cfg).selfComp(), _fmt, \ + ##__VA_ARGS__); \ + } else if ((_log_cfg).selfCompCls()) { \ + BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT_CLASS((_log_cfg).selfCompCls(), \ + _fmt, ##__VA_ARGS__); \ + } else { \ + BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN((_log_cfg).moduleName(), _fmt, \ + ##__VA_ARGS__); \ + } \ + } while (false) + +#define BT_APPEND_CAUSE(_fmt, ...) BT_APPEND_CAUSE_EX((BT_CLOG_CFG), _fmt, ##__VA_ARGS__) + +/* + * Appends a cause to the error of the current thread using the logging + * configuration `_log_cfg`. + */ +#define BT_APPEND_CAUSE_STR_EX(_log_cfg, _str) BT_APPEND_CAUSE_EX((_log_cfg), "%s", _str) + +#define BT_APPEND_CAUSE_STR(_str) BT_APPEND_CAUSE_STR_EX((BT_CLOG_CFG), _str) + +/* + * Appends a cause with an errno message to the error of the current + * thread using the logging configuration `_log_cfg`. + */ +#define BT_APPEND_CAUSE_ERRNO_EX(_log_cfg, _init_msg, _fmt, ...) \ + do { \ + const auto errStr = g_strerror(errno); \ + BT_APPEND_CAUSE_EX((_log_cfg), "%s: %s" _fmt, _init_msg, errStr, ##__VA_ARGS__); \ + } while (false) + +#define BT_APPEND_CAUSE_ERRNO(_init_msg, _fmt, ...) \ + BT_APPEND_CAUSE_EX((BT_CLOG_CFG), _init_msg, _fmt, ##__VA_ARGS__) + +#endif /* BABELTRACE_CPP_COMMON_CFG_ERROR_REPORTING_HPP */ diff --git a/src/cpp-common/cfg-logging-error-reporting-throw.hpp b/src/cpp-common/cfg-logging-error-reporting-throw.hpp new file mode 100644 index 00000000..55a3f981 --- /dev/null +++ b/src/cpp-common/cfg-logging-error-reporting-throw.hpp @@ -0,0 +1,129 @@ +/* + * SPDX-License-Identifier: MIT + * + * Copyright (c) 2022 Francis Deslauriers + * Copyright (c) 2022 Simon Marchi + * Copyright (c) 2022 Philippe Proulx + */ + +#ifndef BABELTRACE_CPP_COMMON_CFG_LOGGING_ERROR_REPORTING_THROW_HPP +#define BABELTRACE_CPP_COMMON_CFG_LOGGING_ERROR_REPORTING_THROW_HPP + +#include +#include "cfg-logging-error-reporting.hpp" + +/* + * Logs with the error level using the configuration `_log_cfg`, appends + * a cause to the error of the current thread, and throws an instance of + * `_exc_cls`. + */ +#define BT_CLOGE_APPEND_CAUSE_AND_THROW_EX(_log_cfg, _exc_cls, _fmt, ...) \ + do { \ + BT_CLOGE_APPEND_CAUSE_EX((_log_cfg), _fmt, ##__VA_ARGS__); \ + throw _exc_cls {}; \ + } while (false) + +#define BT_CLOGE_APPEND_CAUSE_AND_THROW(_exc_cls, _fmt, ...) \ + BT_CLOGE_APPEND_CAUSE_AND_THROW_EX((BT_CLOG_CFG), _exc_cls, _fmt, ##__VA_ARGS__) + +/* + * Logs with the error level using the configuration `_log_cfg`, appends + * a cause to the error of the current thread, and rethrows. + */ +#define BT_CLOGE_APPEND_CAUSE_AND_RETHROW_EX(_log_cfg, _fmt, ...) \ + do { \ + BT_CLOGE_APPEND_CAUSE_EX((_log_cfg), _fmt, ##__VA_ARGS__); \ + throw; \ + } while (false) + +#define BT_CLOGE_APPEND_CAUSE_AND_RETHROW(_fmt, ...) \ + BT_CLOGE_APPEND_CAUSE_AND_RETHROW_EX((BT_CLOG_CFG), _fmt, ##__VA_ARGS__) + +/* + * Logs with the error level using the configuration `_log_cfg`, appends + * a cause to the error of the current thread, and throws an instance of + * `_exc_cls`. + */ +#define BT_CLOGE_STR_APPEND_CAUSE_AND_THROW_EX(_log_cfg, _exc_cls, _str) \ + do { \ + BT_CLOGE_STR_APPEND_CAUSE_EX((_log_cfg), _str); \ + throw _exc_cls {}; \ + } while (false) + +#define BT_CLOGE_STR_APPEND_CAUSE_AND_THROW(_exc_cls, _str) \ + BT_CLOGE_APPEND_CAUSE_AND_THROW_EX((BT_CLOG_CFG), _exc_cls, _str) + +/* + * Logs with the error level using the configuration `_log_cfg`, appends + * a cause to the error of the current thread, and rethrows. + */ +#define BT_CLOGE_STR_APPEND_CAUSE_AND_RETHROW_EX(_log_cfg, _str) \ + do { \ + BT_CLOGE_STR_APPEND_CAUSE_EX((_log_cfg), _str); \ + throw; \ + } while (false) + +#define BT_CLOGE_STR_APPEND_CAUSE_AND_RETHROW(_str) \ + BT_CLOGE_APPEND_CAUSE_AND_RETHROW_EX((BT_CLOG_CFG), _str) + +/* + * Logs an errno message with the error level, using the configuration + * `_log_cfg`, having the initial message `_init_msg`, appends a cause + * to the error of the current thread, and throws an instance of + * `_exc_cls`. + */ +#define BT_CLOGE_ERRNO_APPEND_CAUSE_AND_THROW_EX(_log_cfg, _exc_cls, _init_msg, _fmt, ...) \ + do { \ + BT_CLOGE_ERRNO_APPEND_CAUSE_EX((_log_cfg), _init_msg, _fmt, ##__VA_ARGS__); \ + throw _exc_cls {}; \ + } while (false) + +#define BT_CLOGE_ERRNO_APPEND_CAUSE_AND_THROW(_exc_cls, _init_msg, _fmt, ...) \ + BT_CLOGE_ERRNO_APPEND_CAUSE_AND_THROW_EX((BT_CLOG_CFG), _exc_cls, _init_msg, _fmt, \ + ##__VA_ARGS__) + +/* + * Logs an errno message with the error level, using the configuration + * `_log_cfg`, having the initial message `_init_msg`, appends a cause + * to the error of the current thread, and rethrows. + */ +#define BT_CLOGE_ERRNO_APPEND_CAUSE_AND_RETHROW_EX(_log_cfg, _init_msg, _fmt, ...) \ + do { \ + BT_CLOGE_ERRNO_APPEND_CAUSE_EX((_log_cfg), _init_msg, _fmt, ##__VA_ARGS__); \ + throw; \ + } while (false) + +#define BT_CLOGE_ERRNO_APPEND_CAUSE_AND_RETHROW(_init_msg, _fmt, ...) \ + BT_CLOGE_ERRNO_APPEND_CAUSE_AND_RETHROW_EX((BT_CLOG_CFG), _init_msg, _fmt, ##__VA_ARGS__) + +/* + * Logs memory bytes with the error level using the configuration + * `_log_cfg`, appends a cause to the error of the current thread, and + * throws an instance of `_exc_cls`. + */ +#define BT_CLOGE_MEM_APPEND_CAUSE_AND_THROW_EX(_log_cfg, _exc_cls, _data, _data_sz, _fmt, ...) \ + do { \ + BT_CLOGE_MEM_APPEND_CAUSE_EX((_log_cfg), (_data), (_data_sz), _fmt, ##__VA_ARGS__); \ + throw _exc_cls {}; \ + } while (false) + +#define BT_CLOGE_MEM_APPEND_CAUSE_AND_THROW(_exc_cls, _data, _data_sz, _fmt, ...) \ + BT_CLOGE_MEM_APPEND_CAUSE_AND_THROW_EX((BT_CLOG_CFG), _exc_cls, (_data), (_data_sz), _fmt, \ + ##__VA_ARGS__) + +/* + * Logs memory bytes with the error level using the configuration + * `_log_cfg`, appends a cause to the error of the current thread, and + * rethrows. + */ +#define BT_CLOGE_MEM_APPEND_CAUSE_AND_RETHROW_EX(_log_cfg, _data, _data_sz, _fmt, ...) \ + do { \ + BT_CLOGE_MEM_APPEND_CAUSE_EX((_log_cfg), (_data), (_data_sz), _fmt, ##__VA_ARGS__); \ + throw; \ + } while (false) + +#define BT_CLOGE_MEM_APPEND_CAUSE_AND_RETHROW(_data, _data_sz, _fmt, ...) \ + BT_CLOGE_MEM_APPEND_CAUSE_AND_RETHROW_EX((BT_CLOG_CFG), (_data), (_data_sz), _fmt, \ + ##__VA_ARGS__) + +#endif /* BABELTRACE_CPP_COMMON_CFG_LOGGING_ERROR_REPORTING_THROW_HPP */ diff --git a/src/cpp-common/cfg-logging-error-reporting.hpp b/src/cpp-common/cfg-logging-error-reporting.hpp new file mode 100644 index 00000000..0bd0ec0d --- /dev/null +++ b/src/cpp-common/cfg-logging-error-reporting.hpp @@ -0,0 +1,67 @@ +/* + * SPDX-License-Identifier: MIT + * + * Copyright (c) 2022 Simon Marchi + * Copyright (c) 2022 Philippe Proulx + */ + +#ifndef BABELTRACE_CPP_COMMON_CFG_LOGGING_ERROR_REPORTING_HPP +#define BABELTRACE_CPP_COMMON_CFG_LOGGING_ERROR_REPORTING_HPP + +#include +#include "cfg-logging.hpp" +#include "cfg-error-reporting.hpp" + +/* + * Logs with the error level using the configuration `_log_cfg` and + * appends a cause to the error of the current thread. + */ +#define BT_CLOGE_APPEND_CAUSE_EX(_log_cfg, _fmt, ...) \ + do { \ + BT_CLOGE_EX((_log_cfg), _fmt, ##__VA_ARGS__); \ + BT_APPEND_CAUSE_EX((_log_cfg), _fmt, ##__VA_ARGS__); \ + } while (false) + +#define BT_CLOGE_APPEND_CAUSE(_fmt, ...) \ + BT_CLOGE_APPEND_CAUSE_EX((BT_CLOG_CFG), _fmt, ##__VA_ARGS__) + +/* + * Logs with the error level using the configuration `_log_cfg` and + * appends a cause to the error of the current thread. + */ +#define BT_CLOGE_STR_APPEND_CAUSE_EX(_log_cfg, _str) \ + do { \ + BT_CLOGE_STR_EX((_log_cfg), _str); \ + BT_APPEND_CAUSE_EX((_log_cfg), "%s", _str); \ + } while (false) + +#define BT_CLOGE_STR_APPEND_CAUSE(_str) BT_CLOGE_STR_APPEND_CAUSE_EX((BT_CLOG_CFG), _str) + +/* + * Logs an errno message with the error level, using the configuration + * `_log_cfg`, having the initial message `_init_msg`, and appends a + * cause to the error of the current thread. + */ +#define BT_CLOGE_ERRNO_APPEND_CAUSE_EX(_log_cfg, _init_msg, _fmt, ...) \ + do { \ + BT_CLOGE_ERRNO_EX((_log_cfg), _init_msg, _fmt, ##__VA_ARGS__); \ + BT_APPEND_CAUSE_ERRNO_EX((_log_cfg), _init_msg, _fmt, ##__VA_ARGS__); \ + } while (false) + +#define BT_CLOGE_ERRNO_APPEND_CAUSE(_init_msg, _fmt, ...) \ + BT_CLOGE_ERRNO_APPEND_CAUSE_EX((BT_CLOG_CFG), _init_msg, _fmt, ##__VA_ARGS__) + +/* + * Logs memory bytes with the error level using the configuration + * `_log_cfg` and appends a cause to the error of the current thread. + */ +#define BT_CLOGE_MEM_APPEND_CAUSE_EX(_log_cfg, _data, _data_sz, _fmt, ...) \ + do { \ + BT_CLOGE_MEM_EX((_log_cfg), (_data), (_data_sz), _fmt, ##__VA_ARGS__); \ + BT_APPEND_CAUSE_EX((_log_cfg), _fmt, ##__VA_ARGS__); \ + } while (false) + +#define BT_CLOGE_MEM_APPEND_CAUSE(_data, _data_sz, _fmt, ...) \ + BT_CLOGE_MEM_APPEND_CAUSE_EX((BT_CLOG_CFG), (_data), (_data_sz), _fmt, ##__VA_ARGS__) + +#endif /* BABELTRACE_CPP_COMMON_CFG_LOGGING_ERROR_REPORTING_HPP */ diff --git a/src/cpp-common/cfg-logging.hpp b/src/cpp-common/cfg-logging.hpp new file mode 100644 index 00000000..aeee7622 --- /dev/null +++ b/src/cpp-common/cfg-logging.hpp @@ -0,0 +1,162 @@ +/* + * SPDX-License-Identifier: MIT + * + * Copyright (c) 2022 Philippe Proulx + */ + +#ifndef BABELTRACE_CPP_COMMON_CFG_LOGGING_HPP +#define BABELTRACE_CPP_COMMON_CFG_LOGGING_HPP + +#ifdef BT_CLOG_CFG +# ifdef BT_LOG_OUTPUT_LEVEL +# error `BT_LOG_OUTPUT_LEVEL` may not be defined when `BT_CLOG_CFG` is. +# endif +# define BT_LOG_OUTPUT_LEVEL ((BT_CLOG_CFG).logLevel()) +#endif + +#include +#include "logging/log.h" + +#define _BT_CLOG_OBJ_FMT_PREFIX "[%s] " + +/* + * Logs with the level `_lvl` using the configuration `_log_cfg`. + */ +#define BT_CLOG_EX(_lvl, _log_cfg, _fmt, ...) \ + if (BT_LOG_ON_CUR_LVL(_lvl, (_log_cfg).logLevel())) { \ + if ((_log_cfg).selfComp()) { \ + _bt_log_write((_lvl), _BT_LOG_TAG, _BT_CLOG_OBJ_FMT_PREFIX _fmt, \ + (_log_cfg).compName(), ##__VA_ARGS__); \ + } else if ((_log_cfg).compCls()) { \ + _bt_log_write((_lvl), _BT_LOG_TAG, _BT_CLOG_OBJ_FMT_PREFIX _fmt, \ + (_log_cfg).compClsName(), ##__VA_ARGS__); \ + } else { \ + _bt_log_write((_lvl), _BT_LOG_TAG, _fmt, ##__VA_ARGS__); \ + } \ + } + +#define BT_CLOGF_EX(_log_cfg, _fmt, ...) BT_CLOG_EX(BT_LOG_FATAL, (_log_cfg), _fmt, ##__VA_ARGS__) +#define BT_CLOGE_EX(_log_cfg, _fmt, ...) BT_CLOG_EX(BT_LOG_ERROR, (_log_cfg), _fmt, ##__VA_ARGS__) +#define BT_CLOGW_EX(_log_cfg, _fmt, ...) BT_CLOG_EX(BT_LOG_WARNING, (_log_cfg), _fmt, ##__VA_ARGS__) +#define BT_CLOGI_EX(_log_cfg, _fmt, ...) BT_CLOG_EX(BT_LOG_INFO, (_log_cfg), _fmt, ##__VA_ARGS__) +#define BT_CLOGD_EX(_log_cfg, _fmt, ...) BT_CLOG_EX(BT_LOG_DEBUG, (_log_cfg), _fmt, ##__VA_ARGS__) +#define BT_CLOGT_EX(_log_cfg, _fmt, ...) BT_CLOG_EX(BT_LOG_TRACE, (_log_cfg), _fmt, ##__VA_ARGS__) +#define BT_CLOG(_lvl, _fmt, ...) BT_CLOG_EX((_lvl), (BT_CLOG_CFG), _fmt, ##__VA_ARGS__) +#define BT_CLOGF(_fmt, ...) BT_CLOG(BT_LOG_FATAL, _fmt, ##__VA_ARGS__) +#define BT_CLOGE(_fmt, ...) BT_CLOG(BT_LOG_ERROR, _fmt, ##__VA_ARGS__) +#define BT_CLOGW(_fmt, ...) BT_CLOG(BT_LOG_WARNING, _fmt, ##__VA_ARGS__) +#define BT_CLOGI(_fmt, ...) BT_CLOG(BT_LOG_INFO, _fmt, ##__VA_ARGS__) +#define BT_CLOGD(_fmt, ...) BT_CLOG(BT_LOG_DEBUG, _fmt, ##__VA_ARGS__) +#define BT_CLOGT(_fmt, ...) BT_CLOG(BT_LOG_TRACE, _fmt, ##__VA_ARGS__) + +/* + * Logs with the level `_lvl` using the configuration `_log_cfg`. + */ +#define BT_CLOG_STR_EX(_lvl, _log_cfg, _str) BT_CLOG_EX((_lvl), (_log_cfg), "%s", _str) + +#define BT_CLOGF_STR_EX(_log_cfg, _str) BT_CLOG_STR_EX(BT_LOG_FATAL, (_log_cfg), _str) +#define BT_CLOGE_STR_EX(_log_cfg, _str) BT_CLOG_STR_EX(BT_LOG_ERROR, (_log_cfg), _str) +#define BT_CLOGW_STR_EX(_log_cfg, _str) BT_CLOG_STR_EX(BT_LOG_WARNING, (_log_cfg), _str) +#define BT_CLOGI_STR_EX(_log_cfg, _str) BT_CLOG_STR_EX(BT_LOG_INFO, (_log_cfg), _str) +#define BT_CLOGD_STR_EX(_log_cfg, _str) BT_CLOG_STR_EX(BT_LOG_DEBUG, (_log_cfg), _str) +#define BT_CLOGT_STR_EX(_log_cfg, _str) BT_CLOG_STR_EX(BT_LOG_TRACE, (_log_cfg), _str) +#define BT_CLOG_STR(_lvl, _str) BT_CLOG_STR_EX((_lvl), (BT_CLOG_CFG), _str) +#define BT_CLOGF_STR(_str) BT_CLOG_STR(BT_LOG_FATAL, _str) +#define BT_CLOGE_STR(_str) BT_CLOG_STR(BT_LOG_ERROR, _str) +#define BT_CLOGW_STR(_str) BT_CLOG_STR(BT_LOG_WARNING, _str) +#define BT_CLOGI_STR(_str) BT_CLOG_STR(BT_LOG_INFO, _str) +#define BT_CLOGD_STR(_str) BT_CLOG_STR(BT_LOG_DEBUG, _str) +#define BT_CLOGT_STR(_str) BT_CLOG_STR(BT_LOG_TRACE, _str) + +/* + * Logs an errno message with the level `_lvl`, using the configuration + * `_log_cfg`, and having the initial message `_init_msg`. + */ +#define BT_CLOG_ERRNO_EX(_lvl, _log_cfg, _init_msg, _fmt, ...) \ + if (BT_LOG_ON(_lvl)) { \ + const auto errStr = g_strerror(errno); \ + if ((_log_cfg).selfComp()) { \ + _bt_log_write((_lvl), _BT_LOG_TAG, _BT_CLOG_OBJ_FMT_PREFIX "%s: %s" _fmt, \ + (_log_cfg).compName(), _init_msg, errStr, ##__VA_ARGS__); \ + } else if ((_log_cfg).compCls()) { \ + _bt_log_write((_lvl), _BT_LOG_TAG, _BT_CLOG_OBJ_FMT_PREFIX "%s: %s" _fmt, \ + (_log_cfg).compClsName(), _init_msg, errStr, ##__VA_ARGS__); \ + } else { \ + _bt_log_write((_lvl), _BT_LOG_TAG, "%s: %s" _fmt, _init_msg, errStr, ##__VA_ARGS__); \ + } \ + } + +#define BT_CLOGF_ERRNO_EX(_log_cfg, _init_msg, _fmt, ...) \ + BT_CLOG_ERRNO_EX(BT_LOG_FATAL, (_log_cfg), _init_msg, _fmt, ##__VA_ARGS__) +#define BT_CLOGE_ERRNO_EX(_log_cfg, _init_msg, _fmt, ...) \ + BT_CLOG_ERRNO_EX(BT_LOG_ERROR, (_log_cfg), _init_msg, _fmt, ##__VA_ARGS__) +#define BT_CLOGW_ERRNO_EX(_log_cfg, _init_msg, _fmt, ...) \ + BT_CLOG_ERRNO_EX(BT_LOG_WARNING, (_log_cfg), _init_msg, _fmt, ##__VA_ARGS__) +#define BT_CLOGI_ERRNO_EX(_log_cfg, _init_msg, _fmt, ...) \ + BT_CLOG_ERRNO_EX(BT_LOG_INFO, (_log_cfg), _init_msg, _fmt, ##__VA_ARGS__) +#define BT_CLOGD_ERRNO_EX(_log_cfg, _init_msg, _fmt, ...) \ + BT_CLOG_ERRNO_EX(BT_LOG_DEBUG, (_log_cfg), _init_msg, _fmt, ##__VA_ARGS__) +#define BT_CLOGT_ERRNO_EX(_log_cfg, _init_msg, _fmt, ...) \ + BT_CLOG_ERRNO_EX(BT_LOG_TRACE, (_log_cfg), _init_msg, _fmt, ##__VA_ARGS__) +#define BT_CLOG_ERRNO(_lvl, _init_msg, _fmt, ...) \ + BT_CLOG_ERRNO_EX((_lvl), (BT_CLOG_CFG), _init_msg, _fmt, ##__VA_ARGS__) +#define BT_CLOGF_ERRNO(_init_msg, _fmt, ...) \ + BT_CLOG_ERRNO(BT_LOG_FATAL, _init_msg, _fmt, ##__VA_ARGS__) +#define BT_CLOGE_ERRNO(_init_msg, _fmt, ...) \ + BT_CLOG_ERRNO(BT_LOG_ERROR, _init_msg, _fmt, ##__VA_ARGS__) +#define BT_CLOGW_ERRNO(_init_msg, _fmt, ...) \ + BT_CLOG_ERRNO(BT_LOG_WARNING, _init_msg, _fmt, ##__VA_ARGS__) +#define BT_CLOGI_ERRNO(_init_msg, _fmt, ...) \ + BT_CLOG_ERRNO(BT_LOG_INFO, _init_msg, _fmt, ##__VA_ARGS__) +#define BT_CLOGD_ERRNO(_init_msg, _fmt, ...) \ + BT_CLOG_ERRNO(BT_LOG_DEBUG, _init_msg, _fmt, ##__VA_ARGS__) +#define BT_CLOGT_ERRNO(_init_msg, _fmt, ...) \ + BT_CLOG_ERRNO(BT_LOG_TRACE, _init_msg, _fmt, ##__VA_ARGS__) + +/* + * Logs memory bytes with the level `_lvl` using the configuration + * `_log_cfg`. + */ +#define BT_CLOG_MEM_EX(_lvl, _log_cfg, _data, _data_sz, _fmt, ...) \ + if (BT_LOG_ON(_lvl)) { \ + if ((_log_cfg).selfComp()) { \ + _bt_log_write_mem((_lvl), _BT_LOG_TAG, (_data), (_data_sz), \ + _BT_CLOG_OBJ_FMT_PREFIX _fmt, (_log_cfg).compName(), ##__VA_ARGS__); \ + } else if ((_log_cfg).compCls()) { \ + _bt_log_write_mem((_lvl), _BT_LOG_TAG, (_data), (_data_sz), \ + _BT_CLOG_OBJ_FMT_PREFIX _fmt, (_log_cfg).compClsName(), \ + ##__VA_ARGS__); \ + } else { \ + _bt_log_write_mem((_lvl), _BT_LOG_TAG, (_data), (_data_sz), _fmt, ##__VA_ARGS__); \ + } \ + } + +#define BT_CLOGF_MEM_EX(_log_cfg, _data, _data_sz, _fmt, ...) \ + BT_CLOG_MEM_EX(BT_LOG_FATAL, (_log_cfg), (_data), (_data_sz), ##__VA_ARGS__) +#define BT_CLOGE_MEM_EX(_log_cfg, _data, _data_sz, _fmt, ...) \ + BT_CLOG_MEM_EX(BT_LOG_ERROR, (_log_cfg), (_data), (_data_sz), ##__VA_ARGS__) +#define BT_CLOGW_MEM_EX(_log_cfg, _data, _data_sz, _fmt, ...) \ + BT_CLOG_MEM_EX(BT_LOG_WARNING, (_log_cfg), (_data), (_data_sz), ##__VA_ARGS__) +#define BT_CLOGI_MEM_EX(_log_cfg, _data, _data_sz, _fmt, ...) \ + BT_CLOG_MEM_EX(BT_LOG_INFO, (_log_cfg), (_data), (_data_sz), ##__VA_ARGS__) +#define BT_CLOGD_MEM_EX(_log_cfg, _data, _data_sz, _fmt, ...) \ + BT_CLOG_MEM_EX(BT_LOG_DEBUG, (_log_cfg), (_data), (_data_sz), ##__VA_ARGS__) +#define BT_CLOGT_MEM_EX(_log_cfg, _data, _data_sz, _fmt, ...) \ + BT_CLOG_MEM_EX(BT_LOG_TRACE, (_log_cfg), (_data), (_data_sz), ##__VA_ARGS__) + +#define BT_CLOG_MEM(_lvl, _data, _data_sz, _fmt, ...) \ + BT_CLOG_MEM_EX(_lvl, (BT_CLOG_CFG), (_data), (_data_sz), _fmt, ##__VA_ARGS__) +#define BT_CLOGF_MEM(_data, _data_sz, _fmt, ...) \ + BT_CLOG_MEM(BT_LOG_FATAL, (_data), (_data_sz), _fmt, ##__VA_ARGS__) +#define BT_CLOGE_MEM(_data, _data_sz, _fmt, ...) \ + BT_CLOG_MEM(BT_LOG_ERROR, (_data), (_data_sz), _fmt, ##__VA_ARGS__) +#define BT_CLOGW_MEM(_data, _data_sz, _fmt, ...) \ + BT_CLOG_MEM(BT_LOG_WARNING, (_data), (_data_sz), _fmt, ##__VA_ARGS__) +#define BT_CLOGI_MEM(_data, _data_sz, _fmt, ...) \ + BT_CLOG_MEM(BT_LOG_INFO, (_data), (_data_sz), _fmt, ##__VA_ARGS__) +#define BT_CLOGD_MEM(_data, _data_sz, _fmt, ...) \ + BT_CLOG_MEM(BT_LOG_DEBUG, (_data), (_data_sz), _fmt, ##__VA_ARGS__) +#define BT_CLOGT_MEM(_data, _data_sz, _fmt, ...) \ + BT_CLOG_MEM(BT_LOG_TRACE, (_data), (_data_sz), _fmt, ##__VA_ARGS__) + +#endif /* BABELTRACE_CPP_COMMON_CFG_LOGGING_HPP */