Add os log buffer
This commit is contained in:
		| @@ -10,12 +10,14 @@ import ( | |||||||
| 	"time" | 	"time" | ||||||
|  |  | ||||||
| 	"github.com/google/uuid" | 	"github.com/google/uuid" | ||||||
|  | 	"github.com/micro/go-micro/util/ring" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| // Should stream from OS | // Should stream from OS | ||||||
| type osLog struct { | type osLog struct { | ||||||
| 	sync.RWMutex | 	sync.RWMutex | ||||||
| 	subs map[string]*osStream | 	buffer *ring.Buffer | ||||||
|  | 	subs   map[string]*osStream | ||||||
| } | } | ||||||
|  |  | ||||||
| type osStream struct { | type osStream struct { | ||||||
| @@ -94,11 +96,8 @@ func (o *osLog) run() { | |||||||
|  |  | ||||||
| 		o.Lock() | 		o.Lock() | ||||||
|  |  | ||||||
| 		// bail if there's no subscribers | 		// write to the buffer | ||||||
| 		if len(o.subs) == 0 { | 		o.buffer.Put(r) | ||||||
| 			o.Unlock() |  | ||||||
| 			return |  | ||||||
| 		} |  | ||||||
|  |  | ||||||
| 		// check subs and send to stream | 		// check subs and send to stream | ||||||
| 		for id, sub := range o.subs { | 		for id, sub := range o.subs { | ||||||
| @@ -119,7 +118,14 @@ func (o *osLog) run() { | |||||||
|  |  | ||||||
| // Read reads log entries from the logger | // Read reads log entries from the logger | ||||||
| func (o *osLog) Read(...ReadOption) ([]Record, error) { | 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 | // Write writes records to log | ||||||
| @@ -134,11 +140,6 @@ func (o *osLog) Stream() (Stream, error) { | |||||||
| 	o.Lock() | 	o.Lock() | ||||||
| 	defer o.Unlock() | 	defer o.Unlock() | ||||||
|  |  | ||||||
| 	// start stream watcher |  | ||||||
| 	if len(o.subs) == 0 { |  | ||||||
| 		go o.run() |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	// create stream | 	// create stream | ||||||
| 	st := &osStream{ | 	st := &osStream{ | ||||||
| 		stream: make(chan Record, 128), | 		stream: make(chan Record, 128), | ||||||
| @@ -166,7 +167,12 @@ func (o *osStream) Stop() error { | |||||||
| } | } | ||||||
|  |  | ||||||
| func NewLog(opts ...Option) Log { | func NewLog(opts ...Option) Log { | ||||||
| 	return &osLog{ | 	l := &osLog{ | ||||||
| 		subs: make(map[string]*osStream), | 		buffer: ring.New(1024), | ||||||
|  | 		subs:   make(map[string]*osStream), | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | 	go l.run() | ||||||
|  |  | ||||||
|  | 	return l | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user