|
-
Feb 19th, 2007, 03:50 PM
#1
[RESOLVED] [2005] Access to current sub/function name?
In debugging a recent program I found it useful to add a line at the top of every function and sub such as;
VB Code:
Private Sub MySub
If DebugMode Then WriteToLog("MySub")
By scanning down the log I can see the sequence of functions/subs that are running. So I can check that things are happening in the right order, no multiple loops, events are firing etc. Is this something I can already do in the IDE?, I can't see anything like this in the menus.
Also, is there a string function or something that returns the name of the current sub/function? So I could do something like;
VB Code:
Private Sub MySub
If DebugMode Then WriteToLog(NiftyFunction.CurrentSubFunction)
instead of having to add the debug lines mentioned individually?
-
Feb 19th, 2007, 05:23 PM
#2
Re: [2005] Access to current sub/function name?
When you hit a break point you can look at the Call Stack window, but it will only go back as far as the most recent Windows message that prompted an action.
As for your second question, look at the StackTrace class. While you're there, explore the System.Diagnostics namespace.
-
Feb 19th, 2007, 05:25 PM
#3
Re: [2005] Access to current sub/function name?
Also, don't use code like this:
VB Code:
If DebugMode Then WriteToLog("MySub")
If you're not going to use the Debug class itself, or the Trace class, then use conditional compilation so the code won't even exist in a Release build:
VB Code:
#If DEBUG
WriteToLog("MySub")
#End If
-
Feb 19th, 2007, 06:56 PM
#4
Re: [2005] Access to current sub/function name?
Ah, ok thanks. So for posterity, this is what I ended up with...
VB Code:
Public Class Form1
#If DEBUG Then
Dim LogFile As String = Application.StartupPath & "\" & "LogFile.txt"
Public Sub WriteStackTrace()
Dim StackTrace As New StackTrace(True)
Dim sw As System.IO.StreamWriter
Dim str As String()
'Create or Append
sw= New System.IO.StreamWriter(LogFile, System.IO.File.Exists(LogFile))
str = StackTrace.GetFrame(1).ToString.Split(" ")
'Strip off the function/sub name (there is more info than just that available)
sw.WriteLine(Date.Now.TimeOfDay.ToString & " " & str(0))
sw.Close()
End Sub
#End If
'The load event
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
#If DEBUG Then
'Delete an existing log file
If System.IO.File.Exists(LogFile) Then System.IO.File.Delete(LogFile)
WriteStackTrace()
#End If
'The program
Me.Close()
End Sub
In each sub/function put;
VB Code:
#If DEBUG Then
WriteStackTrace()
#End If
Which will write out a logfile showing the current time and the name of the function/sub running. Since the code added to each function/sub is always the same, this can be easily edited into an existing program. I tested this and it works, but if my code is no good then please comment.
An enhancement would be to subtract the times, then you can see how long each sub takes.
Last edited by Bulldog; Feb 19th, 2007 at 07:06 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|