Example
Logging is easy to setup - use CreateLoggingContext to create a context for logging. Optionally use AddChildLoggingContext for a log that writes to several files at the same time. Use LogMsg to record an event.
This example shows how an error log can write to both the debug log and the verbose log, and how the debug log can write to the verbose log.
Program
Uses
hlog,
Cobra2d,
keyset
Function LoadSafeSprite(fileName: String) : Element
Var
ok: Boolean = TRUE
Begin
LogMsg(debugLog, "LoadSafeSprite: '"+fileName+"'")
IndentLog(debugLog)
If (FileExists(fileName) = FALSE) then
LogMsg(errorLog, "LoadSafeSprite: '"+fileName+"' not found")
ok = FALSE
Else
Result = LoadSprite(fileName)
If Result = NULL then
LogMsg(errorLog, "LoadSafeSprite: '"+fileName+"' was found, but could not be loaded")
ok = FALSE
Endif
Endif
If ok = FALSE then
Result = CreateSprite(32, 32)
CLS(ToRGBA(255,0,0), Result)
If Result = NULL then LogMsg(errorLog, "LoadSafeSprite: '"+fileName+"' fall-back sprite could not be created!")
Endif
IndentLog(debugLog, -1)
End
Var
verboseLog: ^loggingContexts
debugLog: ^loggingContexts
errorLog: ^loggingContexts
anImage: Element
x: Integer
xdir: Integer = 5
Begin
OpenScreen(320,240,32,FALSE,COB_SHOWBORDER+COB_SHOWCLOSE)
verboseLog = CreateLoggingContext("verbose.Log", FALSE)
debugLog = CreateLoggingContext("debug.log", TRUE)
errorLog = CreateLoggingContext("error.log", TRUE)
AddChildLoggingContext(errorLog, debugLog)
AddChildLoggingContext(debugLog, verboseLog)
anImage = LoadSafeSprite("foo.png")
LogMsg(verboseLog, "MainLoop: Start")
IndentLog(verboseLog)
While Not ExitRequested
PositionSprite(anImage, x, 10)
If x + SpriteWidth(anImage) > 320 then
xdir = -5
LogMsg(verboseLog, "MainLoop: Sprite hit the right border")
Endif
If x < 0 then
xdir = 5
LogMsg(verboseLog, "MainLoop: Sprite hit the left border")
Endif
x = x + xdir
Pause(1)
Flip
If KeyDown(VK_ESCAPE) then RequestExit
Wend
IndentLog(verboseLog, -1)
LogMsg(verboseLog, "MainLoop: End")
FreeAllLoggingContexts()
End
error.log
311 LoadSafeSprite: 'foo.png' not found
debug.log
310 LoadSafeSprite: 'foo.png'
312 LoadSafeSprite: 'foo.png' not found
verbose.log
311 LoadSafeSprite: 'foo.png'
313 LoadSafeSprite: 'foo.png' not found
313 MainLoop: Start
1208 MainLoop: Sprite hit the right border
2130 MainLoop: Sprite hit the left border
3052 MainLoop: Sprite hit the right border
3974 MainLoop: Sprite hit the left border
4896 MainLoop: Sprite hit the right border
5818 MainLoop: Sprite hit the left border
6474 MainLoop: End