Documentation
¶
Overview ¶
Package filelog tees the agent's stderr stream to a file on disk so that when agent-api is unreachable (network failure, firewall, expired credentials), the customer can be asked to share the local log file with support.
Mechanism: Start() opens an os.Pipe, swaps os.Stderr for the pipe's write end, and spawns a goroutine that reads from the pipe and writes to both the original stderr file and the on-disk log file. Stop() closes the write end, waits for the goroutine to drain, restores os.Stderr, and closes the file. This mirrors the in-memory capture already used by internal/telemetry/logcapture.go, with a file in place of the bytes.Buffer.
The file is opened with O_APPEND|O_SYNC. O_SYNC matters because os.Exit (called from many sites in main.go) skips defers, so the tee goroutine may not flush before the process dies — O_SYNC makes each individual Write durable at the cost of a few extra ms per run.
Rotation is single-file and per-open: if the existing file exceeds maxBytes at Start time, it is renamed to path+".prev" so one prior run survives. This is a diagnostic snapshot, not an audit log; the 5 MiB cap matches the precedent set by internal/aiagents/cli/errlog.go.
In service mode (launchd LaunchAgent, systemd user units, Windows Task Scheduler) the OS-level redirect is already writing the agent's stderr to a file. StartIfEligible detects this via ShouldSkip (os.Stderr.Stat reports a regular file) and returns (nil, nil) without opening anything, so the OS redirect remains the sole writer and no lines are duplicated.
Index ¶
Constants ¶
const DefaultMaxBytes int64 = 5 * 1024 * 1024
DefaultMaxBytes is the size cap that triggers single-file rotation at Start time. Matches internal/aiagents/cli/errlog.go's MaxErrorLogBytes for consistency with the existing on-disk-log convention.
const Filename = "agent.error.log"
Filename is the basename of the stderr log file the agent writes into the configured base directory. Exported so callers (main, configure prompts) can show the full resolved path to the user without duplicating the literal.
const StdoutFilename = "agent.log"
StdoutFilename is the basename of the stdout log file the OS-level scheduler redirect writes to in service mode. The agent itself does not write here, but RotateIfOverCap is applied to it at startup so it can't grow unbounded.
Variables ¶
This section is empty.
Functions ¶
func DefaultDir ¶
func DefaultDir() string
DefaultDir returns the resolved install dir (paths.Home()) — that is the directory the agent writes agent.error.log into by default. Empty string if the home directory cannot be resolved; callers treat empty as "disabled."
func DefaultPath ¶
func DefaultPath() string
DefaultPath is a convenience for DefaultDir() + Filename.
func RotateIfOverCap ¶
RotateIfOverCap renames path to path+".prev" when its current size exceeds maxBytes, freeing the original name for a fresh log. Used by Start before opening the stderr file, and by main at startup for the stdout-redirect file (agent.log) so neither log can grow unbounded. Best-effort: any stat/rename failure is swallowed so a missing file or a cross-device rename doesn't take down the agent.
func ShouldSkip ¶
ShouldSkip reports whether the in-process file writer should be skipped because the given stream is already redirected to a regular file by the OS (service mode). Terminals report ModeCharDevice and pipes report ModeNamedPipe; only regular files trigger the skip.
Errors from Stat are treated as "no skip" — better to log to a fresh file than to log nowhere.
Types ¶
type Capture ¶
type Capture struct {
// contains filtered or unexported fields
}
Capture owns the swapped os.Stderr, the pipe ends, the log file, and the tee goroutine's lifecycle. A nil receiver is safe to call Stop on — that simplifies main.go where StartIfEligible may return nil.
func Start ¶
Start installs the stderr tee, opening (and rotating) the log file and swapping os.Stderr in a single best-effort sequence. On any failure before os.Stderr is reassigned, the global state is left untouched and an error is returned.
path must be non-empty. To install conditionally based on path / service-mode detection, use StartIfEligible instead.
func StartIfEligible ¶
StartIfEligible is the main.go-facing entrypoint. It returns (nil, nil) when capture should be skipped (empty path, or ShouldSkip(os.Stderr) returns true), and the result of Start otherwise. Callers can `defer cap.Stop()` unconditionally because Stop on a nil receiver is a no-op.