MACROs: "Write To Log File", etc., **Updated 2016-12-28

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):

or


Versions with Elapsed Time (slower):

or

PURPOSE:

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.

ET-versions:

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.

USAGE:

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|message

  • 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 | character.

Some examples:

  • ~/Documents/TestLog.txt|This is a log message.
  • %Variable%LogFilePath%|This is a log message.
  • %Variable%LogFilePath%|%Variable%MyLogMessage%

Additional Macros:

Two other macros are included, which you can use to trim a log file to a maximum number of lines:

and

FUTURE THINGS

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.

Stay tuned.

6 Likes

Seriously? Only one person has downloaded these macros? I must not have explained their usefulness very well. I use them a lot!

Anyone care to comment on why these macros don’t appeal to you? You won’t hurt my feelings - I just want to know. Thanks!

Dan, they are great macros, but have, IMO, limited appeal and limited use cases. Probably appeal mostly to developer types, and there are only a few of those on this board. :wink:

Don't let it worry you. Publish what you think will be useful, and go on to the next project.
As they say: "You can lead a horse to water, but you can't make him drink". LOL

Personally, I don't have a need for them right now.
I do have a couple of KM Macros that I turn on logging occasionally for debug purposes, but I have found it is easiest to just use a KM "DND" variable for this purpose.

Here is an example of this:

Simple, but very effective and easy to use.

That helps, thanks.

How do you tell how many people have downloaded your macros?

There’s a number next to the link. Can you see it? I think everyone can see it…

Normally I would just sit in a Terminal window with tail -f Engine.log (or open the Engine.log file in Console) and use the Log action.

1 Like

Updated to version 1.1. Now includes ET (Elapsed Time) versions. See the first post for more info.

Coming soon: A video demonstration of the benefits of these macros.

1 Like

If I have understood well, this macro can be used to write logs while debugging a macro ?

I'm not Dan, but Dan's system is simply used by inserting an "Execute Macro" Action wherever you want to write to the log. So, yes, it will work in KM debug mode just like any other Action.

@DanThomas, I just want to add that I think that these macros are great, and I so appreciate that you created and shared these!

Just as a quick question—if I had wanted to write to a text file that had a space in its path or its filename, would I need to escape that character? Or does your macro automagically work with paths or filenames even if they might have spaces?

I would think it would handle them without escaping them. I'm usually pretty good about thinking about those kinds of things. But you'd have to try it to make sure.

2 posts were merged into an existing topic: How To Solve KM Problems Using the KM Debugger