2018-09-05 01:53:23 +02:00
|
|
|
package all
|
|
|
|
|
|
|
|
import (
|
2019-06-20 09:25:43 +02:00
|
|
|
"github.com/bdlm/log"
|
2019-07-17 22:31:15 +02:00
|
|
|
"time"
|
2018-09-05 01:53:23 +02:00
|
|
|
|
2019-08-08 15:51:55 +02:00
|
|
|
"dev.sum7.eu/sum7/logmania/bot"
|
|
|
|
"dev.sum7.eu/sum7/logmania/database"
|
|
|
|
"dev.sum7.eu/sum7/logmania/output"
|
2018-09-05 01:53:23 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var logger = log.WithField("notify", "all")
|
|
|
|
|
|
|
|
type Output struct {
|
|
|
|
output.Output
|
|
|
|
list []output.Output
|
|
|
|
db *database.DB
|
|
|
|
channelNotify chan *log.Entry
|
|
|
|
}
|
|
|
|
|
|
|
|
func Init(configInterface interface{}, db *database.DB, bot *bot.Bot) output.Output {
|
|
|
|
config := configInterface.(map[string]interface{})
|
|
|
|
|
|
|
|
var list []output.Output
|
2018-09-05 21:00:02 +02:00
|
|
|
|
2018-09-05 01:53:23 +02:00
|
|
|
for outputType, init := range output.Register {
|
|
|
|
configForItem := config[outputType]
|
|
|
|
if configForItem == nil {
|
|
|
|
log.Warnf("the input type '%s' has no configuration\n", outputType)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
notify := init(configForItem, db, bot)
|
|
|
|
|
|
|
|
if notify == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
list = append(list, notify)
|
|
|
|
def := notify.Default()
|
2018-09-05 21:00:02 +02:00
|
|
|
if def == nil {
|
2018-09-05 01:53:23 +02:00
|
|
|
continue
|
|
|
|
}
|
2018-09-05 21:00:02 +02:00
|
|
|
db.DefaultNotify = append(db.DefaultNotify, def...)
|
2018-09-05 01:53:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
out := &Output{
|
|
|
|
db: db,
|
|
|
|
list: list,
|
|
|
|
channelNotify: make(chan *log.Entry),
|
|
|
|
}
|
|
|
|
go out.sender()
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
func (out *Output) sender() {
|
|
|
|
for c := range out.channelNotify {
|
|
|
|
e, _, tos := out.db.SendTo(c)
|
|
|
|
for _, to := range tos {
|
|
|
|
send := false
|
|
|
|
for _, item := range out.list {
|
|
|
|
send = item.Send(e, to)
|
|
|
|
if send {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !send {
|
2019-06-20 10:37:39 +02:00
|
|
|
logger.Warnf("notify not send to %s: [%d] %s", to.Address(), c.Level, c.Message)
|
2018-09-05 01:53:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (out *Output) Send(e *log.Entry, to *database.Notify) bool {
|
2019-07-17 22:31:15 +02:00
|
|
|
before := time.Now()
|
|
|
|
|
|
|
|
logger := log.WithFields(e.Data)
|
|
|
|
logger = logger.WithField("msg", e.Message)
|
|
|
|
|
|
|
|
logger.Debugf("starting forward message")
|
|
|
|
|
2018-09-05 01:53:23 +02:00
|
|
|
out.channelNotify <- e
|
2019-07-17 22:31:15 +02:00
|
|
|
|
|
|
|
after := time.Now()
|
|
|
|
delta := after.Sub(before)
|
|
|
|
logger.WithField("ms", float64(delta)/float64(time.Millisecond)).Debugf("end forward message")
|
|
|
|
|
2018-09-05 01:53:23 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (out *Output) Close() {
|
|
|
|
for _, item := range out.list {
|
|
|
|
item.Close()
|
|
|
|
}
|
|
|
|
}
|