78 lines
1.0 KiB
Go
78 lines
1.0 KiB
Go
package history
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"kumoly.io/kumoly/app/errors"
|
|
)
|
|
|
|
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{}
|
|
var started chan struct{}
|
|
|
|
func init() {
|
|
Tunnel = make(chan *History)
|
|
quit = make(chan struct{}, 1)
|
|
started = make(chan struct{}, 1)
|
|
}
|
|
|
|
func Start(r Receiver) {
|
|
select {
|
|
case started <- struct{}{}:
|
|
default:
|
|
panic(errors.New(500, "history has already started!"))
|
|
}
|
|
go func() {
|
|
for {
|
|
select {
|
|
case h := <-Tunnel:
|
|
r(h)
|
|
case <-quit:
|
|
<-started
|
|
return
|
|
}
|
|
}
|
|
}()
|
|
}
|
|
|
|
func Stop() {
|
|
quit <- struct{}{}
|
|
}
|
|
|
|
func Send(h *History) {
|
|
if h.Type == "" {
|
|
h.Type = INFO
|
|
}
|
|
Tunnel <- h
|
|
}
|
|
|
|
type Receiver func(*History)
|
|
|
|
var DBReceiver Receiver = func(h *History) {
|
|
|
|
}
|
|
|
|
var ConsoleReceiver Receiver = func(h *History) {
|
|
fmt.Printf("%+v\n", h)
|
|
}
|