BDE 4.14.0 Production release
Loading...
Searching...
No Matches
ball_streamobserver

Detailed Description

Outline

Purpose

Provide an observer that emits log records to a stream.

Classes

See also
ball_record, ball_context, ball_loggermanager

Description

This component provides a concrete implementation of the ball::Observer protocol for receiving and processing log records:

,--------------------.
`--------------------'
| ctor
| setRecordFormatFunctor
V
,--------------.
( ball::Observer )
`--------------'
publish
releaseRecords
dtor
Definition ball_streamobserver.h:192

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.

Log Record Formatting

By default, the output format of published log records is:

DATE_TIME PID THREAD-ID SEVERITY FILE LINE CATEGORY MESSAGE USER-FIELDS

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:

18MAY2005_18:58:12.076 7959 1 WARN ball_streamobserver2.t.cpp 404 TEST hi!

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:

streamObserver.setRecordFormatFunctor(
ball::RecordStringFormatter("\n%I %p:%t %s %f:%l %c %m %u\n"));
Definition ball_recordstringformatter.h:218

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 .

Thread Safety

All methods of ball::StreamObserver are thread-safe, and can be called concurrently by multiple threads.

Usage

This section illustrates intended use of this component.

Example 1: Basic Usage

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.

ball::UserFields fieldValues;
ball::Context context;
record(new (*ga) ball::Record(attributes, fieldValues, ga), ga);
Definition ball_context.h:295
Definition ball_recordattributes.h:274
Definition ball_record.h:178
Definition ball_userfields.h:136
Definition bslstl_sharedptr.h:1830
Definition bslma_allocator.h:457
static Allocator * globalAllocator(Allocator *basicAllocator=0)
Definition bslma_default.h:905

Next, create a stream observer observer with the bsl::cout as the output stream.

ball::StreamObserver observer(&bsl::cout);

Finally, publish record and context to observer.

observer.publish(record, context);

This will produce the following output on stdout:

01JAN0001_24:00:00.000 0 0 OFF 0