src/cpp-common: add BT_APPEND_CAUSE*() and BT_CLOG_*() macros
authorSimon Marchi <simon.marchi@efficios.com>
Wed, 27 Jul 2022 13:20:50 +0000 (09:20 -0400)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Mon, 11 Sep 2023 15:24:02 +0000 (11:24 -0400)
commitb49534620adcfe1f1dd38bf8ad3e8d9f088bbc8e
tree2af83185a9b35ac222e0a430a5d234a461dd5e5e
parentf197dce340f4723816cd473b7803129911606008
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 <eeppeliteloop@gmail.com>
Change-Id: I3e689fe612b90be2f8f69288bc7947c727434d5a
Reviewed-on: https://review.lttng.org/c/babeltrace/+/10810
Tested-by: jenkins <jenkins@lttng.org>
src/cpp-common/Makefile.am
src/cpp-common/cfg-error-reporting-throw.hpp [new file with mode: 0644]
src/cpp-common/cfg-error-reporting.hpp [new file with mode: 0644]
src/cpp-common/cfg-logging-error-reporting-throw.hpp [new file with mode: 0644]
src/cpp-common/cfg-logging-error-reporting.hpp [new file with mode: 0644]
src/cpp-common/cfg-logging.hpp [new file with mode: 0644]
This page took 0.025978 seconds and 4 git commands to generate.