micro/debug/log/os.go

201 lines
3.1 KiB
Go
Raw Normal View History

2019-12-17 18:16:45 +00:00
package log
import (
"bufio"
"encoding/json"
2019-12-18 15:06:25 +00:00
"io"
2019-12-17 18:16:45 +00:00
"os"
2019-12-18 15:19:20 +00:00
"strings"
2019-12-18 15:06:25 +00:00
"sync"
2019-12-17 18:16:45 +00:00
"time"
2019-12-18 15:06:25 +00:00
"github.com/google/uuid"
"github.com/micro/go-micro/v2/util/ring"
2019-12-17 18:16:45 +00:00
)
// Should stream from OS
2019-12-18 15:06:25 +00:00
type osLog struct {
2019-12-19 12:20:33 +00:00
format FormatFunc
once sync.Once
2019-12-18 18:36:42 +00:00
2019-12-18 15:06:25 +00:00
sync.RWMutex
2019-12-18 17:06:29 +00:00
buffer *ring.Buffer
subs map[string]*osStream
2019-12-18 15:06:25 +00:00
}
2019-12-17 18:16:45 +00:00
type osStream struct {
2019-12-18 15:06:25 +00:00
stream chan Record
stop chan bool
}
// watch io stream
func (o *osLog) run() {
// save outputs
stdout := *os.Stdout
stderr := *os.Stderr
// new os pipe
r, w := io.Pipe()
// create new iopipes
r1, w1, _ := os.Pipe()
r2, w2, _ := os.Pipe()
// create tea readers
tee1 := io.TeeReader(r1, &stdout)
tee2 := io.TeeReader(r2, &stderr)
// start copying
go io.Copy(w, tee1)
go io.Copy(w, tee2)
// set default go log output
//log.SetOutput(w2)
// replace os stdout and os stderr
*os.Stdout = *w1
*os.Stderr = *w2
// this should short circuit everything
defer func() {
// reset stdout and stderr
*os.Stdout = stdout
*os.Stderr = stderr
//log.SetOutput(stderr)
// close all the outputs
r.Close()
r1.Close()
r2.Close()
w.Close()
w1.Close()
w2.Close()
}()
// read from standard error
scanner := bufio.NewReader(r)
for {
// read the line
line, err := scanner.ReadString('\n')
if err != nil {
return
}
// check if the line exists
if len(line) == 0 {
continue
}
// parse the record
var r Record
if line[0] == '{' {
json.Unmarshal([]byte(line), &r)
} else {
r = Record{
Timestamp: time.Now(),
2019-12-18 16:02:11 +00:00
Message: strings.TrimSuffix(line, "\n"),
2019-12-18 15:06:25 +00:00
Metadata: make(map[string]string),
}
}
o.Lock()
2019-12-18 17:06:29 +00:00
// write to the buffer
o.buffer.Put(r)
2019-12-18 15:06:25 +00:00
// check subs and send to stream
for id, sub := range o.subs {
// send to stream
select {
case <-sub.stop:
delete(o.subs, id)
case sub.stream <- r:
// send to stream
default:
// do not block
}
}
o.Unlock()
}
2019-12-17 18:16:45 +00:00
}
// Read reads log entries from the logger
func (o *osLog) Read(...ReadOption) ([]Record, error) {
2019-12-18 18:36:42 +00:00
o.once.Do(func() {
go o.run()
})
2019-12-18 17:06:29 +00:00
var records []Record
// read the last 100 records
for _, v := range o.buffer.Get(100) {
records = append(records, v.Value.(Record))
}
return records, nil
2019-12-17 18:16:45 +00:00
}
// Write writes records to log
func (o *osLog) Write(r Record) error {
2019-12-18 18:36:42 +00:00
o.once.Do(func() {
go o.run()
})
2019-12-19 12:20:33 +00:00
// generate output
out := o.format(r) + "\n"
_, err := os.Stderr.Write([]byte(out))
2019-12-17 18:16:45 +00:00
return err
}
// Stream log records
func (o *osLog) Stream() (Stream, error) {
2019-12-18 18:36:42 +00:00
o.once.Do(func() {
go o.run()
})
2019-12-18 15:06:25 +00:00
o.Lock()
defer o.Unlock()
2019-12-17 18:16:45 +00:00
2019-12-18 15:06:25 +00:00
// create stream
st := &osStream{
stream: make(chan Record, 128),
stop: make(chan bool),
}
2019-12-17 18:16:45 +00:00
2019-12-18 15:06:25 +00:00
// save stream
o.subs[uuid.New().String()] = st
return st, nil
2019-12-17 18:16:45 +00:00
}
func (o *osStream) Chan() <-chan Record {
return o.stream
}
func (o *osStream) Stop() error {
select {
case <-o.stop:
return nil
default:
close(o.stop)
}
return nil
}
func NewLog(opts ...Option) Log {
2019-12-19 12:20:33 +00:00
options := Options{
Format: DefaultFormat,
}
for _, o := range opts {
o(&options)
}
2019-12-18 17:06:29 +00:00
l := &osLog{
2019-12-19 12:20:33 +00:00
format: options.Format,
2019-12-18 17:06:29 +00:00
buffer: ring.New(1024),
subs: make(map[string]*osStream),
2019-12-18 15:06:25 +00:00
}
2019-12-18 17:06:29 +00:00
return l
2019-12-17 18:16:45 +00:00
}