Example 7: Customizing the Trace Message Prefix

Each trace message begins with a trace message prefix composed of data about the trace message. The format of the trace message prefix is stored in the %TRACE_FORMAT_PREFIX% environment variable. By changing the value of the environment variable, you can customize the trace message prefix to display the data you need about the trace message in the format that is most useful to you. The variables in the default trace message prefix, and all variables that you can use in a trace message prefix are described in the Trace Message Prefix topic.

The following display shows the default trace message prefix. The trace messages were generated by Tracedrv, the trace-enabled sample driver in the Windows Driver Kit (WDK).

[0]0AF4.0C64::07/25/2003-14:55:39.998 [tracedrv]IOCTL = 1
[0]0AF4.0C64::07/25/2003-14:55:39.998 [tracedrv]Hello, 1 Hi
[0]0AF4.0C64::07/25/2003-14:55:39.998 [tracedrv]Hello, 2 Hi
...

The format of the default prefix is as follows.

[%9!d!]%8!04X!.%3!04X!::%4!s! [%1!s!]

which represents the following data:

[CPUNumber]ProcessID.ThreadID::SystemTime [MessageGUIDFriendlyName]

where the MessageGUIDFriendlyName is, by default, the name of the directory in which the trace provider was built.

To create a new trace message prefix, use the set command to reset the value of the %TRACE_FORMAT_PREFIX% environment variable. For example,

set TRACE_FORMAT_PREFIX=%2!s!: %!FUNC!: %8!04x!.%3!04x!: %4!s!:

This command sets the trace message prefix following format:

SourceFile_LineNumber: FunctionName: ProcessID.ThreadID: SystemTime 

As a result, Tracefmt output uses the new trace message prefix, as shown in the following display:

tracedrv_c258: TracedrvDispatchDeviceControl: 0af4.0c64: 07/25/2003-13:55:39.998:  IOCTL = 1
tracedrv_c264: TracedrvDispatchDeviceControl: 0af4.0c64: 07/25/2003-13:55:39.998:  Hello, 1 Hi
tracedrv_c264: TracedrvDispatchDeviceControl: 0af4.0c64: 07/25/2003-13:55:39.998:  Hello, 2 Hi
tracedrv_c264: TracedrvDispatchDeviceControl: 0af4.0c64: 07/25/2003-13:55:39.998:  Hello, 3 Hi

...

Note   If you are setting the trace prefix in a command or batch file, where the percent symbol represents a variable for a command-line parameter, use two consecutive percent symbols for the prefix variables. For example, to include the system time in the prefix, type %%4.