Using RWTraceLevelFilter
The
RWTraceLevelFilter filter is used to dynamically filter trace events at runtime. The
setLevel() function can be used by your program to reset the filter’s cut-off level at any time during your program’s execution. This filter and two other ways of filtering trace events are compared in
Controlling Trace Output.Example 54 shows how to use the
RWTraceLevelFilter class.
Example 54 – Implementing dynamic filtering of trace events
#define RW_USER_TRACE_LEVEL 8 //1
#include <rw/trace/trace.h>
int main()
{
// create trace client that logs messages to cerr
RWTraceOstreamClient myTraceClient(cerr); //2
// create filter that only lets info and more important events
// pass through
RWTraceLevelFilter myTraceLevelFilter(RW_TRACE_LEVEL_INFO); //3
// connect client to level filter
myTraceClient.connect(myTraceLevelFilter); //4
// connect level filter to manager
myTraceLevelFilter.connectToManager(); //5
RW_USER_TRACEABLE_FUNCTION("main”); //6
// generate some trace information
RW_USER_TRACE_INFO("This info level message should be seen.");
RW_USER_TRACE_WARNING("This warning level message should be seen.");
RW_USER_TRACE_ERROR("This error level message should be seen.");
// change the level
myTraceLevelFilter.setLevel(RW_TRACE_LEVEL_ERROR); //7
// generate some more trace information
RW_USER_TRACE_INFO("This info level message should not be seen.");
RW_USER_TRACE_WARNING("This warning level message should”
"not be seen.");
RW_USER_TRACE_ERROR("This error level message should be seen.");
return 0;
}
The symbolic constants referred to in line //3 are defined in the file rw/trace/RWTraceEventSeverity.h. They cannot be used when setting the RW_USER_TRACE_LEVEL macro, because this macro needs to be set before including any trace header files.
The constants are: RW_TRACE_LEVEL_FATAL, RW_TRACE_LEVEL_ERROR, RW_TRACE_LEVEL_WARNING, RW_TRACE_LEVEL_INFO, RW_TRACE_LEVEL_TEST, RW_TRACE_LEVEL_DEBUG, RW_TRACE_LEVEL_ENTRY and RW_TRACE_LEVEL_NONE. They all map directly to the appropriate severity level. RW_TRACE_LEVEL_NONE filters out all trace messages. It is equivalent to zero.