MACRO: Queued Logging to BBEdit (with Multiple Tab Support)

MACRO: Queued Logging to BBEdit (with Multiple Tab Support)

REQUIRES:

PURPOSE:

Allows you to send log messages to BBEdit, using a message queue so the caller doesn't have to wait for BBEdit to finish adding the message(s).

image

 
NOTE: You can also call it using "Execute a Macro", and passing the arguments in the "with parameter" field. See the coments at the start of the macro for more information.

FEATURES:

You can target multiple BBEdit documents (which I call "Tabs"):

image

 
NOTE: Even though the tab names look like file names, there's no actual files on disk unless you save them.

And of course you can take advantage of BBEdit's color-coding, by specifying the appropriate file extension like this:

image

VERSION HISTORY

1.0 - Initial Release

INSTALLATION

Click to expand

UPGRADE INSTRUCTIONS:

  1. Delete the old macros, or just delete the entire group.
  2. Install the new macros.
  3. There should be no breaking changes.

BBEdit Queued Logging.v1.0.kmmacros.zip (8.8 KB)

Download the macros, unzip them, and double-click the .kmmacros file. It will import these macros:

  • [BBEdit Queued Logging] Log - The main macro
  • [BBEdit Queued Logging] Queue Handler (internal use)
  • [BBEdit Queued Logging] Queue Watcher (internal use)
  • Example - Shows how to use the main macro

4 Likes

What's the advantages of this over, say, a single semaphored async macro that does the writing? I'm guessing one would be that there's less chance of hitting the maximum concurrent macros limit if you sent a flurry of messages and BBEdit couldn't keep up.

Example logger, lacking some of your fancy bits but with a delay in the AppleScript to spoof BBEdit dragging a bit:

Message Writer (Macro).kmmacros (2.6 KB)

Image

Example caller, sending 10 timestamped messages in quick succession and finishing before the first is processed by BBEdit:

Write Test (Macro).kmmacros (4.4 KB)

Image

I started that way, but the idea of possibly reaching a limit did bother me. I can't say it really matters, but still.

And honestly, I wrote this because over the course of my programming jobs, I've always had a queued logger, in whatever programming language I was using a the time, and I wanted to see if I could do it in KM/JXA/AppleScript.

The main advantage is speed, although I can't say how much it matters. But if you run the Example macro, you'll probably see it write 2 messages at a time most of the time. That's because when the Queue Watcher retrieves the next message(s) to write, it combines consecutive queued messages to the same tab into one BBEdit write.

I never did a comparison, though, so I could be wrong.

But come on - this is cool! :joy:

1 Like

Both excellent reasons. I'm afraid my logging tops out at spawning "Display text"s all over the screen...

And that, of course, is the best reason of all!

1 Like

:smile:

By the way, I've been using chatGPT to figure out how do manipulate BBEdit this way. And I just realized it works if I ask for jxa instead of applescript.

chatGPRT is friggin' awesome!

So... a funny thing happened...

I spent some time working on an update to this that is MUCH faster - by an order of magnitude. Great stuff.

Then, out of the blue, I remembered something I wrote back in 2016, which I still use now in macros that I run every day. Although obviously I haven't checked the results in a while or I would have remembered.

It's a set of macros that do logging, including having timestamps and elapsed time! They do it by writing to a file. And guess what? You can open the file in BBEdit, and BBEdit will refresh it when the file changes.

2024-08-03 18:00:02.328	 1:57:29.882	********Starting Macro **************************
2024-08-03 18:00:02.631	         303	Mounting Volume (if needed)
2024-08-03 18:00:06.036	       3.405	Starting Backup
2024-08-03 18:00:06.261	         225	Starting Backup
2024-08-03 18:00:06.557	         296	Enabling "Time Machine Backups - Check for Done", timeout: 1722736806
2024-08-03 18:00:06.805	         248	=========Macro ended without errors==============
2024-08-03 18:01:02.108	      55.303	------------ Checking for Done -------------
2024-08-03 18:01:32.413	      30.305	Backup has finished, unmounting volume
2024-08-03 20:00:02.335	 1:58:29.922	********Starting Macro **************************
2024-08-03 20:00:02.636	         301	Mounting Volume (if needed)
2024-08-03 20:00:06.043	       3.407	Starting Backup
2024-08-03 20:00:06.269	         226	Starting Backup
2024-08-03 20:00:06.578	         309	Enabling "Time Machine Backups - Check for Done", timeout: 1722744006
2024-08-03 20:00:06.838	         260	=========Macro ended without errors==============
2024-08-03 20:01:02.101	      55.263	------------ Checking for Done -------------
2024-08-03 20:01:32.399	      30.298	Backup has finished, unmounting volume
2024-08-03 22:00:02.331	 1:58:29.932	********Starting Macro **************************
2024-08-03 22:00:02.630	         299	Mounting Volume (if needed)
2024-08-03 22:00:06.064	       3.434	Starting Backup
2024-08-03 22:00:06.323	         259	Starting Backup
2024-08-03 22:00:06.651	         328	Enabling "Time Machine Backups - Check for Done", timeout: 1722751206
2024-08-03 22:00:06.881	         230	=========Macro ended without errors==============
2024-08-03 22:01:02.102	      55.221	------------ Checking for Done -------------
2024-08-03 22:01:32.425	      30.323	Backup has finished, unmounting volume
2024-08-03 22:01:34.362	       1.937	-------- Macro ended without errors --------

So it turns out I actually don't need this BBEdit macro - the file version is much better. Sometimes I can be a real moron. Here's the post I made about it on this forum:

I could actually use the "queue" mechanism I wrote to make the "elapsed time" version a little faster, but at this point, all I can do is shake my head.

2 Likes

In case anyone cares, I ended up writing a queued version of the "write to file" logger. It's blazingly fast. I'm still working out a few situations where something crashes, but knock on wood I haven't seen it happen in a while, so maybe it's time to think about releasing it.

Although I suspect nobody really cares about it. Which is fine with me.

1 Like

Please do post it up @DanThomas as I've been mulling over a process that would involve a queue and it might help me to see your approach. Sorry if that sounds selfish but I don't want to reinvent the wheel if I can help it :grinning:

1 Like

No, it doesn't sound selfish at all! The main workhorse portion of it is written in JXA - I can't remember if JXA is your thing or not.

If it isn't, I can explain the logic to see if it might help you. I put a lot of thought into the logic (which doesn't mean it's right, by the way...)

JXA is read-only for me, more or less, so I should be able to understand the logic and being a Brit I'm very familiar with queues :joy:

Well, chances are that my JXA may be over your head - no insult intended, but I'm not capable of doing anything simply. :slight_smile:

With that said, the logic of what the JXA does isn't that complicated. I'll see what I can come up with.

Oh, and having worked at Disneyland for years, I know what queues are ILR too. :joy:

1 Like

Hey, L is pushing in!

1 Like

Please do post it!

2 Likes