about summary refs log tree commit diff
path: root/third_party/gerrit-queue/misc/rotatingloghandler.go
diff options
context:
space:
mode:
Diffstat (limited to 'third_party/gerrit-queue/misc/rotatingloghandler.go')
-rw-r--r--third_party/gerrit-queue/misc/rotatingloghandler.go34
1 files changed, 34 insertions, 0 deletions
diff --git a/third_party/gerrit-queue/misc/rotatingloghandler.go b/third_party/gerrit-queue/misc/rotatingloghandler.go
new file mode 100644
index 0000000000..3d4c5f3a83
--- /dev/null
+++ b/third_party/gerrit-queue/misc/rotatingloghandler.go
@@ -0,0 +1,34 @@
+package misc
+
+import (
+	"sync"
+
+	"github.com/apex/log"
+)
+
+// RotatingLogHandler implementation.
+type RotatingLogHandler struct {
+	mu         sync.Mutex
+	Entries    []*log.Entry
+	maxEntries int
+}
+
+// NewRotatingLogHandler creates a new rotating log handler
+func NewRotatingLogHandler(maxEntries int) *RotatingLogHandler {
+	return &RotatingLogHandler{
+		maxEntries: maxEntries,
+	}
+}
+
+// HandleLog implements log.Handler.
+func (h *RotatingLogHandler) HandleLog(e *log.Entry) error {
+	h.mu.Lock()
+	defer h.mu.Unlock()
+	// drop tail if we have more entries than maxEntries
+	if len(h.Entries) > h.maxEntries {
+		h.Entries = append([]*log.Entry{e}, h.Entries[:(h.maxEntries-2)]...)
+	} else {
+		h.Entries = append([]*log.Entry{e}, h.Entries...)
+	}
+	return nil
+}