Click here to Skip to main content
Licence 
First Posted 6 Jan 2002
Views 112,982
Downloads 732
Bookmarked 26 times

Writing trace messages in VB (script) programs

By Xiangyang Liu 刘向阳 | 27 Jan 2002
A cool com component to help you programming in VB (script)

1

2
2 votes, 22.2%
3
4 votes, 44.4%
4
3 votes, 33.3%
5
4.06/5 - 42 votes
μ 4.06, σa 1.37 [?]

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.

History

28 Jan 2002 - updated source files.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Xiangyang Liu 刘向阳



United States United States

Member


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionCannot compile in VC++6.0 PinmemberPorthos dot net16:20 9 Nov '06  
GeneralProblem in installing Pinmembercriant4:32 11 Mar '02  
GeneralRe: Problem in installing PinmemberXiangYangLiu7:41 11 Mar '02  
GeneralProblem in compiling Pinmembercriant7:45 7 Mar '02  
GeneralRe: Problem in compiling PinmemberXiangYangLiu8:22 7 Mar '02  
GeneralUpdate 2002/01/16 PinmemberXiangYangLiu2:52 16 Jan '02  
GeneralCode doesn't compile PinmemberSurya Sonti11:01 15 Jan '02  
GeneralRe: Code doesn't compile PinmemberXiangYangLiu2:51 16 Jan '02  
GeneralUpdate 2002/01/08 PinmemberXiangYangLiu2:07 8 Jan '02  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120210.1 | Last Updated 28 Jan 2002
Article Copyright 2002 by Xiangyang Liu 刘向阳
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid