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

Detailed Description

Outline

Purpose

Provide a helper for implementing the ball::Observer protocol.

Classes

See also
ball_observer, ball_record, ball_context

Description

This component provides a single class ball::ObserverAdapter that aids in the implementation of the ball::Observer protocol by allowing clients to implement that protocol by implementing a single method signature: publish(const ball::Record&, const ball::Context&). A primary goal of this component is to simplify the transition for older implementations of the ball::Observer protocol (that accept const-references to ball::Record objects) to the updated protocol (that accepts shared-pointers to ball::Record objects). ball::ObserverAdapter inherits from ball::Observer, and implements the (newer) overload of the publish method in (accepting a shared-pointer to a record) by calling the overload of the publish method (accepting a reference to a record).

Usage

This section illustrates intended use of this component.

Example 1: Concrete Observer Derived From ball::ObserverAdapter

The following code fragments illustrate the essentials of defining and using a concrete observer inherited from ball::ObserverAdapter.

First define a concrete observer MyOstreamObserver derived from ball::ObserverAdapter that declares a single publish method accepting a const-reference to a ball::Record object:

class MyOstreamObserver : public ball::ObserverAdapter {
bsl::ostream *d_stream;
public:
explicit MyOstreamObserver(bsl::ostream *stream) : d_stream(stream) { }
virtual ~MyOstreamObserver();
using Observer::publish; // avoid hiding base class method
virtual void publish(const ball::Record& record,
const ball::Context& context);
};
Definition ball_context.h:295
Definition ball_observeradapter.h:189
void publish(const Record &record, const Context &context) BSLS_KEYWORD_OVERRIDE=0
Definition ball_record.h:178

Then, we implement the public methods of MyOstreamObserver, including the publish method. This implementation of publish simply prints out the content of the record it receives to the stream supplied at construction.

MyOstreamObserver::~MyOstreamObserver()
{
}
void MyOstreamObserver::publish(const ball::Record& record,
const ball::Context&)
{
const ball::RecordAttributes& fixedFields = record.fixedFields();
*d_stream << fixedFields.timestamp() << ' '
<< fixedFields.processID() << ' '
<< fixedFields.threadID() << ' '
<< fixedFields.fileName() << ' '
<< fixedFields.lineNumber() << ' '
<< fixedFields.category() << ' '
<< fixedFields.message() << ' ';
const ball::UserFields& customFields = record.customFields();
const int numCustomFields = customFields.length();
for (int i = 0; i < numCustomFields; ++i) {
*d_stream << customFields[i] << ' ';
}
*d_stream << '\n' << bsl::flush;
}
Definition ball_recordattributes.h:274
int lineNumber() const
Return the line number attribute of this record attributes object.
Definition ball_recordattributes.h:599
const bdlt::Datetime & timestamp() const
Return the timestamp attribute of this record attributes object.
Definition ball_recordattributes.h:635
bsls::Types::Uint64 threadID() const
Return the threadID attribute of this record attributes object.
Definition ball_recordattributes.h:617
const char * category() const
Return the category attribute of this record attributes object.
Definition ball_recordattributes.h:587
int processID() const
Return the processID attribute of this record attributes object.
Definition ball_recordattributes.h:605
const char * message() const
const char * fileName() const
Return the filename attribute of this record attributes object.
Definition ball_recordattributes.h:593
ball::UserFields & customFields()
Definition ball_record.h:414
RecordAttributes & fixedFields()
Return the modifiable fixed fields of this log record.
Definition ball_record.h:396
Definition ball_userfields.h:136
int length() const
Return the number of user field values in this object.
Definition ball_userfields.h:406

Now, we defined a function main in which we create a MyOstreamObserver object and assign the address of this object to a ball::ObserverAdapter pointer:

int main(bool verbose)
{
MyOstreamObserver myObserver(&out);
ball::ObserverAdapter *adapter = &myObserver;
Definition bslstl_ostringstream.h:175

Finally, publish three messages by calling publish method accepting a shared-pointer, provided by ball::ObserverAdapter, that in turn will call the publish method defined in MyOstreamObserver:

ball::UserFields customFields;
const int NUM_MESSAGES = 3;
for (int n = 0; n < NUM_MESSAGES; ++n) {
fixedFields,
customFields);
adapter->publish(handle,
n,
NUM_MESSAGES));
}
if (verbose) {
bsl::cout << out.str() << bsl::endl;
}
return 0;
}
void setTimestamp(const bdlt::Datetime &timestamp)
Definition ball_recordattributes.h:580
Definition bdlt_datetime.h:331
void str(const StringType &value)
Definition bslstl_ostringstream.h:581
Definition bslstl_sharedptr.h:1830
void createInplace()
Definition bslstl_sharedptr.h:5448
@ e_TRIGGER
Definition ball_transmission.h:215
static Datetime utc()
Definition bdlt_currenttime.h:296
static Allocator * allocator(Allocator *basicAllocator=0)
Definition bslma_default.h:897

The above code fragments print to stdout like this:

Publish a sequence of three messages.
22FEB2012_00:12:12.000 201 31 0
22FEB2012_00:12:12.000 202 32 0
22FEB2012_00:12:12.000 203 33 0