   For vb beginners - Debugging module
  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) typeDebugbuildFor it to load up, but not to save any logsorStartdebugFor it to load up and save logs.Simply type assert x < 3 or whatever you need.for eg.x = 4assert 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 < 3You 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", trueThe 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 codeerr "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 noticeDim Logfile As StringDim Errfile As StringDim Warnfile As StringDim Fpath As StringDim Setdebugbuild As BooleanDim Savefile 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.log" 'The programmer didn't specify a parameter so use the defultElse    Logfile = logf 'Otherwise, use what the programmer asked forEnd IfIf IsMissing(errf) Then    Errfile = Fpath & "Errfile.log"Else    Errfile = errfEnd IfIf IsMissing(warnf) Then    Warnfile = Fpath & "Warnfile.log"Else    Warnfile = warnfEnd IfTouchfile Logfile 'Make files if there not already thereTouchfile ErrfileTouchfile WarnfileDebuglog "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, "File created on " & Now    Close #fileopEnd IfOpen filename For Append As fileop 'Add a timestamp for it.Print #fileop, ""Print #fileop, "Debugging started on " & NowClose #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, "Attention :" & msg    Close #newfileElse    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 SelectEnd IfEnd SubIf 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   
  New version out. It now produces a html output file.Screenie from firefoxThe 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'><b><i><center><font size='6'>This is a logfile made for project X for debugging purposes</b></i></font><p><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 & "<p><hr>"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""><b>Attention</b> :" & msg & "</font><br><hr>"    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'><b>Error level</b> <i>" & lvl & "</i>: " & msg & "</font><br><hr>"   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'><b>Warning</b>: " & msg & "<br></font><hr>"   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, "<b><font size='+5' color='#FF6600'><blink>Critical Error:</b></blink> " & msg & "</font><br><hr>"   Close #newfile    End IfEnd IfEnd Sub [Edited by - Nice Coder on December 15, 2005 4:55:02 AM]