|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
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, 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 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. ' first, create the com object
' you can use one or more such objects within a single process
dim objTrace
set objTrace = CreateObject("XYTraceObj.1")
dim myInt = -2
' then set the trace file prefix
' this method needs only to be called once within a process
' the trace file will be created in directory "c:\temp" with name prefix "VBLog"
objTrace.SetTraceFilePrefix "c:\temp\VBLog"
' set the current trace level to 10
' this method affect the whole process
objTrace.SetTraceLevel 10
' write trace messages to the trace file
' the first parameter of WriteTrace is the intended trace level
' the second parameter of WriteTrace is a format string as in the C function printf
' only the first message will be written because the current trace level is 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
' set the current trace level to 20
objTrace.SetTraceLevel 20
' write trace messages to the trace file
' only the first two messages will be written because the current trace level is 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
' set the current trace level to 30
objTrace.SetTraceLevel 30
' write trace messages to the trace file
' all three messages will be written because the current trace level is 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
' disable tracing by setting the trace level to 0
objTrace.SetTraceLevel 0
' this message will not be written
objTrace.WriteTrace 10, "Why am I not written? Because tracing is disabled!"
' set objTrace to nothing
set objTrace = nothing
Here is the trace file for the above program. The string 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 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. History28 Jan 2002 - updated source files.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||