BDE 4.14.0 Production release
|
Provide a record formatter that uses a printf
-style format spec.
printf
-style formatter for log recordsThis component provides a value-semantic function-object class, ball::RecordStringFormatter
, that is used to format log records according to a printf
-style format specification (see "Record Format Specification" below). A format specification and a timestamp offset (in the form of a bdlt::DatetimeInterval
) are optionally supplied upon construction of a ball::RecordStringFormatter
object (or simply "record formatter"). If a format specification is not supplied, a default one (defined below) is used. If a timestamp offset is not supplied, it defaults to 0. Both the format specification and timestamp offset of a record formatter can be modified following construction.
An overloaded operator()
is defined for ball::RecordStringFormatter
that takes a ball::Record
and an bsl::ostream
as arguments. This method formats the given record according to the format specification of the record formatter and outputs the result to the given stream. Additionally, each timestamp indicated in the format specification is biased by the timestamp offset of the record formatter prior to outputting it to the stream. This facilitates the logging of records in local time, if desired, in the event that the timestamp attribute of records are in UTC.
The following table lists the printf
-style (%
-prefixed) conversion specifications, including their expansions, that are recognized within the format specification of a record formatter:
(Note that F
is used to indicate the shortened form of __FILE__
rather than f
because f
was given its current interpretation in an earlier version of this component.)
In addition, the following '\'-escape sequences are interpolated in the formatted output as indicated when they occur in the format specification:
Any other text included in the format specification of the record formatter is output verbatim.
When not supplied at construction, the default format specification of a record formatter is:
A default-formatted record having no user-defined fields will have the following appearance:
Log record attributes are rendered as space-separated name="value"
pairs (for example: mylibrary.username="mbloomberg"). Note that attribute names are not quoted, whereas attribute values, if they are strings, are always quoted.
The following snippets of code illustrate how to use an instance of ball::RecordStringFormatter
to format log records.
First we instantiate a record formatter with an explicit format specification (but we accept the default timestamp offset since it will not be used in this example):
The chosen format specification indicates that, when a record is formatted using formatter
, the thread Id attribute of the record will be output followed by the message attribute of the record.
Next we create a default ball::Record
and set the thread Id and message attributes of the record to dummy values:
The following "invocation" of the formatter
function object formats record
to bsl::cout
according to the format specification supplied at construction:
As a result of this call, the following is printed to stdout
: