Including a Timestamp in Trace Output
Example 52 provides an implementation for the pure-virtual function doTrace() in a class derived from RWTraceEventClientImp. The code is taken from buildspace\examples\trace\example4.cpp. In this example, the class derives from RWTraceOstreamClientImp, which already has most of the necessary behavior. Only the doTrace() function is modified to provide timestamping.
Example 52 – Producing trace output with a timestamp
#include <rw/tools/datetime.h>
. . .
// Override our parent's doTrace member.
void
TimeStampClientImp::doTrace(RWTraceEvent& ev)
{
RWTimeTime now;
RWCString time( now.asString("%H:%M:%S"));
{
// Acquire a lock so we can output safely.
RWGUARD(getMutex());
 
getOstream() << time << "|" << ev.getFileName() << "|"
<< ev.getLineNumber() << "|"
<< ev.getResourceId() << "|";
 
// If an object generated this event, output its address.
if (ev.getThisPtr()) getOstream() << ev.getThisPtr() << "|";
 
getOstream() << ev.getSeverity() << "> " << ev.getMessage()
<< endl;
}
}
};