diff options
| author | Jeroen Wijenbergh <jeroen.wijenbergh@geant.org> | 2026-02-12 12:34:08 +0100 |
|---|---|---|
| committer | Jeroen Wijenbergh <jeroen.wijenbergh@geant.org> | 2026-02-12 12:59:03 +0100 |
| commit | a30ef6b27e578a4cf0a674b24f5b52b4c1516c63 (patch) | |
| tree | 27c7321cbceac2a487c1ba17151711de3d438a53 /internal/log/rotate.go | |
| parent | b00ce8214479c50e137db73c77b0cc1393c5e7d4 (diff) | |
All: Rename packages that sound useless or clash with std
Diffstat (limited to 'internal/log/rotate.go')
| -rw-r--r-- | internal/log/rotate.go | 109 |
1 files changed, 0 insertions, 109 deletions
diff --git a/internal/log/rotate.go b/internal/log/rotate.go deleted file mode 100644 index 2971f70..0000000 --- a/internal/log/rotate.go +++ /dev/null @@ -1,109 +0,0 @@ -package log - -import ( - "io" - "os" - "sync" - - "codeberg.org/eduVPN/eduvpn-common/internal/atomicfile" -) - -var ( - // MaxSize is the maximum size in bytes from when to start trimming - MaxSize int64 = 10 * 1024 * 1024 - - // TrimSize denotes how much to trim from the beginning - TrimSize = MaxSize / 2 - - // TrimMsg is the message to display when it was trimmed - TrimMsg = "--- previous part was trimmed by eduvpn-common as the file was too big (10MB) ---\n" -) - -// FileRotater is a file that is trimmed when a maximum size is reached -// This is for logging useful -type FileRotater struct { - filename string - file *os.File - mu sync.Mutex -} - -// NewFileRotater creates a new log file rotater -func NewFileRotater(filename string) (*FileRotater, error) { - fr := &FileRotater{ - filename: filename, - } - - err := fr.open() - if err != nil { - return nil, err - } - return fr, nil -} - -func (fr *FileRotater) open() error { - f, err := os.OpenFile(fr.filename, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0o666) - if err != nil { - return err - } - fr.file = f - return nil -} - -func (fr *FileRotater) trim() error { - // We need to seek to the trim size to skip over that part as we discard it - _, err := fr.file.Seek(TrimSize, io.SeekStart) - if err != nil { - return err - } - - // get the part of the file that we want to keep - keep, err := io.ReadAll(fr.file) - if err != nil { - return err - } - - all := []byte(TrimMsg) - all = append(all, keep...) - err = atomicfile.WriteFile(fr.file.Name(), all, 0o666) - if err != nil { - return err - } - - // re-open the handle as the file was renamed - err = fr.file.Close() - if err != nil { - return err - } - err = fr.open() - if err != nil { - return err - } - return nil -} - -// Write implements io.Writer for the log rotater -func (fr *FileRotater) Write(p []byte) (n int, err error) { - fr.mu.Lock() - defer fr.mu.Unlock() - fi, err := fr.file.Stat() - if err != nil { - return 0, err - } - - if fi.Size() >= MaxSize { - err = fr.trim() - if err != nil { - return 0, err - } - } - // we don't write atomically here as we want it to be as fast as possible - // and if we lose a part of one log statement it's not a big deal - return fr.file.Write(p) -} - -// Close closes the file in a safe way by locking and unlocking the mutex -func (fr *FileRotater) Close() error { - fr.mu.Lock() - defer fr.mu.Unlock() - return fr.file.Close() -} |
