From d4bec24eb73a94053bcf5698b10516d07f97e7d0 Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Wed, 18 Dec 2019 17:06:29 +0000 Subject: [PATCH] Add os log buffer --- debug/log/os.go | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/debug/log/os.go b/debug/log/os.go index bae3662e..53ae60c5 100644 --- a/debug/log/os.go +++ b/debug/log/os.go @@ -10,12 +10,14 @@ import ( "time" "github.com/google/uuid" + "github.com/micro/go-micro/util/ring" ) // Should stream from OS type osLog struct { sync.RWMutex - subs map[string]*osStream + buffer *ring.Buffer + subs map[string]*osStream } type osStream struct { @@ -94,11 +96,8 @@ func (o *osLog) run() { o.Lock() - // bail if there's no subscribers - if len(o.subs) == 0 { - o.Unlock() - return - } + // write to the buffer + o.buffer.Put(r) // check subs and send to stream for id, sub := range o.subs { @@ -119,7 +118,14 @@ func (o *osLog) run() { // Read reads log entries from the logger func (o *osLog) Read(...ReadOption) ([]Record, error) { - return []Record{}, nil + var records []Record + + // read the last 100 records + for _, v := range o.buffer.Get(100) { + records = append(records, v.Value.(Record)) + } + + return records, nil } // Write writes records to log @@ -134,11 +140,6 @@ func (o *osLog) Stream() (Stream, error) { o.Lock() defer o.Unlock() - // start stream watcher - if len(o.subs) == 0 { - go o.run() - } - // create stream st := &osStream{ stream: make(chan Record, 128), @@ -166,7 +167,12 @@ func (o *osStream) Stop() error { } func NewLog(opts ...Option) Log { - return &osLog{ - subs: make(map[string]*osStream), + l := &osLog{ + buffer: ring.New(1024), + subs: make(map[string]*osStream), } + + go l.run() + + return l }