1
0
forked from x/icebergs
icebergs/base/log/debug.go
2023-03-30 08:34:49 +08:00

86 lines
3.1 KiB
Go

package log
import (
"strings"
"unicode"
ice "shylinux.com/x/icebergs"
"shylinux.com/x/icebergs/base/ctx"
"shylinux.com/x/icebergs/base/mdb"
"shylinux.com/x/icebergs/base/nfs"
kit "shylinux.com/x/toolkits"
)
const DEBUG = "debug"
func init() {
Index.MergeCommands(ice.Commands{
DEBUG: {Name: "debug level=error,bench,debug,error,watch offset filter auto reset doc", Help: "后台日志", Actions: ice.Actions{
"doc": {Help: "文档", Hand: func(m *ice.Message, arg ...string) { m.ProcessOpen("https://pkg.go.dev/std") }},
"reset": {Help: "文档", Hand: func(m *ice.Message, arg ...string) {
m.Cmd(nfs.CAT, ice.VAR_LOG+arg[0]+".log", func(line string, index int) {
m.ProcessRewrite("offset", index+2)
})
}},
}, Hand: func(m *ice.Message, arg ...string) {
offset, stats := kit.Int(kit.Select("0", arg, 1)), map[string]int{}
switch arg[0] {
case BENCH, ERROR, DEBUG:
m.Cmd(nfs.CAT, ice.VAR_LOG+arg[0]+".log", func(line string, index int) {
if len(arg) > 2 && !strings.Contains(line, arg[2]) || index < offset {
return
}
ls := strings.SplitN(line, ice.SP, 6)
if len(ls) < 6 {
return
}
m.Push(mdb.TIME, ls[0]+ice.SP+ls[1]).Push(mdb.ID, ls[2])
i := strings.LastIndex(ls[5], ice.SP)
if strings.HasPrefix(ls[5][i+1:], ice.BASE) || strings.HasPrefix(ls[5][i+1:], ice.CORE) || strings.HasPrefix(ls[5][i+1:], ice.MISC) {
m.Push(nfs.PATH, ice.USR_ICEBERGS)
m.Push(nfs.FILE, strings.TrimSpace(strings.Split(ls[5][i:], ice.DF)[0]))
m.Push(nfs.LINE, strings.TrimSpace(strings.Split(ls[5][i:], ice.DF)[1]))
ls[5] = ls[5][:i]
} else if strings.HasPrefix(ls[5][i+1:], ice.USR_ICEBERGS) {
m.Push(nfs.PATH, ice.USR_ICEBERGS)
m.Push(nfs.FILE, strings.TrimPrefix(strings.TrimSpace(strings.Split(ls[5][i:], ice.DF)[0]), ice.USR_ICEBERGS))
m.Push(nfs.LINE, strings.TrimSpace(strings.Split(ls[5][i:], ice.DF)[1]))
ls[5] = ls[5][:i]
} else {
m.Push(nfs.PATH, ice.USR_ICEBERGS)
m.Push(nfs.FILE, "init.go")
m.Push(nfs.LINE, "90")
}
if ls[4] == ice.LOG_CMDS {
_ls := strings.SplitN(ls[5], ice.SP, 2)
ls[4] = _ls[0]
ls[5] = _ls[1]
if !unicode.IsDigit(rune(ls[5][0])) {
_ls := strings.SplitN(ls[5], ice.SP, 2)
ls[4] += ice.SP + _ls[0]
ls[5] = _ls[1]
}
}
m.Push(ctx.SHIP, ls[3]).Push(ctx.ACTION, ls[4]).Push(nfs.CONTENT, ls[5])
stats[ls[4]]++
})
case WATCH:
m.Cmd(nfs.CAT, ice.VAR_LOG+arg[0]+".log", func(line string, index int) {
if len(arg) > 2 && !strings.Contains(line, arg[2]) || index < offset {
return
}
ls := strings.SplitN(line, ice.SP, 6)
m.Push(mdb.TIME, ls[0]+ice.SP+ls[1]).Push(mdb.ID, ls[2])
i := strings.LastIndex(ls[5], ice.SP)
m.Push(nfs.PATH, ice.USR_ICEBERGS)
m.Push(nfs.FILE, strings.TrimSpace(strings.Split(ls[5][i:], ice.DF)[0]))
m.Push(nfs.LINE, strings.TrimSpace(strings.Split(ls[5][i:], ice.DF)[1]))
m.Push(ctx.SHIP, ls[3]).Push(ctx.ACTION, ls[4]).Push(nfs.CONTENT, ls[5][:i])
stats[ls[4]]++
})
}
m.StatusTimeCountTotal(offset+m.Length(), stats)
}},
})
}