feat(recycler): add error cecycler (#10)

This commit is contained in:
Li4n0
2021-04-26 16:31:32 +08:00
committed by GitHub
parent 7e99d93e43
commit 3d8507e682
19 changed files with 263 additions and 67 deletions
+1 -1
View File
@@ -41,7 +41,7 @@ func (a *Announcer) AddBot(b Bot) *Announcer {
func Notice(r record.Record) {
for _, bot := range announcer.Bots {
if err := bot.notice(r); err != nil {
log.Error(err.Error())
log.Warn(err.Error())
}
}
}
+2 -2
View File
@@ -18,7 +18,7 @@ func init() {
if os.IsNotExist(err) {
err := download()
if err != nil {
log.Error("Download qqwry.dat failed, caused by:%v, recommend to download it by yourself otherwise the `IpArea` will be null", err)
log.Warn("Download qqwry.dat failed, caused by:%v, recommend to download it by yourself otherwise the `IpArea` will be null", err)
}
}
} else if time.Until(info.ModTime()) > 5*24*time.Hour {
@@ -36,7 +36,7 @@ func GetQQWry() *qqwry.QQwry {
init := qqwry.DatData.InitDatFile()
if v, ok := init.(error); ok {
if v != nil {
log.Error("qqwry init failed")
log.Warn("qqwry init failed")
wry = nil
}
}
+83
View File
@@ -0,0 +1,83 @@
package recycler
import (
"bytes"
"fmt"
"io/ioutil"
"runtime"
log "unknwon.dev/clog/v2"
)
var (
dunno = []byte("???")
centerDot = []byte("·")
dot = []byte(".")
slash = []byte("/")
)
func Recycle(err interface{}) {
stack := string(stack(3))
log.Error("%v\r\n%v", err, stack)
}
// stack returns a nicely formatted stack frame, skipping skip frames.
func stack(skip int) []byte {
buf := new(bytes.Buffer) // the returned data
// As we loop, we open files and read them. These variables record the currently
// loaded file.
var lines [][]byte
var lastFile string
for i := skip; ; i++ { // Skip the expected number of frames
pc, file, line, ok := runtime.Caller(i)
if !ok {
break
}
// Print this much at least. If we can't find the source, it won't show.
_, _ = fmt.Fprintf(buf, "%s:%d (0x%x)\n", file, line, pc)
if file != lastFile {
data, err := ioutil.ReadFile(file)
if err != nil {
continue
}
lines = bytes.Split(data, []byte{'\n'})
lastFile = file
}
_, _ = fmt.Fprintf(buf, "\t%s: %s\n", function(pc), source(lines, line))
}
return buf.Bytes()
}
// source returns a space-trimmed slice of the n'th line.
func source(lines [][]byte, n int) []byte {
n-- // in stack trace, lines are 1-indexed but our array is 0-indexed
if n < 0 || n >= len(lines) {
return dunno
}
return bytes.TrimSpace(lines[n])
}
// function returns, if possible, the name of the function containing the PC.
func function(pc uintptr) []byte {
fn := runtime.FuncForPC(pc)
if fn == nil {
return dunno
}
name := []byte(fn.Name())
// The name includes the path name to the package, which is unnecessary
// since the file name is already included. Plus, it has center dots.
// That is, we see
// runtime/debug.*T·ptrmethod
// and want
// *T.ptrmethod
// Also the package path might contains dot (e.g. code.google.com/...),
// so first eliminate the path prefix
if lastSlash := bytes.LastIndex(name, slash); lastSlash >= 0 {
name = name[lastSlash+1:]
}
if period := bytes.Index(name, dot); period >= 0 {
name = name[period+1:]
}
name = bytes.Replace(name, centerDot, dot, -1)
return name
}
+1 -1
View File
@@ -35,7 +35,7 @@ func (br BaseRule) Match(s string) (flag, flagGroup string, vars map[string]stri
return
} else {
if catcher, err := regexp.Compile(br.FlagFormat); err != nil {
log.Error("%s[rule:%s]", err, br.Name)
log.Warn("%s[rule:%s]", err, br.Name)
return
} else {
br.flagCatcher = catcher