2021-12-16 06:25:57 +00:00
|
|
|
package history
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
ERROR = "ERROR"
|
|
|
|
INFO = "INFO"
|
|
|
|
)
|
|
|
|
|
|
|
|
type History struct {
|
|
|
|
ID uint `gorm:"primaryKey"`
|
|
|
|
CreatedAt time.Time
|
|
|
|
|
|
|
|
Module string
|
|
|
|
Type string
|
|
|
|
|
|
|
|
Message string
|
|
|
|
Body string
|
|
|
|
Issuer string
|
|
|
|
Caller string
|
|
|
|
Trace string
|
|
|
|
}
|
|
|
|
|
|
|
|
var Tunnel chan *History
|
|
|
|
var quit chan struct{}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
Tunnel = make(chan *History)
|
2021-12-16 10:11:34 +00:00
|
|
|
quit = make(chan struct{}, 1)
|
2021-12-16 06:25:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Start(r Receiver) {
|
|
|
|
go func() {
|
2021-12-16 10:11:34 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case h := <-Tunnel:
|
|
|
|
r(h)
|
|
|
|
case <-quit:
|
|
|
|
return
|
|
|
|
}
|
2021-12-16 06:25:57 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func Stop() {
|
|
|
|
quit <- struct{}{}
|
|
|
|
}
|
|
|
|
|
2021-12-16 10:11:34 +00:00
|
|
|
func Send(h *History) {
|
|
|
|
Tunnel <- h
|
|
|
|
}
|
|
|
|
|
2021-12-16 06:25:57 +00:00
|
|
|
type Receiver func(*History)
|
|
|
|
|
|
|
|
var DBReceiver Receiver = func(h *History) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
var ConsoleReceiver Receiver = func(h *History) {
|
|
|
|
fmt.Printf("%+v\n", h)
|
|
|
|
}
|