|
BDE 4.39.x Production Release
|
Provide macros to facilitate bsl::format logging.
Provide macros to facilitate bsl::format logging.
*_BLOCKe_TRACE levele_DEBUG levele_INFO levele_WARN levele_ERROR levele_FATAL levelThis component provides preprocessor macros that facilitate logging using standard format format strings. This component provides additional macros that build on those defined in ball_log, and this documentation assumes readers will be familiar with the terminology established by that component, such as log category, log record, log level, etc.
All macros defined in this component are thread-safe, and can be invoked concurrently by multiple threads.
Additionally, each use of a logging macro will create a distinct log record, and ball::Observer implementations (like those in ball) generally guarantee that output for different log records are not interleaved.
This section documents the preprocessor macros defined in this component.
The macros defined in this subsection are the ones that are actually used to produce log records. A use of any of the logging macros require a logging category be established in scope – e.g., using BALL_LOG_SET_CATEGORY. For more information of setting the log category for a particular scope, see ball_log. Note that the formatted string that is generated for the message attribute of each log record includes the category that is in scope and the filename as established by the standard __FILE__ macro.
The code within any logging statement/code block must not produce any side effects because it may or may not be executed based on run-time configuration of the ball logging subsystem:
The following BALL_FMT_* macros, are the simplest mechanisms to format (using a standard format specification) a single message to a log:
where ARG1, ARG2, ... represents any sequence of values for which a bsl::format formatter is defined. The resulting formatted message string is logged with the severity indicated by the name of the macro (e.g., BALL_FMT_TRACE logs with severity ball::Severity::e_TRACE).
ball_log provides several kinds of macros to create potentially executed code blocks from which more complicated logging can be performed. As a reminder the most commonly used such block macros are:
Please see ball_log for the other such block macros.
Within logging code blocks the special macro, BALL_FMT provides standard format-style logging into the log record being built there.
The following code fragments illustrate the standard pattern of macro usage.
The following trivial example shows how to use the logging macros to log messages at various levels of severity.
First, we initialize the log category within the context of this function. The logging macros such as BALL_FMT_ERROR will not compile unless a category has been specified in the current lexical scope:
Then, we record messages at various levels of severity. These messages will be conditionally written to the log depending on the current logging threshold of the category (configured using the ball::LoggerManager singleton):
Next, we demonstrate how to use proprietary code within logging macros. Suppose you want to add the content of a vector to the log trace:
Note that the code block will be conditionally executed depending on the current logging threshold of the category. The code within the block must not produce any side effects, because its execution depends on the current logging configuration. The special macro BALL_FMT provides the means to write to the log record from within the block.
The following example shows how to use the bslfmt::streamed facility with types that have no support for bsl::formating, but have support for writing to an ostream to facilitate formatted logging.
Suppose we have a type that (we do not own, and) is some sort of identifier that is capable of "printing" itself to an ostream:
First, we initialize the log category within the context of this function. The logging macros such as BALL_FMT_ERROR will not compile unless a category has been specified in the current lexical scope:
Then, we record a message containing identifiers using bslfmt::streamed:
Note that the wrapper created by bslfmt::streamed uses the ostream insert operator<< of abc::Identifier to get the characters to print and uses the syntax of string formatting for the format specification.
| #define BALL_FMT | ( | ... | ) |
| #define BALL_FMT_DEBUG | ( | ... | ) |
| #define BALL_FMT_ERROR | ( | ... | ) |
| #define BALL_FMT_FATAL | ( | ... | ) |
| #define BALL_FMT_INFO | ( | ... | ) |
| #define BALL_FMT_TRACE | ( | ... | ) |
| #define BALL_FMT_WARN | ( | ... | ) |