Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New FileHandle for each log write #81

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

evannjohnson
Copy link

@evannjohnson evannjohnson commented Aug 2, 2024

This change covers a niche use case, and should not functionally alter the program in any way. It is a change to how the log file is written to, and how the operations interact with macOS system APIs.

The purpose is to generate FSEvents for each log write, so that writes can trigger a launchd agent that uses WatchPaths to run a program when the TomatoBar.log file is written to. I made this change so I could set up an agent to set the wallpaper color based on the state of TomatoBar, by monitoring the log file and reading the last line when it changes.

Currently, TomatoBar opens a FileHandle and maintains it for the duration of the program, using FileHandle.synchronize() to write to disk. However, this does not generate an FSEvent, and does not trigger the launchd agent. For that, the FileHandle needs to be closed, either with an explicit FileHandle.close(), or by the FileHandle object being deallocated.

I removed the logHandle property of TBLogger, since we will be initializing and deallocating the FileHandle on each write, we don't need to hold onto it. I moved the code for creating a FileHandle out of TBLogger's init() into a separate function, openFileHandle(), which returns the created FileHandle. append() calls openFileHandle(), does the append, and then when the function exits the FileHandle is deallocated, an FSEvent is generated, and the launchd agent (if the user has created one) will trigger. This process repeats each time the log is appended to.

I believe the performance impact of this change is negligible due to the infrequency of logging. I understand if you don't want to merge this change to cover this usage.

Evan Johnson added 2 commits August 2, 2024 01:01
This causes an FSEvent to be generated for each log write,
which launchd or other programs can watch for.
When append() finishes, logHandle deallocates and closes its file descriptor.
This causes an FSEvent to be generated for the file modification.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant