I have an app that I want to distribute to others, and for that reason I have a lot of ‘log’ messages in the code for debugging purposes. However, I’d like to be able to have the user turn logging on or off with a button or menu item on the interface, as I don’t really want to fill up the console with log messages that are not needed unless the app is misbehaving (in short, I want to be able to tell the user to turn logging on if and only if there’s a problem).
Is there a way to suppress the AppleScript ‘log’ messages, or some other way I can turn logging on/off?
TIA
EDIT: it occurs to me that I could set a property and prepend each log statement with a conditional statement based on its setting. But that seems a bit laborious. Any other way?
Sorry to answer my own question, but yes, I’ve sort of half-solved the problem by declaring a property and then doing a search and replace of all the ‘log’ messages with 'if theLogging = “on” then log".
However, that’s raised a new issue. How do I set the logging property programmatically? I want it ‘off’ by default, but I want to be able to do some kind of ‘defaults write com.myapp.app -logging ON’ type message in Terminal.
I just started to look at the developer pages for NSUserDefaultsController and am feeling a bit overwhelmed.
tell application “Finder”
set existsFile to exists item “Macintosh HD:test.jpeg”
end tell
updateLog(“Check file exists”, 1, “Bla Bla bla”, existsFile)
on updateLog(routineName, errorCode, errorMsg, myVar)
if pDebugLog is true then
log {routineName, errorCode, errorMsg, myVar}
end if
end updateLog
You can use the updateLog also to write a txt log file or simply to log in console.
You can trap it in try on error or simply use as described above.
For enable the property just a menu command or capture if app has been launched with command-key or other modifier keys.
That’s more or less the way I did it, using a property and a conditional statement, but with more overhead {
sense deserted me and instead of thinking of putting the if.then clause in a mini-handler I just pre-pended it to each log statement :o
}:
I’ll fix that for aesthetic purposes alone, now you’ve pointed it out.