2020-12-21 16:18:08 +08:00
|
|
|
package utils
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"regexp"
|
|
|
|
|
"strings"
|
2020-12-28 16:36:59 +08:00
|
|
|
|
|
|
|
|
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
|
2020-12-21 16:18:08 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
EndTag = "NNNN"
|
2020-12-28 16:58:36 +08:00
|
|
|
Expression = "(?s)ZCZC.*NNNN"
|
2020-12-21 16:18:08 +08:00
|
|
|
)
|
|
|
|
|
|
2020-12-29 11:11:13 +08:00
|
|
|
var buffer string
|
2020-12-29 11:35:09 +08:00
|
|
|
var exp, _ = regexp.Compile(Expression)
|
2020-12-28 16:36:59 +08:00
|
|
|
var RawLog *rotatelogs.RotateLogs
|
2020-12-21 16:18:08 +08:00
|
|
|
|
2020-12-29 11:00:01 +08:00
|
|
|
func Append(data string) bool {
|
2020-12-29 11:43:43 +08:00
|
|
|
buffer += (data + "\n")
|
2020-12-28 16:58:36 +08:00
|
|
|
return check()
|
2020-12-21 16:18:08 +08:00
|
|
|
}
|
|
|
|
|
|
2020-12-28 16:58:36 +08:00
|
|
|
func check() bool {
|
2020-12-29 11:11:13 +08:00
|
|
|
teleString := buffer
|
2020-12-21 16:18:08 +08:00
|
|
|
if strings.Index(teleString, EndTag) > 0 {
|
2020-12-29 11:11:13 +08:00
|
|
|
telegram := exp.FindString(teleString)
|
2020-12-21 16:18:08 +08:00
|
|
|
if len(telegram) > 0 {
|
2020-12-28 16:13:57 +08:00
|
|
|
// Log.Info("telegram ", zap.String("text", telegram))
|
2020-12-28 17:01:14 +08:00
|
|
|
telegram = removeEmpty(telegram) + "\n\n"
|
2020-12-29 11:11:13 +08:00
|
|
|
telegram += "\n"
|
2020-12-29 11:30:19 +08:00
|
|
|
RawLog.Write([]byte(telegram + "\n"))
|
2020-12-29 11:11:13 +08:00
|
|
|
buffer = ""
|
2020-12-21 17:59:37 +08:00
|
|
|
InsertTelegram(telegram)
|
2020-12-28 16:58:36 +08:00
|
|
|
return true
|
2020-12-21 16:18:08 +08:00
|
|
|
}
|
|
|
|
|
}
|
2020-12-28 16:58:36 +08:00
|
|
|
return false
|
2020-12-21 16:18:08 +08:00
|
|
|
}
|
2020-12-28 16:46:25 +08:00
|
|
|
|
|
|
|
|
func removeEmpty(s string) string {
|
2020-12-28 16:49:36 +08:00
|
|
|
return regexp.MustCompile(`[\t\r\n]+`).ReplaceAllString(strings.TrimSpace(s), "\n")
|
2020-12-28 16:46:25 +08:00
|
|
|
}
|