Posted: 3rd Oct 2011 0:04
I believe 100% of the people here use print statement on screen and/or file to debug their Basic games.

I've came up with a module that allows you to show the logs on screen, keeping only the 10 most recent messages, add timestamp to the message and optionally save to a log file.

See the comments at the beginning to know what global variables you have to create on your main.agc file.

Here is the log.agc file:

+ Code Snippet
RemStart
**********************************************************
 Log Manager - by Paulo Garcia

 Besides adding this module to your project, declare
 the following glogal variables in your main.agc

    #constant MAX_LOGS = 10
    Dim logs[MAX_LOGS] as String
    Dim textLog[MAX_LOGS]
    Global log_idx = 0
    Global fontLog = 0
**********************************************************
RemEnd

// Constants
#constant INITIAL_Y = 700
#constant FONT_SIZE = 16
#constant FONT_ALPHA = 150
#constant LOG_FILENAME = "log.txt"

// optional features - set 0 to disable, 1 to enable
#constant LOG_FILE = 1

#constant ADD_TIMER = 1

// -----------------------------------------------------------
// Initialize Text objects
// -----------------------------------------------------------
Function InitLogs()
    log_idx = 0

    y = INITIAL_Y

    logFont = LoadImage("img/logfont.png")

    For i = 0 to MAX_LOGS - 1
        logs[i] = ""
        textLog[i] = CreateText("")
        SetTextColor(textLog[i], 255, 0, 0, FONT_ALPHA)
        SetTextFontImage(textLog[i], logFont)
        SetTextVisible(textLog[i], 1)
        SetTextPosition(textLog[i], 0, y)
        SetTextSize(textLog[i], FONT_SIZE)
        SetTextSpacing(textLog[i], -5)
        y = y + FONT_SIZE
    Next i

    If LOG_FILE = 1
        // Create log file
        lf = OpenToWrite(LOG_FILENAME, 0)
        WriteLine(lf, "------------------------------ START -------------------------------------")
        CloseFile(lf)
    EndIf


EndFunction

// -----------------------------------------------------------
// Show log messages - call from update or render
// -----------------------------------------------------------
Function ShowLogs()
    rem Print logs
    For i = 0 to MAX_LOGS - 1
        SetTextString(textLog[i], logs[i])
    Next i
EndFunction

// -----------------------------------------------------------
// Add log message - call whereever is needed to log something
// -----------------------------------------------------------
Function Log(log_string as String)

    If ADD_TIMER = 1
        log_string = "<" + Str(Timer())+ "> " + log_string
    EndIf

    If LOG_FILE = 1
        // Create log file
        lf = OpenToWrite(LOG_FILENAME, 1)
        WriteLine(lf, log_string)
        CloseFile(lf)
    EndIf


    logs[log_idx] = log_string
    log_idx = log_idx + 1
    if log_idx >= MAX_LOGS
        ShiftArray()
    EndIf

EndFunction

// -----------------------------------------------------------
// Shift array - older message will be lost
// -----------------------------------------------------------
Function ShiftArray()
    For x = 0 to MAX_LOGS -2
        logs[x] = logs[x+1]
    Next x
    log_idx = MAX_LOGS - 1
    logs[log_idx] = ""
EndFunction


I hope it will be useful to someone else. Suggestions to improve it is also welcome.

This image shows how the messages are displayed.



(*) I'd forgotten the font file I used. Now it is attached here...

Cheers


Paulo Garcia
Posted: 10th Oct 2011 21:43
Hi folks, I've added this free code to the CodeBase.

In case you don't know (I didn't), under "My account" there is a link called "Code Base" where you can add your contribution on browser for other free resources made by the community. As I write this there are 5 entries for AGK.

Cheers
Posted: 11th Oct 2011 0:29
Thanks for that, I was going to suggest adding to the codebase.