In my previous article "Writing
trace messages in multi-threaded C++ applications", I provided source
code for a utility that can do dynamic tracing in multi-threaded programs.
The idea can be used not just in debugging, but also in production to track
down hard-to-detect problems. The utility provides three simple functions.
The first function, SetTraceFilePrefix,
decides where to store and how to name the trace file. The second
one, SetTraceLevel, can be
called at run-time to dynamically changing the current level of tracing.
The last one, WriteTrace,
writes easily formatted (similar to printf)
trace messages to the current trace file. Please refer to the article
and source code for details.
This article aims at providing similar functionality to VB and script
programs. At first you may ask: why do we want to write trace messages
to a file in a VB program? Remember that VB script is used in many
web-server programs (ASP pages, etc.) which are multi-threaded. Even
for GUI client programs, the ability to dynamically generate trace messages
is a nice feature to have. For example, we can set the trace level
to 0 so that no tracing is
done when everything is ok and increase the trace level through a menu
command or a button to get detailed debug information when there is a problem.
For VB (and script) programmers, I implemented a com component that
wraps the three functions in the above C++ utility. Here is a sample
VB script code that uses this com component.
dim objTrace
set objTrace = CreateObject("XYTraceObj.1")
dim myInt = -2
objTrace.SetTraceFilePrefix "c:\temp\VBLog"
objTrace.SetTraceLevel 10
objTrace.WriteTrace 10, "The string: %s", "Hello, world"
objTrace.WriteTrace 20, "The int: %d", 300
objTrace.WriteTrace 30, "Something else: %d, %s, %04d", 200, "Hey", myInt
objTrace.SetTraceLevel 20
objTrace.WriteTrace 10, "The string: %s", "Hello, world"
objTrace.WriteTrace 20, "The int: %d", 300
objTrace.WriteTrace 30, "Something else: %d, %s, %04d", 200, "Hey", myInt
objTrace.SetTraceLevel 30
objTrace.WriteTrace 10, "The string: %s", "Hello, world"
objTrace.WriteTrace 20, "The int: %d", 300
objTrace.WriteTrace 30, "Something else: %d, %s, %04d", 200, "Hey", myInt
objTrace.SetTraceLevel 0
objTrace.WriteTrace 10, "Why am I not written? Because tracing is disabled!"
set objTrace = nothing
Here is the trace file for the above program. The string 06:57:30_843_1EB
in the trace message is the timestamp (down to milliseconds) and the hex
thread id.
06:57:30_843_1EB: The string: Hello, world
06:57:30_843_1EB: The string: Hello, world
06:57:30_843_1EB: The int: 300
06:57:30_843_1EB: The string: Hello, world
06:57:30_843_1EB: The int: 300
06:57:30_843_1EB: Something else: 200, Hey, -002
Note that even if you are calling the SetTraceFilePrefix
method and the SetTraceLevel
method using different objects, the whole process will be affected.
A limitation to this com component, unlike its C++ counter-part,
is that you can only provide string or integer arguments to the WriteTrace
method and you can use at most 8
optional arguments in addition to the trace level and the format string.
The zip file in this article contains source code for the com component.
The source code for the most recent version of the C++ utility can be obtained from my
previous artcle. Thank you for reading this article and my
other articles.
History
28 Jan 2002 - updated source files.