Original Post
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 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
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
