Simple Macro for Debugging

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
Lothar Schirm
Posts: 375
Joined: Sep 28, 2013 15:08
Location: Germany

Simple Macro for Debugging

Post by Lothar Schirm »

Simple macro for debugging, may be helpful perhaps for somebody to check the vaule of variables in a running program:

Code: Select all

'===============================================================================
' Debug.bi
' Macro for debugging
'===============================================================================

#Include Once "windows.bi"


Dim As Integer Debug_ff


#Macro Debug(v, target)
	'print the actual value of variable v
	'- target = 0: print to console
	'- target = 1: print to messagebox
	'- target = 2: print to logfile
	'Write Debug(v, target) under the appropriate line in your code.
	
	Select Case target
		Case 0, 2
			Debug_ff = FreeFile
			If target = 0 Then Open Cons For Output As #Debug_ff
			If target = 2 Then Open "LogFile.txt" For Append As #Debug_ff 
			Print #Debug_ff, __FILE__& " (" &  __LINE__ & ") "; #v & " = " & v	
			Close #Debug_ff
		Case 1
			MessageBox(0, __FILE__ & " (" &  __LINE__ & ") " & #v & " = " & v, "Debug", MB_SETFOREGROUND)
End Select

#EndMacro
Simple example to test the debugging functions:
Debug(i, 0) will print file name, line numer and actual value of i on the console.
Debug(i, 1) will show the same information on a messagebox (Windows only). The program is stopped until the "Ok" button in the messagebox is clicked.
Debug (i, 2) will save the same information into LogFile.txt. The logfile is created automatically and may be deleted after use "manually"

Code: Select all

#Include "Debug.bi"

Dim As Integer i
Dim As String s(20)

Screenres 400, 400
For i = 0 To 25
  s(i) = Str(i)
  Print s(i)
  Debug(i, 0)	
  Debug(i, 1)
  Debug(i, 2)
Next
  
GetKey

End
Post Reply