Simple (error) logger (for games)

Game development specific discussions.
Post Reply
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Simple (error) logger (for games)

Post by badidea »

Debugging a game can sometimes be difficult. One often cannot use a simple print statement at the location of interest because the screen is overwritten milliseconds later by the graphics stuff. A simple logToFile subroutine has helped me to track what is going on. In combination with a "tail -f logfile.txt" (linux command, don't know the windows equivalent) in a second window, events and error messages can be tracked almost real time (about 1 second delay).
Code with example use:

Code: Select all

const as string LOG_FILE_NAME = "logfile.txt"

sub logToFile(text as string)
	dim as integer fileNum
	fileNum = freefile
	open LOG_FILE_NAME for append as fileNum
	print #fileNum, time & " " & text
	close fileNum
end sub

'------------ test code -------------

sub doSometing()
	dim as integer errorCode = int(rnd*10)
	logToFile("Something went wrong, error: " & str(errorCode))
end sub

dim as double rndSeed = 1234
randomize 1234
logToFile("Game start")
sleep 100, 1
logToFile("Random seed: " & str(rndSeed))
sleep 100, 1
doSometing()
sleep 100, 1
logToFile("Game end")
print "End. See output in: " & LOG_FILE_NAME
This produces:
...
01:20:52 Game start
01:20:53 Random seed: 1234
01:20:53 Something went wrong, error: 2
01:20:53 Game end
...
bcohio2001
Posts: 556
Joined: Mar 10, 2007 15:44
Location: Ohio, USA
Contact:

Re: Simple (error) logger (for games)

Post by bcohio2001 »

Here is mine.
A little more complex, but worth it.

Code: Select all

Sub DebugFile(FName As String, Note As String)
	'if not defined then will do nothing!
	#Ifdef __DebugFileOn__
	Dim As Integer DFile, FileCnt,KB
	Dim As Double LastLog
	Dim As String CheckIt, NextCheck
	'
	KB = 30 'this can be changed any time
	'check size of file, if getting large, make new file (infinite loop or A LOT of errors ????)
	'skip over older files!!!
	Do
		FileCnt += 1
		If FileCnt > 1 Then
			CheckIt = NextCheck
		Else
			CheckIt = ExePath + "\" + FName + ".txt"
		EndIf
		'append # to name
		NextCheck = ExePath + "\" + FName + "-" + Str(FileCnt+1)+ ".txt"
		If FileExists(NextCheck)=0 Then
			'check size of last used file
			If FileLen(CheckIt) > KB * 1024 Then CheckIt = NextCheck
			Exit Do
		EndIf
	Loop
	LastLog = 0
	If FileExists(CheckIt) Then
		LastLog = FileDateTime(CheckIt)
	EndIf
	DFile = FreeFile
	Open CheckIt For Append As #DFile
	If Int(Now) > Int(LastLog) Then
		'new date
		If Lof(DFile) Then Print #DFile, "" 'blank line -- will not add if a "new" file
		Print #DFile, String(10,"-");" ";
		Print #DFile, Format(Now,"mmmm d, yyyy");
		Print #DFile, " ";String(10,"-")
	EndIf
	Print #DFile, "At ";
	Print #DFile, Format(Now,"h:mm:ssam/pm");
	Print #DFile, " ";Note
	Close #DFile
	#EndIf
End Sub
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Simple (error) logger (for games)

Post by Tourist Trap »

Hi all,

I did that times ago. Quite complicated I admit:
viewtopic.php?f=7&t=24116&p=213044&hili ... 2A#p213044
Post Reply