![]() |
Languages »
VBScript »
General
Intermediate
Writing trace messages in VB (script) programsBy Xiangyang Liu 刘向阳A cool com component to help you programming in VB (script) |
VBScript, VC6.NET 1.0, Win2K, WinXP, Dev
|
|
Advanced Search |
|
|
|
||||||||||||||||
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.
' 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
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.
28 Jan 2002 - updated source files.
| You must Sign In to use this message board. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 27 Jan 2002 Editor: Chris Maunder |
Copyright 2002 by Xiangyang Liu 刘向阳 Everything else Copyright © CodeProject, 1999-2009 Web19 | Advertise on the Code Project |