Xtalk debugging ideas

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

1 Like

i do something similar to what you are doing, but i also have taken to marking temporary debug lines with a comment, like #xxx, when i don’t feel like putting my app into full debug mode, which, in my case, fires off debug statements all over the place, usually launching the remote debugger.
if you use levure, there is a logging library built in, which can target different output channels, which is useful if you deploy to mobile, where there is no message box.
i also have two buttons that i include in a common background in every project, “query”, and “exec”. “query” sends a query to the database engine and answers the reply. “exec” brings up an ask dialog that i can type a command into. it then does it. i flip a flag in the stack script to show or hide those buttons.