Including Thread IDs in Trace Output
In
Example 53 the derived client shown in
Example 52 is given the ability to record which thread produced the trace events. The code is taken from one of the Threads package examples,
-buildspace\examples\thread\trace.cpp.
Example 53 – Producing trace output with thread IDs
#include <rw/trace/trace.h>
#include <rw/tools/datetime.h> // for RWDateTime
#include <rw/sync/RWThreadId.h> // for rwThreadId()
...
class TimeStampClientImp : public RWTraceOstreamClientImp {
public:
// The static make function constructs a body, and returns a
// handle to it. Note: the returned handle's type is actually
// the parent class of our handle.
static RWTraceOstreamClient make(std::ostream& ostr = std::cerr) {
return new TimeStampClientImp(ostr);
}
protected:
// Default constructor.
TimeStampClientImp(void) { }
// A constructor that takes an ostream.
TimeStampClientImp(std::ostream& ostr)
: RWTraceOstreamClientImp(ostr) { }
// Override our parent's doTrace member.
virtual void doTrace(const RWTraceEvent& ev) {
RWTimeDate now;
RWCString time( now.asString("%H:%M:%S"));
unsigned threadID = rwThreadHash( rwThreadId() );
{
// Acquire a lock so we can output safely.
RWGUARD(getMutex());
getOstream() << time << "|" << "TID:" << threadID << "|"
<< 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() << std::endl;
}
}
};