This is a log entry generator I’ve been using for a while. Well, more truthfully, the latest in a long evolution of this script.
The premise is to allow logging of key events to a special file I use for tracking my automated scripts. Also needs to be human-readable so I like having indent levels. I also use a variant of this script to simply to debugging logs (by changing the log file name, for example). It’s just a kind of “generic file write with tab idents.”
I’m looking to clean-up some of my automation scripts and use loadable libraries to standardize them, so figured I’d consult the wizards here for tips.
Ideally it should check for the existance of a pre-existing log “g_log_file_path” and use it, else create a new one. It should also cleanly open and close the log file so ScriptDebugger doesn’t have a hissyfit every time I run a script. writeEntry has been done separately for flexibility. Not necessary, but I’ve found it handy now and again.
My only request is I find too much “one lining” (Nigel…heheh) of scripts to make them tough to change or debug in a hurry, since AppleScripting isn’t my only hat here at work. So unless one-lining really has some “worth it” gains, I prefer “the long way” under most circumstances.
So, streamline away!
For reference, “g_debug” is used to allow me to leave in key debugging lines in some scripts but turn off the actually logging of those lines in the finished products.
on logMe(log_string, indent_level)
if g_debug is "true" then --allows turning the debugger on and off so logMe's can be left in final version
try
writeEntry(log_string, indent_level, g_log_file_path)
on error
tell application "Finder"
make new file at g_desktop_folder_path with properties {name:g_log_file_name & ".txt"}
end tell
writeEntry(log_string, indent_level, g_log_file_path)
end try
end if
end logMe
on writeEntry(log_string, indent_level, log_file_path)
repeat indent_level times
write tab to (g_log_file_path as alias) starting at eof
end repeat
write (log_string & return) to (g_log_file_path as alias) starting at eof
end writeEntry