logmania/notify/all/internal.go

51 lines
880 B
Go
Raw Normal View History

2017-06-16 10:33:35 +02:00
package all
import (
"github.com/genofire/logmania/lib"
2017-08-09 08:45:45 +02:00
"github.com/genofire/logmania/log"
2017-06-16 10:33:35 +02:00
"github.com/genofire/logmania/notify"
)
type Notifier struct {
notify.Notifier
2017-08-09 08:45:45 +02:00
list []notify.Notifier
channelNotify chan *log.Entry
2017-06-16 10:33:35 +02:00
}
func Init(config *lib.NotifyConfig, state *notify.NotifyState) notify.Notifier {
2017-06-16 10:33:35 +02:00
var list []notify.Notifier
for _, init := range notify.NotifyRegister {
notify := init(config, state)
2017-06-16 10:33:35 +02:00
if notify == nil {
continue
}
list = append(list, notify)
}
2017-08-09 08:45:45 +02:00
n := &Notifier{
list: list,
channelNotify: make(chan *log.Entry),
2017-06-16 10:33:35 +02:00
}
2017-08-09 08:45:45 +02:00
go n.sender()
return n
2017-06-16 10:33:35 +02:00
}
2017-08-09 08:45:45 +02:00
func (n *Notifier) sender() {
for c := range n.channelNotify {
for _, item := range n.list {
item.Send(c)
}
2017-06-16 10:33:35 +02:00
}
}
2017-08-09 08:45:45 +02:00
func (n *Notifier) Send(e *log.Entry) {
n.channelNotify <- e
}
2017-06-16 10:33:35 +02:00
func (n *Notifier) Close() {
for _, item := range n.list {
item.Close()
}
}