BDE 4.14.0 Production release
|
Provide an observer that emits log records to a stream.
This component provides a concrete implementation of the ball::Observer
protocol for receiving and processing log records:
ball::StreamObserver
is a concrete class derived from ball::Observer
that processes the log records it receives through its publish
method by writing them to an output stream. Given its minimal functionality, ball::StreamObserver
should be used with care in a production environment. It is not recommended to construct this observer with file-based streams due to lack of any file rotation functionality.
By default, the output format of published log records is:
where DATE
and TIME
are of the form DDMonYYYY
and HH:MM:SS.mmm
, respectively (Mon
being the 3-letter abbreviation for the month). For example, assuming that no user-defined fields are present, a log record will have the following appearance when the default format is in effect:
The default format can be overridden by supplying a suitable formatting functor to setRecordFormatFunctor
. For example, an instance of ball::RecordStringFormatter
conveniently provides such a functor:
The above statement will cause subsequent records to be logged in a format that is almost identical to the default format except that the timestamp attribute will be written in ISO 8601 format. See ball_recordstringformatter for information on how format specifications are defined and interpreted.
Note that the observer emits newline characters at the beginning and at the end of a log record by default, so the user needs to add them explicitly to the format string to preserve this behavior.
Also note that in the sample message above the timestamp has millisecond precision (18MAY2005_18:58:12.076
). If microsecond precision is desired instead, consider using either the D
or O
format specification supported by ball_recordstringformatter .
All methods of ball::StreamObserver
are thread-safe, and can be called concurrently by multiple threads.
This section illustrates intended use of this component.
The following snippets of code illustrate the basic usage of ball::StreamObserver
.
First create a ball::Record
object record
and a ball::Context
object context
. Note that the default values for these objects (or their contained objects) are perfectly suitable for logging purposes.
Next, create a stream observer observer
with the bsl::cout
as the output stream.
Finally, publish record
and context
to observer
.
This will produce the following output on stdout
: