Skip to content
Snippets Groups Projects
Commit 4ee28e38 authored by Murat Kabilov's avatar Murat Kabilov
Browse files

add ringlog

parent 9c7492f3
Branches
Tags
No related merge requests found
package ringlog
import (
"container/list"
"sync"
)
// RingLogger describes ring logger methods
type RingLogger interface {
Insert(interface{})
Walk() []interface{}
}
// RingLog is a capped logger with fixed size
type RingLog struct {
sync.RWMutex
size int
list *list.List
}
// New creates new Ring logger
func New(size int) *RingLog {
r := RingLog{
list: list.New(),
size: size,
}
return &r
}
// Insert inserts new LogEntry into the ring logger
func (r *RingLog) Insert(obj interface{}) {
r.Lock()
defer r.Unlock()
r.list.PushBack(obj)
if r.list.Len() > r.size {
r.list.Remove(r.list.Front())
}
}
// Walk dumps all the LogEntries from the Ring logger
func (r *RingLog) Walk() []interface{} {
res := make([]interface{}, 0)
r.RLock()
defer r.RUnlock()
st := r.list.Front()
for i := 0; i < r.size; i++ {
if st == nil {
return res
}
res = append(res, st.Value)
st = st.Next()
}
return res
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment