MACROs: "Write To Log File", etc.
UPDATED 2016-12-28: Version1.1. now includes two "ET" versions, which write elapsed time to the log. Also includes two macros to trim comment files to a maximum number of lines. See the comments below.
To install, delete the old macros and import these. Any macros that call the old versions will continue to work without needing any changes.
Write To Log File.v1.1.kmmacros (181.6 KB)
Versions without Elapsed Time (quicker):
Versions with Elapsed Time (slower):
These four macros allow you to easily write messages to log files.
- The log messages are written with timestamps.
- The log files are plain text files, and you specify the file path.
- The macros are protected with Semaphores, so you can have multiple macros writing to them at the same time, without any worry of something going wrong.
They end up looking something like this (non-ET versions):
2016-12-28 08:42:56.118 This is test 1. 2016-12-28 08:42:56.182 This is test 2. 2016-12-28 08:42:56.245 This is test 3.
2016-12-28 08:43:57.124 1:00.879 This is test 4. 2016-12-28 08:43:57.731 607 This is test 5. 2016-12-28 08:43:57.985 254 This is test 6.
Why Inverted/Non-Inverted Versions?
Normally, people expect to read a log file from the top down, with the newest messages at the end. With the "Inverted" version, the newest messages are at the beginning.
Here's why: If you open a log file using an application like BBEdit or TextWrangler, they have an option that automatically re-reads the file when its content changes. So everytime you write a new message to the file, it will suddenly appear in the editor (it might take a second or two for the editor to notice the file changed).
But if the new message is at the end of the file, the editor doesn't scroll to show the new message - it stays at the beginning of the file.
So, if you use the "Inverted" version, new messages are written to the beginning of the file, and they magically appear at the top of the editor.
"ET" (Elapsed Time) Versions:
The versions with "ET" in the name write the elapsed time between messages to the file. The reason there are versions without this, is because the "ET" versions are slower.
On my machine, the non-ET versions average about 75ms. The ET versions average about 250ms.
All macros are used in exactly the same way:
You use an "Execute a Macro" action, executing one of the two macros. Right-click on the action (or click the "gear" icon), and select the "with parameter" option.
The parameter is in this format:
- logfilepath must be the path of the log file, like "~/Documents/TestLog.txt". You can use the the contents of a variable if you want, of course.
|is the vertical bar character
message is the message. It can be blank, although that's not of much use. But even if it is blank, you must still have the
~/Documents/TestLog.txt|This is a log message.
%Variable%LogFilePath%|This is a log message.
Two other macros are included, which you can use to trim a log file to a maximum number of lines:
It's clear to me from comments that people have made, that the benefit of using these macros is not immediately obvious. So in the hopefully near future, I plan on creating a video to demonstrate some of the ways these macros can be used.