Simple (error) logger (for games)

Game development specific discussions.
Posts: 2281
Joined: May 24, 2007 22:10
Location: The Netherlands

Simple (error) logger (for games)

Postby badidea » Dec 31, 2018 0:32

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
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
Posts: 556
Joined: Mar 10, 2007 15:44
Location: Ohio, USA

Re: Simple (error) logger (for games)

Postby bcohio2001 » Dec 31, 2018 0:51

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!!!
      FileCnt += 1
      If FileCnt > 1 Then
         CheckIt = NextCheck
         CheckIt = ExePath + "\" + FName + ".txt"
      '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
   LastLog = 0
   If FileExists(CheckIt) Then
      LastLog = FileDateTime(CheckIt)
   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,"-")
   Print #DFile, "At ";
   Print #DFile, Format(Now,"h:mm:ssam/pm");
   Print #DFile, " ";Note
   Close #DFile
End Sub
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Simple (error) logger (for games)

Postby Tourist Trap » Jan 01, 2019 12:49

Hi all,

I did that times ago. Quite complicated I admit:

Return to “Game Dev”

Who is online

Users browsing this forum: No registered users and 5 guests