BDE 4.39.x Production Release
Loading...
Searching...
No Matches
ball_filteringobserver

Detailed Description

Provide an observer that filters log records.

Outline

Purpose

Provide an observer that filters log records.

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
V
,--------------.
( ball::Observer )
`--------------'
publish
dtor
Definition ball_filteringobserver.h:170

ball::FilteringObserver processes the log records it receives through its publish method and conditionally forwards them to the inner observer supplied at construction.

Usage

This section illustrates intended use of this component.

Example 1: Basic Usage

This example shows how to use filtering observer to route some log records to a designated observer. A common use case is routing all log records for a given category (or category pattern) to a separate log file.

First, we create a filter that will match a log record's category against the pattern "EQUITY.*":

bool categoryFilter(const ball::Record& record, const ball::Context&)
{
"EQUITY.*");
}
Definition ball_context.h:297
const char * category() const
Return the category attribute of this record attributes object.
Definition ball_recordattributes.h:613
Definition ball_record.h:176
RecordAttributes & fixedFields()
Return the modifiable fixed fields of this log record.
Definition ball_record.h:397
static bool isMatch(const char *inputString, const char *pattern)

Then, we create the observer that will receive filtered log records and create a filtering observer:

new ball::TestObserver(&bsl::cout));
ball::FilteringObserver filteringObserver(innerObserver, categoryFilter);
Definition ball_testobserver.h:217
Definition bslstl_sharedptr.h:1838

Next, we issue a series of log records and verify that only records with the category matching the pattern are published to the inner observer:

record.createInplace();
fixedFields.setCategory("CURNCY.USDGBP");
record->setFixedFields(fixedFields);
filteringObserver.publish(record, context);
assert(0 == innerObserver->numPublishedRecords()); // dropped
fixedFields.setCategory("EQUITY.NYSE");
record->setFixedFields(fixedFields);
filteringObserver.publish(record, context);
assert(1 == innerObserver->numPublishedRecords()); // forwarded
fixedFields.setCategory("EQUIT.");
record->setFixedFields(fixedFields);
filteringObserver.publish(record, context);
assert(1 == innerObserver->numPublishedRecords()); // dropped
Definition ball_recordattributes.h:275
void setCategory(const bsl::string_view &category)
Definition ball_recordattributes.h:564
void createInplace()
Definition bslstl_sharedptr.h:5539
@ e_PASSTHROUGH
Definition ball_transmission.h:216