BDE 4.14.0 Production release
|
Macros | |
#define | BSLS_LOG(severity, ...) |
#define | BSLS_LOG_FATAL(...) |
#define | BSLS_LOG_ERROR(...) |
#define | BSLS_LOG_WARN(...) |
#define | BSLS_LOG_INFO(...) |
#define | BSLS_LOG_DEBUG(...) |
#define | BSLS_LOG_TRACE(...) |
#define | BSLS_LOG_SIMPLE(severity, msg) (BloombergLP::bsls::Log::logMessage((severity), __FILE__, __LINE__, (msg))) |
Provide a namespace for low-level logging functions.
printf
-style format specificationprintf
format specificationprintf
format specificationprintf
format specificationprintf
format specificationprintf
format specificationprintf
format specificationThis component provides a set of macros, along with a namespace, bsls::Log
, which contains a suite of utility functions for logging in low-level code. The macros and functions in this component provide a consistent interface for logging across different platforms through the use of a global cross-platform log message handler function. Users can customize the logging behavior by providing their own log message handler function. Note that this component is intended to be used only when a more fully-featured logger is not available.
This section provides documentation for the macros defined in this component.
Using the functionality of this component instead of writing messages directly to stderr
has the following advantages:
stdout
or stderr
; writing directly to either of these streams is known to hang the process when no console is attached.This section describes the functionality provided by this component in more detail.
The bsls::Log
class provides two static
methods, logMessageHandler
and setLogMessageHandler
, which can be used, respectively, to retrieve and set the globally installed log message handler through which all log messages are written. All log message handlers must follow the signature and contract requirements of the bsls::Log::LogMessageHandler
typedef
.
The log message handler bsls::Log::platformDefaultMessageHandler
is installed by default as the global handler. This handler writes all log messages to stderr
, except in Windows non-console mode where the destination of the OutputDebugString
function is used.
In addition to the default handler, the log message handlers bsls::Log::stdoutMessageHandler
and bsls::Log::stderrMessageHandler
are provided as a set of simple handlers that write log messages to stdout
and stderr
, respectively.
There are four ways to invoke the currently installed log message handler, depending on the specific behavior required: The macro BSLS_LOG
allows a formatted message to be written using a printf
-style format string. The macro BSLS_LOG_SIMPLE
allows a simple, unformatted string to be written. Both of the macros automatically use the file name and line number of the point that the macro was invoked. The static
methods bsls::Log::logFormattedMessage
and bsls::Log::logMessage
provide the same functionality as BSLS_LOG
and 'BSLS_LOG_SIMPLE, respectively, except that these two methods allow a file name and line number to be passed in as parameters. This is described in table form as follows:
Clients submitting a message to bsls::Log
(either through a function or one of the macros) either implicitly or explicitly provide a severity level describing the relative importance of that message to clients. The possible severity levels are FATAL, ERROR, WARNING, INFO, DEBUG, and TRACE (these are enumerated in bsls_logseverity ).
The severity of a logged message is used to determine whether the message is published to the log using the currently installed LogMessageHandler
callback. Also, typically a LogMessageHandler
callback implementation will report a message's severity along side that message in the log.
Clients can configure the severity threshold, at or above which a log message will be published, using setSeverityThreshold
. For example:
This section illustrates the intended use of this component.
Suppose that we want to write a formatted log message using printf
-style format specifiers when the preconditions of a function are not met. The BSLS_LOG
macro can be used for this purpose.
First, we begin to define a function, add
, which will return the sum of two positive integer values:
Now, we check the precondition of the function, and use the BSLS_LOG_ERROR
macro to write a log message if one of the input parameters is less than 0:
Next, we may erroneously call the add
function with a negative argument:
Finally, assuming the default log message handler is currently installed, we observe the following output printed to stderr
or to the Windows debugger:
Note that an arbitrary string should never be passed to BSLS_LOG
as the format string. If the string happens to contain printf
-style format specifiers but the expected substitutions are not present, it will lead to undefined behavior.
#define BSLS_LOG | ( | severity, | |
... | |||
) |
#define BSLS_LOG_DEBUG | ( | ... | ) |
#define BSLS_LOG_ERROR | ( | ... | ) |
#define BSLS_LOG_FATAL | ( | ... | ) |
#define BSLS_LOG_INFO | ( | ... | ) |
#define BSLS_LOG_SIMPLE | ( | severity, | |
msg | |||
) | (BloombergLP::bsls::Log::logMessage((severity), __FILE__, __LINE__, (msg))) |
#define BSLS_LOG_TRACE | ( | ... | ) |
#define BSLS_LOG_WARN | ( | ... | ) |