From 1428dcd4273791d5e836b66eda073e6742e629fb Mon Sep 17 00:00:00 2001 From: shy Date: Thu, 12 Oct 2023 20:14:15 +0800 Subject: [PATCH] opt log --- base/log/log.go | 6 ++++++ base/mdb/hash.go | 5 ++--- base/ssh/script.go | 2 +- base/web/serve.go | 13 ++++++++----- core/code/code.go | 1 + exec.go | 10 ++++++---- logs.go | 25 +++++++++---------------- type.go | 4 ++-- 8 files changed, 35 insertions(+), 31 deletions(-) diff --git a/base/log/log.go b/base/log/log.go index e7e3fc33..a3c10f63 100644 --- a/base/log/log.go +++ b/base/log/log.go @@ -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, "-") diff --git a/base/mdb/hash.go b/base/mdb/hash.go index e33bbb1a..160668ef 100644 --- a/base/mdb/hash.go +++ b/base/mdb/hash.go @@ -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() diff --git a/base/ssh/script.go b/base/ssh/script.go index 97652762..f879f829 100644 --- a/base/ssh/script.go +++ b/base/ssh/script.go @@ -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 } diff --git a/base/web/serve.go b/base/web/serve.go index a4ad0d9b..5273c86a 100644 --- a/base/web/serve.go +++ b/base/web/serve.go @@ -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 ( diff --git a/core/code/code.go b/core/code/code.go index 3bc009d4..b93627b8 100644 --- a/core/code/code.go +++ b/core/code/code.go @@ -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...)) } diff --git a/exec.go b/exec.go index 4472c750..df9e5688 100644 --- a/exec.go +++ b/exec.go @@ -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 { diff --git a/logs.go b/logs.go index 3bea180c..01b78ca1 100644 --- a/logs.go +++ b/logs.go @@ -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()))) diff --git a/type.go b/type.go index 3b220e81..716e3ee8 100644 --- a/type.go +++ b/type.go @@ -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...) }) }