Files
ctrld/cmd/cli/log_budget.go
T
Anthony Wong 8934e09428 feat(cli): keep a log journal on disk with file headers and an upload limit
ctrld keeps a second log file, ctrld-journal.log, next to ctrld.log. The
journal holds all warnings and errors and each event with the field
journal=true. It stays on disk after a start of the service and after a
self-upgrade. Each log file starts with a Log header line. Each log send
upload starts with the same line, made at send time, and has a limit of
about 16 MB.
2026-09-28 16:47:40 +07:00

46 lines
1.7 KiB
Go

package cli
import (
"github.com/Control-D-Inc/ctrld"
)
// journalLogFileName is the journal file next to the debug log in the ctrld
// home directory.
const journalLogFileName = "ctrld-journal.log"
// logBudgets splits the disk space by platform. A router keeps the 10 MB total
// that QA measured on an EdgeRouter-X, because its flash is small. A desktop
// holds more, so a log covers more days.
func logBudgets(isRouter bool) (debug, journal logBudget) {
if isRouter {
return logBudget{maxSize: 5 << 20, backups: 1}, logBudget{maxSize: 1 << 20, backups: 1}
}
return logBudget{maxSize: 10 << 20, backups: 4}, logBudget{maxSize: 2 << 20, backups: 2}
}
const (
// logMaxSizeMBLimit and logMaxBackupsLimit bound the two keys. The log
// file opens before ctrld validates its configuration, so a key outside
// these bounds reaches this code: a size near the largest integer never
// rotates, and a count of one million backups renames one million files.
// The validation rules are the first line of defense, and these bounds
// are the second.
logMaxSizeMBLimit = 1024
logMaxBackupsLimit = 64
)
// debugLogBudget starts from the platform default, so a router keeps its small
// total until an operator asks for more. The two keys size the debug stream
// only. The journal budget stays fixed. A key outside its bounds keeps the
// default.
func debugLogBudget(svc *ctrld.ServiceConfig, isRouter bool) logBudget {
budget, _ := logBudgets(isRouter)
if mb := svc.LogMaxSizeMB; mb >= 1 && mb <= logMaxSizeMBLimit {
budget.maxSize = int64(mb) << 20
}
if count := svc.LogMaxBackups; count != nil && *count >= 0 && *count <= logMaxBackupsLimit {
budget.backups = *count
}
return budget
}