1
0
forked from x/icebergs
This commit is contained in:
IT 老营长 @云轩领航-创始人 2023-10-12 20:14:15 +08:00
parent 84109ec520
commit 1428dcd427
8 changed files with 35 additions and 31 deletions

View File

@ -111,6 +111,10 @@ var Index = &ice.Context{Name: LOG, Help: "日志模块", Configs: ice.Configs{
func init() { ice.Index.Register(Index, &Frame{}, TAIL) }
func init() {
ice.Info.Traceid = "short"
ice.Pulse.Option(ice.LOG_TRACEID, Traceid())
}
func Traceid() (traceid string) {
ls := []string{}
kit.For(kit.Split(ice.Info.Traceid), func(key string) {
@ -121,6 +125,8 @@ func Traceid() (traceid string) {
ls = append(ls, kit.Hashs(mdb.UNIQ))
case "node":
ls = append(ls, ice.Info.NodeName)
case "hide":
ls = ls[:0]
}
})
return strings.Join(ls, "-")

View File

@ -112,10 +112,9 @@ func _hash_export(m *ice.Message, prefix, chain, file string) {
func _hash_import(m *ice.Message, prefix, chain, file string) {
defer Lock(m, prefix)()
f, e := ice.Info.Open(m, kit.Keys(file, JSON))
if os.IsNotExist(e) {
if e != nil && !ice.Info.Important {
return
}
if m.Warn(e) {
} else if m.Warn(e) {
return
}
defer f.Close()

View File

@ -107,7 +107,7 @@ func (f *Frame) scan(m *ice.Message, h, line string) *Frame {
f.ps2 = kit.Simple(mdb.Confv(m, PROMPT, kit.Keym(PS2)))
ps, bio := f.ps1, bufio.NewScanner(f.stdin)
m.I, m.O = f.stdin, f.stdout
for f.prompt(m, ps...); f.stdin != nil && bio.Scan(); f.prompt(m, ps...) {
for f.prompt(m.Sleep300ms(), ps...); f.stdin != nil && bio.Scan(); f.prompt(m, ps...) {
if len(bio.Text()) == 0 && h == STDIO {
continue
}

View File

@ -5,6 +5,7 @@ import (
"net/url"
"path"
"regexp"
"runtime"
"strings"
ice "shylinux.com/x/icebergs"
@ -23,14 +24,16 @@ import (
func _serve_address(m *ice.Message) string { return Domain(tcp.LOCALHOST, m.Option(tcp.PORT)) }
func _serve_start(m *ice.Message) {
defer kit.For(kit.Split(m.Option(ice.DEV)), func(v string) {
m.Sleep30ms().Cmd(SPACE, tcp.DIAL, ice.DEV, v, mdb.NAME, ice.Info.NodeName, m.OptionSimple(TOKEN))
})
kit.If(m.Option(tcp.PORT) == tcp.RANDOM, func() { m.Option(tcp.PORT, m.Cmdx(tcp.PORT, aaa.RIGHT)) })
kit.If(m.Option(aaa.USERNAME), func() { aaa.UserRoot(m, m.Option(aaa.USERNICK), m.Option(aaa.USERNAME)) })
m.Go(func() { m.Cmd(SPIDE, ice.OPS, _serve_address(m)+"/exit", ice.Maps{CLIENT_TIMEOUT: "30ms"}) }).Sleep30ms()
kit.If(m.Option(tcp.PORT) == tcp.RANDOM, func() { m.Option(tcp.PORT, m.Cmdx(tcp.PORT, aaa.RIGHT)) })
kit.If(runtime.GOOS == cli.WINDOWS || m.Cmdx(cli.SYSTEM, "lsof", "-i", ":"+m.Option(tcp.PORT)) != "", func() {
m.Go(func() { m.Cmd(SPIDE, ice.OPS, _serve_address(m)+"/exit", ice.Maps{CLIENT_TIMEOUT: "30ms"}) }).Sleep30ms()
})
cli.NodeInfo(m, kit.Select(ice.Info.Hostname, m.Option(tcp.NODENAME)), SERVER)
m.Start("", m.OptionSimple(tcp.HOST, tcp.PORT)...)
kit.For(kit.Split(m.Option(ice.DEV)), func(dev string) {
m.Sleep30ms(SPACE, tcp.DIAL, ice.DEV, dev, mdb.NAME, ice.Info.NodeName, m.OptionSimple(TOKEN))
})
}
func _serve_main(m *ice.Message, w http.ResponseWriter, r *http.Request) bool {
const (

View File

@ -14,6 +14,7 @@ func init() {
web.Index.Register(Index, &web.Frame{},
WEBPACK, BINPACK, AUTOGEN, COMPILE, PUBLISH, UPGRADE, INSTALL,
XTERM, INNER, VIMER, BENCH, PPROF,
TEMPLATE, COMPLETE, NAVIGATE,
)
}
func Prefix(arg ...ice.Any) string { return web.Prefix(CODE, kit.Keys(arg...)) }

10
exec.go
View File

@ -18,8 +18,8 @@ func (m *Message) TryCatch(catch bool, cb ...func(*Message)) {
case io.EOF, nil:
default:
fileline := m.FormatStack(2, 1)
m.Log(LOG_WARN, "catch: %s %s", e, fileline).Log("chain", m.FormatChain())
m.Log(LOG_WARN, "catch: %s %s", e, kit.FileLine(4, 10)).Log("stack", m.FormatStack(2, 1000))
m.Log(LOG_WARN, "catch: %s %s", e, fileline).Log("chain", "\n"+m.FormatChain())
m.Log(LOG_WARN, "catch: %s %s", e, kit.FileLine(4, 10)).Log("stack", "\n"+m.FormatStack(2, 1000))
m.Log(LOG_WARN, "catch: %s %s", e, fileline).Result(ErrWarn, e, SP, m.FormatStack(2, 5))
if len(cb) > 1 {
m.TryCatch(catch, cb[1:]...)
@ -61,8 +61,10 @@ func (m *Message) GoSleep(t string, arg ...Any) {
m.Go(func() { m.Spawn(kit.Dict(MSG_COUNT, "0")).Sleep(t).Cmd(arg...) })
}
func (m *Message) Go(cb func(), arg ...Any) *Message {
kit.If(len(arg) == 0, func() { arg = append(arg, logs.FileLine(cb)) })
task.Put(m.FormatTaskMeta(), arg[0], func(task *task.Task) { m.TryCatch(true, func(m *Message) { cb() }) })
meta := m.FormatTaskMeta()
meta.FileLine = kit.FileLine(2, 3)
kit.If(len(arg) > 0, func() { meta.FileLine = kit.Format(arg[0]) })
task.Put(meta, nil, func(task *task.Task) { m.TryCatch(true, func(m *Message) { cb() }) })
return m
}
func (m *Message) GoWait(cb func(func()), arg ...Any) *Message {

25
logs.go
View File

@ -53,15 +53,11 @@ func (m *Message) log(level string, str string, arg ...Any) *Message {
if m.Option(LOG_DISABLE) == TRUE {
return m
}
arg = append(arg, logs.TraceidMeta(m.Option(LOG_TRACEID)))
args, traceid := []Any{}, ""
for _, v := range arg {
switch v := v.(type) {
case logs.Meta:
if v.Key == logs.TRACEID {
traceid = kit.Select(strings.TrimSpace(v.Value), traceid)
continue
}
if v, ok := v.(logs.Meta); ok && v.Key == logs.TRACEID {
traceid = kit.Select(strings.TrimSpace(v.Value), traceid)
continue
}
args = append(args, v)
}
@ -78,8 +74,7 @@ func (m *Message) log(level string, str string, arg ...Any) *Message {
prefix, suffix = "\033[31m", "\033[0m"
}
}
kit.If(traceid, func() { traceid = kit.Format("%s: %s ", logs.TRACEID, traceid) })
logs.Infof(str, append(args, logs.PrefixMeta(kit.Format("%s%02d %4s->%-4s %s%s ", traceid, m.code, m.source.Name, m.target.Name, prefix, level)), logs.SuffixMeta(suffix), _source)...)
logs.Infof(str, append(args, logs.PrefixMeta(kit.Format("%s %s%s ", m.FormatShip(traceid), prefix, level)), logs.SuffixMeta(suffix), _source)...)
return m
}
func (m *Message) Log(level string, str string, arg ...Any) *Message {
@ -168,17 +163,15 @@ func (m *Message) Debug(str string, arg ...Any) {
}
func (m *Message) FormatTaskMeta() task.Meta {
_traceid := ""
kit.If(m.Option(LOG_TRACEID), func(traceid string) { _traceid = kit.Format("%s: %s ", logs.TRACEID, traceid) })
return task.Meta{
Prefix: kit.Format("%s%d %4s->%-4s ", _traceid, m.code, m.source.Name, m.target.Name),
FileLine: kit.FileLine(2, 3),
}
return task.Meta{Prefix: m.FormatShip() + " ", FileLine: kit.FileLine(2, 3)}
}
func (m *Message) FormatPrefix(traceid ...string) string {
return kit.Format("%s %s", logs.FmtTime(logs.Now()), m.FormatShip(traceid...))
}
func (m *Message) FormatShip(traceid ...string) string {
_traceid := ""
kit.If(kit.Select(m.Option(LOG_TRACEID), traceid, 0), func(traceid string) { _traceid = kit.Format("%s: %s ", logs.TRACEID, traceid) })
return kit.Format("%s %s%d %s->%s", logs.FmtTime(logs.Now()), _traceid, m.code, m.source.Name, m.target.Name)
return kit.Format("%s%02d %4s->%-4s", _traceid, m.code, m.source.Name, m.target.Name)
}
func (m *Message) FormatSize() string {
return kit.Format("%dx%d %v %v", m.Length(), len(m.value(MSG_APPEND)), kit.Simple(m.value(MSG_APPEND)), kit.FmtSize(len(m.Result())))

View File

@ -193,11 +193,11 @@ func (c *Context) Begin(m *Message, arg ...string) *Context {
return c.Merge(c)
}
func (c *Context) Start(m *Message, arg ...string) {
m.Log(CTX_START, c.Prefix())
m.Log(CTX_START, c.Prefix(), logs.FileLineMeta(2))
kit.If(c.server != nil, func() { c.server.Start(m, arg...) })
}
func (c *Context) Close(m *Message, arg ...string) {
m.Log(CTX_CLOSE, c.Prefix())
m.Log(CTX_CLOSE, c.Prefix(), logs.FileLineMeta(2))
kit.If(c.server != nil, func() { c.server.Close(m, arg...) })
}