recorder.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Copyright (C) 2025 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package slogutil
  7. import (
  8. "log/slog"
  9. "sync"
  10. "time"
  11. )
  12. const maxLogLines = 1000
  13. type Recorder interface {
  14. Since(t time.Time) []Line
  15. Clear()
  16. }
  17. func NewRecorder(level slog.Level) Recorder {
  18. return &lineRecorder{level: level}
  19. }
  20. type lineRecorder struct {
  21. level slog.Level
  22. mut sync.Mutex
  23. lines []Line
  24. }
  25. func (r *lineRecorder) record(line Line) {
  26. if line.Level < r.level {
  27. return
  28. }
  29. r.mut.Lock()
  30. r.lines = append(r.lines, line)
  31. if len(r.lines) > maxLogLines {
  32. r.lines = r.lines[len(r.lines)-maxLogLines:]
  33. }
  34. r.mut.Unlock()
  35. }
  36. func (r *lineRecorder) Clear() {
  37. r.mut.Lock()
  38. r.lines = nil
  39. r.mut.Unlock()
  40. }
  41. func (r *lineRecorder) Since(t time.Time) []Line {
  42. r.mut.Lock()
  43. defer r.mut.Unlock()
  44. for i := range r.lines {
  45. if r.lines[i].When.After(t) {
  46. return r.lines[i:]
  47. }
  48. }
  49. return nil
  50. }