Skip to main content
gamedev.net
🔒 Locked

For vb beginners - Debugging module

Started by Nice Coder Dec 14, 2005 at 10:56 PM 0 replies 700+ views
Original Post
Nice Coder
Nice Coder
For all those who have a hard time debugging stuff, look no further! This is very, very simple to use. To start it, in your form_load (or sub_main) type

Debugbuild
For it to load up, but not to save any logs or

Startdebug
For it to load up and save logs. Simply type assert x < 3 or whatever you need. for eg.

x = 4
assert x < 3, "X is greater then three, so this assert fails"
You can also just use assert, and a default warning message is used.

assert x < 3
You can also have "assert warnings", so you can see things that are peculiar, yet not deadly.

assert x < 3, "X is greater then three, but this isn't very critical", true
The final true makes it a warning. There is a third command called "Debuglog"

Debuglog "This is what i want to show up"
There is an optional level attribute that lets you decide how importaint it is. (or where its coming from, for eg. Everything coming from the gui could be level 1, everything from the data loading routines 2, ect.). Note that 9 and 10 are reserved for assert warnings and assert errors. There is also err and warn

'some code
err "You should never get here" 'For example, your looking through a list 
                                'to find something that should be there, but isn't.
If you say err "This could not have happened" it logs it (though debuglog) before exiting. Warn does the same thing. (they are basically asserts which always fail, for situations where you really shouldn't be in). Without further ado, here is the code you need. Just make a new module called Debug.bas and copy it in

'Made by nice coder.
'Code has with no warranty, express or implied.
'Any redistribution of this code must contain this notice

Dim Logfile As String
Dim Errfile As String
Dim Warnfile As String
Dim Fpath As String
Dim Setdebugbuild As Boolean
Dim Savefile As Boolean

Public Sub Debugbuild(Optional re)
If IsMissing(re) Then re = True 'This lets you decide if it is a debug build. Simply say Debugbuild and it'll start.
Setdebugbuild = re
End Sub

Public Sub Startdebug(Optional logf, Optional errf, Optional warnf)
Setdebugbuild = True
Savefile = True

Fpath = App.Path
If Right(Fpath, 1) <> "\" Then Fpath = Fpath & "\" 'Find where we are

If IsMissing(logf) Then
    Logfile = Fpath & "Logfile.log" 'The programmer didn't specify a parameter so use the defult
Else
    Logfile = logf 'Otherwise, use what the programmer asked for
End If

If IsMissing(errf) Then
    Errfile = Fpath & "Errfile.log"
Else
    Errfile = errf
End If

If IsMissing(warnf) Then
    Warnfile = Fpath & "Warnfile.log"
Else
    Warnfile = warnf
End If

Touchfile Logfile 'Make files if there not already there
Touchfile Errfile
Touchfile Warnfile

Debuglog "Debugging started" 'Start the log
End Sub

Private Sub Touchfile(filename)
Dim fileop As Integer
fileop = FreeFile
If Dir(filename) = "" Then 'If the file doesn't exist
    Open filename For Output As fileop 'Make it
    Print #fileop, "File created on " & Now
    Close #fileop
End If

Open filename For Append As fileop 'Add a timestamp for it.
Print #fileop, ""
Print #fileop, "Debugging started on " & Now
Close #fileop

End Sub

Public Sub Assert(condition, Optional errmsg, Optional iswarning As Boolean)
If Not Setdebugbuild Then Exit Sub

If Not condition Then 'Condition failed. Error
    If IsMissing(errmsg) Then errmsg = "Assert failed" 'Generic messages
    If Trim(errmsg) = "" Then errmsg = "Assert failed"
    
    If iswarning Then 'Check if its an "assert warning"
        Debuglog errmsg, 9
    Else
        Debuglog errmsg, 10
        End
    End If
End If
End Sub

Public Sub Err(msg) 'Simple functions for ease of use
If Not Setdebugbuild Then Exit Sub
Debuglog msg, 10
End
End Sub
Public Sub Warn(msg)
Debuglog msg, 9
End Sub

Public Sub Debuglog(msg, Optional lvl)
If Not Setdebugbuild Then Exit Sub

Dim newfile As Integer
newfile = FreeFile

If IsMissing(lvl) Then 'If you don't have a level, then you simply make it an "Attention:" statement
    Debug.Print "Attention :" & msg 'Tell the user
    If Not Savefile Then Exit Sub
    Open Logfile For Append As #newfile 'Write it to the logfile
    Print #newfile, "Attention :" & msg
    Close #newfile
Else
    Select Case lvl
    Case Is < 9 Or lvl > 10 'Its not an assert warning or error.
        If Not Savefile Then Exit Sub
        Debug.Print "Error level " & lvl & " :" & msg 'Tell the user
        Open Logfile For Append As #newfile
        Print #newfile, "Error level " & lvl & " :" & msg
        Close #newfile
    Case Is = 9 'Its an assert warning
        Debug.Print "Warning :" & msg
        If Not Savefile Then Exit Sub
        Open Warnfile For Append As #newfile
        Print #newfile, "Warning :" & msg
        Close #newfile
    Case Is = 10 'Its an assert error
        Debug.Print "Critical Error :" & msg
        If Not Savefile Then Exit Sub
        Open Errfile For Append As #newfile
        Print #newfile, "Critical Error :" & msg
        Close #newfile
    End Select
End If
End Sub

If anybody's interested in this one, i'll add some extra features (like outputting a html logfile, or templated logsheets, or allowing the program to query itself for errors, ect.) From, Nice coder
Click here to patch the mozilla IDN exploit, or click Here then type in Network.enableidn and set its value to false. Restart the browser for the patches to work.
Nice Coder
Nice Coder
New version out. It now produces a html output file.

Screenshot
Screenie from firefox

The new code:

'Made by nice coder.'All rights reserved.'Use code without any warentee express or implied.'Any redistribution of this code must contain this notice.Dim Logfile As StringDim Fpath As StringDim Setdebugbuild As BooleanDim Savefile As BooleanDim programon As BooleanPublic Sub Debugbuild(Optional re)If IsMissing(re) Then re = True 'This lets you decide if it is a debug build. Simply say Debugbuild and it'll start.Setdebugbuild = reEnd SubPublic Sub Startdebug(Optional logf, Optional errf, Optional warnf)Setdebugbuild = TrueSavefile = TrueFpath = App.PathIf Right(Fpath, 1) <> "\" Then Fpath = Fpath & "\" 'Find where we areIf IsMissing(logf) Then    Logfile = Fpath & "Logfile.html" 'The programmer didn't specify a parameter so use the defultElse    Logfile = logf 'Otherwise, use what the programmer asked forEnd IfTouchfile Logfile 'Make files if there not already thereIf Not programon Then Debuglog "Program started on " & NowDebuglog "Debugging started" 'Start the logEnd SubPrivate Sub Touchfile(filename)Dim fileop As Integerfileop = FreeFileIf Dir(filename) = "" Then 'If the file doesn't exist    Open filename For Output As fileop 'Make it    Print #fileop, "<html><head><title>This is " & filename & "</title></head><body bgcolor='#000000' text='#FFFFFF'><center><font size='6'>This is a logfile made for project X for debugging purposes</font>

<font size='5'>" Close #fileopEnd IfOpen filename For Append As fileop 'Add a timestamp for it.Print #fileop, ""Print #fileop, "<h2>Debugging started on</h2> " & Now & "


"Close #fileopEnd SubPublic Sub Assert(condition, Optional errmsg, Optional iswarning As Boolean)If Not Setdebugbuild Then Exit SubIf Not condition Then 'Condition failed. Error If IsMissing(errmsg) Then errmsg = "Assert failed" 'Generic messages If Trim(errmsg) = "" Then errmsg = "Assert failed" If iswarning Then 'Check if its an "assert warning" Debuglog errmsg, 9 Else Debuglog errmsg, 10 End End IfEnd IfEnd SubPublic Sub Err(msg) 'Simple functions for ease of useIf Not Setdebugbuild Then Exit SubDebuglog msg, 10EndEnd SubPublic Sub Warn(msg)Debuglog msg, 9End SubPublic Sub Debuglog(msg, Optional lvl)If Not Setdebugbuild Then Exit SubDim newfile As Integernewfile = FreeFileIf IsMissing(lvl) Then 'If you don't have a level, then you simply make it an "Attention:" statement Debug.Print "Attention :" & msg 'Tell the user If Not Savefile Then Exit Sub Open Logfile For Append As #newfile 'Write it to the logfile Print #newfile, "<font color=""#00CCFF"">Attention :" & msg & "</font>

" Close #newfileElse If (lvl < 9 Or lvl > 10) Then 'Its not an assert warning or error. If Not Savefile Then Exit Sub Debug.Print "Error level " & lvl & " :" & msg 'Tell the user Open Logfile For Append As #newfile Print #newfile, "<font color='#FFCC66'>Error level " & lvl & ": " & msg & "</font>

" Close #newfile ElseIf lvl = 9 Then 'Its an assert warning Debug.Print "Warning :" & msg If Not Savefile Then Exit Sub Open Logfile For Append As #newfile Print #newfile, "<font color='#FFFF66' size='+3'>Warning: " & msg & "
</font>
" Close #newfile ElseIf lvl = 10 Then 'Its an assert error Debug.Print "Critical Error :" & msg If Not Savefile Then Exit Sub Open Logfile For Append As #newfile Print #newfile, "<font size='+5' color='#FF6600'><blink>Critical Error:</blink> " & msg & "</font>

" Close #newfile End IfEnd IfEnd Sub



[Edited by - Nice Coder on December 15, 2005 4:55:02 AM]
Click here to patch the mozilla IDN exploit, or click Here then type in Network.enableidn and set its value to false. Restart the browser for the patches to work.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.