Files
Kelx/console-log-persistence.go
T
2026-11-30 16:32:37 +00:00

134 lines
3.0 KiB
Go

package main
import (
"bytes"
"io"
"os"
"strings"
)
// adpanel:console-history-byte-cap - a line count does not bound the space
// or allocation needed for container-controlled log data.
const consolePersistMaxBytes = 2 * 1024 * 1024
func readConsoleLogFileTail(path string, limit int64) ([]string, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
info, err := file.Stat()
if err != nil {
return nil, err
}
start := max(0, info.Size()-limit)
readStart := max(0, start-1)
data, err := io.ReadAll(io.NewSectionReader(file, readStart, info.Size()-readStart))
if err != nil {
return nil, err
}
if start > 0 {
next := bytes.IndexByte(data, '\n')
if next < 0 {
return nil, nil
}
data = data[next+1:]
}
if len(data) == 0 {
return nil, nil
}
return strings.Split(strings.TrimSuffix(string(data), "\n"), "\n"), nil
}
func (t *logTail) appendPersistedLines(lines []string) {
if t == nil || len(lines) == 0 {
return
}
t.persistMu.Lock()
defer t.persistMu.Unlock()
if t.persistPath == "" {
return
}
var buf strings.Builder
added := 0
for _, line := range lines {
for _, part := range strings.Split(line, "\n") {
part = strings.TrimRight(part, "\r")
if part == "" {
continue
}
buf.WriteString(part)
buf.WriteByte('\n')
added++
}
}
if added == 0 {
return
}
file, err := os.OpenFile(t.persistPath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o600)
if err != nil {
return
}
_, writeErr := file.WriteString(buf.String())
info, statErr := file.Stat()
_ = file.Close()
if writeErr == nil {
t.persistLines += added
t.persistLineTotal += int64(added)
}
overBudget := statErr == nil && info.Size() > consolePersistMaxBytes
if overBudget || t.persistLines > consolePersistMaxLines {
t.rotatePersistedLogLocked(overBudget)
}
}
// Caller holds persistMu. Leave headroom on byte-driven rotations so a burst
// of long lines does not rewrite the capped file on every append.
func (t *logTail) rotatePersistedLogLocked(overByteBudget bool) {
limit := int64(consolePersistMaxBytes)
if overByteBudget {
limit = limit * 3 / 4
}
lines, err := readConsoleLogFileTail(t.persistPath, limit)
if err != nil {
return
}
if len(lines) > consolePersistMaxLines {
lines = lines[len(lines)-consolePersistMaxLines:]
} else if !overByteBudget {
t.persistLines = len(lines)
return
}
data := strings.Join(lines, "\n")
if len(lines) > 0 {
data += "\n"
}
tmp := t.persistPath + ".tmp"
if err := os.WriteFile(tmp, []byte(data), 0o600); err != nil {
return
}
if err := os.Rename(tmp, t.persistPath); err != nil {
_ = os.Remove(tmp)
return
}
t.persistLines = len(lines)
}
func (t *logTail) readPersistedSnapshot(tail int) ([]string, int64) {
if t == nil || tail <= 0 {
return nil, 0
}
t.persistMu.Lock()
defer t.persistMu.Unlock()
if t.persistPath == "" {
return nil, 0
}
lines, err := readConsoleLogFileTail(t.persistPath, consolePersistMaxBytes)
if err != nil {
return nil, 0
}
return lines[max(0, len(lines)-tail):], t.persistLineTotal
}