1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//! In-place trim of the daemon's supervisor-redirected log.
//!
//! `~/.clauth/daemon.log` is opened by **launchd** (`StandardErrorPath`) with an
//! `O_APPEND` fd that launchd holds for the daemon's whole lifetime and never
//! reopens. That rules out rename-based rotation: renaming the file leaves
//! launchd appending to the renamed inode, so the "new" `daemon.log` stays empty
//! and the real file grows unbounded. The only rotation that survives a held
//! `O_APPEND` fd is an **in-place trim** — rewrite the SAME inode to keep just its
//! tail. launchd's next append lands at the new EOF, so logging continues
//! seamlessly; at most a few bytes written during the trim window are dropped (a
//! diagnostic log, not a ledger — an acceptable, rare loss).
//!
//! Sized for the real worst case (#39 corrected: ~5 MB/day, not "multi-GB in
//! months"), so this is a size cap, not a heavyweight log pipeline.
use ;
use Path;
/// Trim once the log passes ~5 MiB…
pub const LOG_MAX_BYTES: u64 = 5 * 1024 * 1024;
/// …keeping the last ~1 MiB of history.
pub const LOG_KEEP_BYTES: u64 = 1024 * 1024;
/// If `path` is larger than `max_bytes`, rewrite it in place to retain only its
/// last `keep_bytes` (trimmed to a line boundary). Returns `Ok(true)` when a trim
/// happened. The common case — file absent or under the cap — is a single
/// `metadata` stat returning `Ok(false)`, cheap enough to call on a timer.
///
/// Never renames: launchd holds this inode's `O_APPEND` fd, so a rename would
/// orphan every future log line. The rewrite is on the same inode, and after
/// `set_len` launchd's next append resumes at the new EOF.
pub