From 00c61c544c51bedf50ff1f633cb03eaf34a595e7 Mon Sep 17 00:00:00 2001 From: shaoying Date: Thu, 10 Jan 2019 08:38:04 +0800 Subject: [PATCH] add msg.format.summary --- src/contexts/ctx/ctx.go | 95 +++++++++++++++++++++-------------------- src/contexts/log/log.go | 2 +- src/contexts/nfs/nfs.go | 18 ++++---- src/contexts/ssh/ssh.go | 3 +- src/contexts/tcp/tcp.go | 16 ++----- 5 files changed, 64 insertions(+), 70 deletions(-) diff --git a/src/contexts/ctx/ctx.go b/src/contexts/ctx/ctx.go index 9ec32525..e5992536 100644 --- a/src/contexts/ctx/ctx.go +++ b/src/contexts/ctx/ctx.go @@ -386,20 +386,60 @@ func (m *Message) Target() *Context { func (m *Message) Message() *Message { return m.message } -func (m *Message) Format(arg ...string) string { +func (m *Message) Format(arg ...interface{}) string { if len(arg) == 0 { arg = append(arg, "time", "ship") } meta := []string{} for _, v := range arg { - switch v { + switch kit.Format(v) { + case "summary": + msg := arg[1].(*Message) + ms := make([]*Message, 0, 1024) + ms = append(ms, msg.message, msg) + + for i := 0; i < len(ms); i++ { + msg := ms[i] + if m.Add("append", "index", i); msg == nil { + m.Add("append", "message", "") + m.Add("append", "time", "") + m.Add("append", "code", "") + m.Add("append", "source", "") + m.Add("append", "target", "") + m.Add("append", "details", "") + m.Add("append", "options", "") + continue + } + + if msg.message != nil { + m.Add("append", "message", msg.message.code) + } else { + m.Add("append", "message", "") + } + m.Add("append", "time", msg.time.Format("15:04:05")) + m.Add("append", "code", msg.code) + m.Add("append", "source", msg.source.Name) + m.Add("append", "target", msg.target.Name) + m.Add("append", "details", fmt.Sprintf("%v", msg.Meta["detail"])) + m.Add("append", "options", fmt.Sprintf("%v", msg.Meta["option"])) + + if i == 0 { + continue + } + + if len(ms) < 30 && len(arg) > 2 && arg[2] == "deep" { + ms = append(ms, ms[i].messages...) + } + } + m.Table() case "time": meta = append(meta, m.Time()) case "code": meta = append(meta, kit.Format(m.code)) case "ship": meta = append(meta, fmt.Sprintf("%d(%s->%s)", m.code, m.source.Name, m.target.Name)) + case "detail": meta = append(meta, fmt.Sprintf("%v", m.Meta["detail"])) case "option": @@ -408,6 +448,7 @@ func (m *Message) Format(arg ...string) string { meta = append(meta, fmt.Sprintf("%v", m.Meta["append"])) case "result": meta = append(meta, fmt.Sprintf("%v", m.Meta["result"])) + case "full": case "chain": ms := []*Message{} @@ -458,7 +499,7 @@ func (m *Message) Format(arg ...string) string { } default: - meta = append(meta, kit.FileName(v, "time")) + meta = append(meta, kit.FileName(kit.Format(v), "time")) } } return strings.Join(meta, " ") @@ -1658,17 +1699,12 @@ var Index = &Context{Name: "ctx", Help: "模块中心", Server: &CTX{}, if len(arg) > 0 { switch arg[0] { - case "full": - m.Echo(m.Format("full")) - return - case "back": - m.Echo(m.Format("back")) - return - case "stack": - m.Echo(m.Format("stack")) + case "time", "code", "ship", "full", "chain", "stack": + m.Echo(m.Format(arg[0])) return } } + if len(arg) > 0 && arg[0] == "spawn" { sub := msg.Spawn() m.Echo("%d", sub.code) @@ -1681,41 +1717,8 @@ var Index = &Context{Name: "ctx", Help: "模块中心", Server: &CTX{}, return } - if msg.message != nil { - m.Add("append", "time", msg.message.time.Format("15:04:05")) - m.Add("append", "code", msg.message.code) - m.Add("append", "source", msg.message.source.Name) - m.Add("append", "target", msg.message.target.Name) - if msg.message.Meta != nil { - m.Add("append", "details", fmt.Sprintf("%v", msg.message.Meta["detail"])) - m.Add("append", "options", fmt.Sprintf("%v", msg.message.Meta["option"])) - } else { - m.Add("append", "details", "") - m.Add("append", "options", "") - } - } else { - m.Add("append", "time", "") - m.Add("append", "code", "") - m.Add("append", "source", "") - m.Add("append", "target", "") - m.Add("append", "details", "") - m.Add("append", "options", "") - } - m.Add("append", "time", msg.time.Format("15:04:05")) - m.Add("append", "code", msg.code) - m.Add("append", "source", msg.source.Name) - m.Add("append", "target", msg.target.Name) - m.Add("append", "details", fmt.Sprintf("%v", msg.Meta["detail"])) - m.Add("append", "options", fmt.Sprintf("%v", msg.Meta["option"])) - for _, v := range msg.messages { - m.Add("append", "time", v.time.Format("15:04:05")) - m.Add("append", "code", v.code) - m.Add("append", "source", v.source.Name) - m.Add("append", "target", v.target.Name) - m.Add("append", "details", fmt.Sprintf("%v", v.Meta["detail"])) - m.Add("append", "options", fmt.Sprintf("%v", v.Meta["option"])) - } - m.Table() + m.Format("summary", msg, "deep") + msg.CopyTo(m) return }}, "detail": &Command{Name: "detail [index] [value...]", Help: "查看或添加参数", Hand: func(m *Message, c *Context, key string, arg ...string) (e error) { diff --git a/src/contexts/log/log.go b/src/contexts/log/log.go index 14e4f884..b0af0a17 100644 --- a/src/contexts/log/log.go +++ b/src/contexts/log/log.go @@ -52,7 +52,7 @@ func (log *LOG) Log(msg *ctx.Message, action string, str string, arg ...interfac } } - fmt.Fprintln(file, fmt.Sprintf("%d %s %s%s %s%s", m.Capi("nout", 1), msg.Format(kit.Trans(value["meta"])...), + fmt.Fprintln(file, fmt.Sprintf("%d %s %s%s %s%s", m.Capi("nout", 1), msg.Format(value["meta"].([]interface{})...), kit.Format(value["color_begin"]), action, fmt.Sprintf(str, arg...), kit.Format(value["color_end"]))) return } diff --git a/src/contexts/nfs/nfs.go b/src/contexts/nfs/nfs.go index d8146351..bc3f6917 100644 --- a/src/contexts/nfs/nfs.go +++ b/src/contexts/nfs/nfs.go @@ -157,9 +157,7 @@ func open(m *ctx.Message, name string, arg ...int) (string, *os.File, error) { func (nfs *NFS) Term(msg *ctx.Message, action string, args ...interface{}) *NFS { m := nfs.Context.Message() - // if action != "print" { - m.Log("term", "%s %v", action, args) - // } + m.Log("debug", "%s %v", action, args) switch action { case "init": @@ -182,7 +180,6 @@ func (nfs *NFS) Term(msg *ctx.Message, action string, args ...interface{}) *NFS y := m.Confi("term", "cursor_y") bg := termbox.Attribute(msg.Confi("term", "bgcolor")) fg := termbox.Attribute(msg.Confi("term", "fgcolor")) - m.Log("fuck", "what %v %v", bg, fg) begin_row := m.Confi("term", "begin_row") begin_col := m.Confi("term", "begin_col") @@ -252,7 +249,7 @@ func (nfs *NFS) Term(msg *ctx.Message, action string, args ...interface{}) *NFS if len(args) > 0 { n = kit.Int(args[0]) } - m.Log("term", "<<<< scroll page (%v, %v)", begin_row, begin_col) + m.Log("debug", "<<<< scroll page (%v, %v)", begin_row, begin_col) // 向下滚动 for i := begin_row; n > 0 && i < m.Capi("noutput"); i++ { @@ -297,7 +294,7 @@ func (nfs *NFS) Term(msg *ctx.Message, action string, args ...interface{}) *NFS } } - m.Log("term", ">>>> scroll page (%v, %v)", begin_row, begin_col) + m.Log("debug", ">>>> scroll page (%v, %v)", begin_row, begin_col) m.Conf("term", "begin_row", begin_row) m.Conf("term", "begin_col", begin_col) @@ -334,7 +331,6 @@ func (nfs *NFS) Term(msg *ctx.Message, action string, args ...interface{}) *NFS } if y >= bottom { - m.Log("fuck", "scroll %v", m.Option("scroll")) if !m.Options("scroll") { nfs.Term(m, "scroll") } @@ -406,7 +402,7 @@ func (nfs *NFS) Read(p []byte) (n int, err error) { } if v := m.Conf("input", []interface{}{which, "line"}); v != "" { what = []rune(v) - m.Log("term", "what %v %v", which, what) + m.Log("debug", "what %v %v", which, what) nfs.prompt(what) } case termbox.KeyCtrlN: @@ -415,7 +411,7 @@ func (nfs *NFS) Read(p []byte) (n int, err error) { } if v := m.Conf("input", []interface{}{which, "line"}); v != "" { what = []rune(v) - m.Log("term", "what %v %v", which, what) + m.Log("debug", "what %v %v", which, what) nfs.prompt(what) } @@ -577,7 +573,7 @@ func (nfs *NFS) printf(arg ...interface{}) *NFS { if !strings.HasSuffix(line, "\n") { line += "\n" } - m.Log("term", "noutput %s", m.Cap("noutput", m.Capi("noutput")+1)) + m.Log("debug", "noutput %s", m.Cap("noutput", m.Capi("noutput")+1)) m.Confv("output", -2, map[string]interface{}{"time": time.Now().Unix(), "line": line}) if m.Caps("termbox") { @@ -1268,6 +1264,7 @@ var Index = &ctx.Context{Name: "nfs", Help: "存储中心", if _, ok := m.Target().Server.(*NFS); m.Assert(ok) { //{{{ m.Sess("tcp").Call(func(sub *ctx.Message) *ctx.Message { sub.Start(fmt.Sprintf("file%d", m.Capi("nfile", 1)), "远程文件") + return sub.Sess("target", m.Source()).Call(func(sub1 *ctx.Message) *ctx.Message { nfs, _ := sub.Target().Server.(*NFS) sub1.Remote = make(chan bool, 1) @@ -1283,6 +1280,7 @@ var Index = &ctx.Context{Name: "nfs", Help: "存储中心", if _, ok := m.Target().Server.(*NFS); m.Assert(ok) { m.Sess("tcp").Call(func(sub *ctx.Message) *ctx.Message { sub.Start(fmt.Sprintf("file%d", m.Capi("nfile", 1)), "远程文件") + return sub.Sess("target", m.Source()).Call(func(sub1 *ctx.Message) *ctx.Message { nfs, _ := sub.Target().Server.(*NFS) sub1.Remote = make(chan bool, 1) diff --git a/src/contexts/ssh/ssh.go b/src/contexts/ssh/ssh.go index 273d3d94..dcd35394 100644 --- a/src/contexts/ssh/ssh.go +++ b/src/contexts/ssh/ssh.go @@ -46,9 +46,10 @@ var Index = &ctx.Context{Name: "ssh", Help: "集群中心", "listen": &ctx.Command{Name: "listen address [security [protocol]]", Help: "网络监听", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { m.Sess("nfs").Call(func(sub *ctx.Message) *ctx.Message { sub.Start(fmt.Sprintf("host%d", m.Capi("nhost", 1)), "远程主机") - sub.Spawn().Cmd("pwd", "") + // sub.Spawn().Cmd("pwd", "") return sub }, m.Meta["detail"]) + if !m.Caps("domain") { m.Cap("domain", m.Cap("hostname", m.Conf("hostname"))) } diff --git a/src/contexts/tcp/tcp.go b/src/contexts/tcp/tcp.go index 262b6d86..97c848fc 100644 --- a/src/contexts/tcp/tcp.go +++ b/src/contexts/tcp/tcp.go @@ -122,29 +122,19 @@ func (tcp *TCP) Start(m *ctx.Message, arg ...string) bool { return true } func (tcp *TCP) Close(m *ctx.Message, arg ...string) bool { - return false switch tcp.Context { case m.Target(): if tcp.Listener != nil { - m.Log("info", "%d close %v", m.Capi("nlisten", -1)+1, m.Cap("stream")) + m.Log("info", " close %v", m.Cap("stream")) tcp.Listener.Close() tcp.Listener = nil } if tcp.Conn != nil { - m.Log("info", "%d close %v", m.Capi("nclient", -1)+1, m.Cap("stream")) + m.Log("info", " close %v", m.Cap("stream")) tcp.Conn.Close() tcp.Conn = nil } case m.Source(): - if tcp.Conn != nil { - msg := m.Spawn(tcp.Context) - if !tcp.Context.Close(msg, arg...) { - return false - } - } - } - if m.Target() == Index { - return false } return true } @@ -192,9 +182,11 @@ var Index = &ctx.Context{Name: "tcp", Help: "网络中心", "ifconfig": &ctx.Command{Name: "ifconfig", Help: "网络配置", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { if ifs, e := net.Interfaces(); m.Assert(e) { for _, v := range ifs { + if ips, e := v.Addrs(); m.Assert(e) { for _, x := range ips { ip := x.String() + if !strings.Contains(ip, ":") && len(ip) > 0 && len(v.HardwareAddr) > 0 { m.Add("append", "index", v.Index) m.Add("append", "name", v.Name)