Quick Links:

bal | bbl | bdl | bsl

Namespaces

Component ball_filteringobserver
[Package ball]

Provide an observer that filters log records. More...

Namespaces

namespace  ball

Detailed Description

Outline
Purpose:
Provide an observer that filters log records.
Classes:
ball::FilteringObserver observer that filters log records
See also:
Component ball_record, Component ball_context, Component ball_loggermanager
Description:
This component provides a concrete implementation of the ball::Observer protocol for receiving and processing log records:
               ,-----------------------.
              ( ball::FilteringObserver )
               `-----------------------'
                           |              ctor
                           V
                    ,--------------.
                   ( ball::Observer )
                    `--------------'
                                          publish
                                          dtor
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&)
  {
      return ball::PatternUtil::isMatch(record.fixedFields().category(),
                                        "EQUITY.*");
  }
Then, we create the observer that will receive filtered log records and create a filtering observer:
  bsl::shared_ptr<ball::TestObserver> innerObserver(
                                         new ball::TestObserver(&bsl::cout));

  ball::FilteringObserver filteringObserver(innerObserver, categoryFilter);
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:
  const ball::Context context(ball::Transmission::e_PASSTHROUGH, 0, 1);

  ball::RecordAttributes fixedFields;

  bsl::shared_ptr<ball::Record> record;
  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