Documentation
¶
Overview ¶
Package logcap caps the size of the daemon's own log file.
Under launchd the daemon's stdout/stderr are a plain file (StandardOutPath/StandardErrorPath = ~/.pilot/daemon.log) that launchd opens and never rotates, and nothing else is set up to: one laptop's daemon.log reached 22 MB, a June log 13 MB compressed. Renaming it away would not help — the daemon, and every child it spawns (they inherit the descriptor), keep writing to the open file.
So the daemon rotates it from the inside, copy-truncate style: when the file exceeds the limit, copy it to <log>.pilot.1, truncate the original through the daemon's own descriptor, then gzip the copy into <log>.pilot.1.gz, shifting older generations up to <log>.pilot.N.gz. Truncation is safe for every writer: launchd (and `pilotctl daemon start`) open the log O_APPEND, so each write lands at the new end of file, and children share that same open file description. Lines written between the copy and the truncate are lost — the usual copy-truncate trade-off, a window of milliseconds.
The oldest generation is dropped only once the log is truncated. A log that cannot be truncated (an append-only file, a filesystem that refuses it) keeps all its generations: the copy is removed and the shift undone, and the daemon retries at growing intervals rather than copying the whole log every minute.
Files logcap did not create are left alone. The ".pilot" infix keeps its names apart from the ones other rotators give the same log (logrotate's <log>.1 and <log>.N.gz, newsyslog's <log>.0.gz), so an operator's own rotation keeps its history. Within its own names, logcap reads back, renames or removes only a regular file owned by the current user; a symlink, a directory or another user's file at one of them stops that round's backup (the log is still truncated). Files are created O_EXCL|O_NOFOLLOW, never through an existing name. When the log's directory is writable by others, or by a group other than the user's own private group, or owned by another user (not root), logcap keeps no backups at all and only truncates, so it creates nothing where someone else could have planted a link.
A rotation that dies between the copy and the compress (crash, kill) leaves <log>.pilot.1 behind; the next rotation of that log finishes it. `pilotctl daemon start` names each daemon's log pilot-<pid>.log, so no later daemon rotates the name a crashed one staged under: a rotation in the directory `pilotctl daemon start` uses (one of Options.Within, not a directory below it) also finishes the copies of those logs, pilot-<pid>.log.pilot.1. It takes no other <name>.pilot.1 for an interrupted copy: that may be another program's file, such as logrotate's delaycompress generation of a log named <name>.pilot. A rotation holds its staged copy flock(2)ed from creation until it is compressed, which is how a copy another live daemon is still working on is told apart and left alone.
Options.Anywhere, Options.Within and Options.Files set where it applies: the daemon rotates by default only a log Pilot set up — inside ~/.pilot, where install.sh's launchd job and `pilotctl daemon start` put it, or the Homebrew service's log — and a log elsewhere only when the operator asks for rotation explicitly.
When the log is not a regular file — journald under systemd, a terminal, a pipe — there is nothing to cap and the package does nothing.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Watch ¶
Watch checks f every interval until ctx is done, rotating it whenever it has grown past opts.MaxBytes; less often while f cannot be truncated (retryAfter). It returns false, starting nothing, when capping is disabled (MaxBytes <= 0), f is not a regular file, the platform cannot map a descriptor back to its path, or the log is out of scope (see Options.Anywhere).
Types ¶
type Options ¶
type Options struct {
// MaxBytes is the size past which the log is rotated; <= 0 disables
// rotation.
MaxBytes int64
// MaxBackups is the number of gzipped generations kept; 0 truncates
// without keeping any.
MaxBackups int
// Anywhere rotates the log wherever it lives. Without it only a log
// inside one of the Within directories (at any depth), or at one of
// the Files paths, is rotated, so the zero Options rotate nothing.
Anywhere bool
// Within are Pilot's own directories. A rotation of a log directly in
// one of them, with or without Anywhere, also finishes the
// interrupted rotations of `pilotctl daemon start`'s logs there.
Within []string
// Files are single logs in scope in a directory that is not Pilot's
// as a whole, such as the Homebrew service's
// <prefix>/var/log/pilot-daemon.log. A log matches one by name within
// the same directory, the directory compared by identity.
Files []string
}
Options configure a Rotator.