Logging messages
While talking with Emily about an annoyingly persistent bug and the problem of forgetting where you put the debugging statements, thus forgetting to remove them: I remembered that I posted this thread some years ago in a different galaxy. Still seems like a useful tweak, so I hope it comes in handy.
Posting messages into the messagebox is a time-tested method of logging what your code is doing. At some point, though, finding the lines of code that post status messages and remembering to remove them or comment them out later gets old. Instead, I have a logging handler that relies on a property I set in a common script (the stack script, a frontscript, backscript, etc) and that I can toggle true or false (along with a getter function for the property so that’s it’s globally available). That way I only log messages to the messagebox if the property is true, and I don’t have to disable the dbgLog messages.
In common script:
constant kDebugging? = true
function isDebugging?
return kDebugging?
end isDebugging?
command dbgLog pMessage
if isDebugging()? then
put pMessage & cr after msg
# answer pMessage # as an alternate
end if
end dbgLog
In stack under test:
dispatch “dbgLog” with param(0) && “I’m about to do something here”
Note that param(0) gives you the currently executing handler, and is very useful for logging debug messages. The next level of utility is the executionContexts, which gives you a deep dive into how you got to this point in your code (aka the “stack frame”).
dispatch “dbgLog” with param(0) & cr & the executioncontexts