1
0
forked from x/ContextOS

add msg.format.summary

This commit is contained in:
shaoying 2019-01-10 08:38:04 +08:00
parent 9145fa6bc6
commit 00c61c544c
5 changed files with 64 additions and 70 deletions

View File

@ -386,20 +386,60 @@ func (m *Message) Target() *Context {
func (m *Message) Message() *Message { func (m *Message) Message() *Message {
return m.message return m.message
} }
func (m *Message) Format(arg ...string) string { func (m *Message) Format(arg ...interface{}) string {
if len(arg) == 0 { if len(arg) == 0 {
arg = append(arg, "time", "ship") arg = append(arg, "time", "ship")
} }
meta := []string{} meta := []string{}
for _, v := range arg { 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": case "time":
meta = append(meta, m.Time()) meta = append(meta, m.Time())
case "code": case "code":
meta = append(meta, kit.Format(m.code)) meta = append(meta, kit.Format(m.code))
case "ship": case "ship":
meta = append(meta, fmt.Sprintf("%d(%s->%s)", m.code, m.source.Name, m.target.Name)) meta = append(meta, fmt.Sprintf("%d(%s->%s)", m.code, m.source.Name, m.target.Name))
case "detail": case "detail":
meta = append(meta, fmt.Sprintf("%v", m.Meta["detail"])) meta = append(meta, fmt.Sprintf("%v", m.Meta["detail"]))
case "option": case "option":
@ -408,6 +448,7 @@ func (m *Message) Format(arg ...string) string {
meta = append(meta, fmt.Sprintf("%v", m.Meta["append"])) meta = append(meta, fmt.Sprintf("%v", m.Meta["append"]))
case "result": case "result":
meta = append(meta, fmt.Sprintf("%v", m.Meta["result"])) meta = append(meta, fmt.Sprintf("%v", m.Meta["result"]))
case "full": case "full":
case "chain": case "chain":
ms := []*Message{} ms := []*Message{}
@ -458,7 +499,7 @@ func (m *Message) Format(arg ...string) string {
} }
default: default:
meta = append(meta, kit.FileName(v, "time")) meta = append(meta, kit.FileName(kit.Format(v), "time"))
} }
} }
return strings.Join(meta, " ") return strings.Join(meta, " ")
@ -1658,17 +1699,12 @@ var Index = &Context{Name: "ctx", Help: "模块中心", Server: &CTX{},
if len(arg) > 0 { if len(arg) > 0 {
switch arg[0] { switch arg[0] {
case "full": case "time", "code", "ship", "full", "chain", "stack":
m.Echo(m.Format("full")) m.Echo(m.Format(arg[0]))
return
case "back":
m.Echo(m.Format("back"))
return
case "stack":
m.Echo(m.Format("stack"))
return return
} }
} }
if len(arg) > 0 && arg[0] == "spawn" { if len(arg) > 0 && arg[0] == "spawn" {
sub := msg.Spawn() sub := msg.Spawn()
m.Echo("%d", sub.code) m.Echo("%d", sub.code)
@ -1681,41 +1717,8 @@ var Index = &Context{Name: "ctx", Help: "模块中心", Server: &CTX{},
return return
} }
if msg.message != nil { m.Format("summary", msg, "deep")
m.Add("append", "time", msg.message.time.Format("15:04:05")) msg.CopyTo(m)
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()
return return
}}, }},
"detail": &Command{Name: "detail [index] [value...]", Help: "查看或添加参数", Hand: func(m *Message, c *Context, key string, arg ...string) (e error) { "detail": &Command{Name: "detail [index] [value...]", Help: "查看或添加参数", Hand: func(m *Message, c *Context, key string, arg ...string) (e error) {

View File

@ -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"]))) kit.Format(value["color_begin"]), action, fmt.Sprintf(str, arg...), kit.Format(value["color_end"])))
return return
} }

View File

@ -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 { func (nfs *NFS) Term(msg *ctx.Message, action string, args ...interface{}) *NFS {
m := nfs.Context.Message() m := nfs.Context.Message()
// if action != "print" { m.Log("debug", "%s %v", action, args)
m.Log("term", "%s %v", action, args)
// }
switch action { switch action {
case "init": case "init":
@ -182,7 +180,6 @@ func (nfs *NFS) Term(msg *ctx.Message, action string, args ...interface{}) *NFS
y := m.Confi("term", "cursor_y") y := m.Confi("term", "cursor_y")
bg := termbox.Attribute(msg.Confi("term", "bgcolor")) bg := termbox.Attribute(msg.Confi("term", "bgcolor"))
fg := termbox.Attribute(msg.Confi("term", "fgcolor")) fg := termbox.Attribute(msg.Confi("term", "fgcolor"))
m.Log("fuck", "what %v %v", bg, fg)
begin_row := m.Confi("term", "begin_row") begin_row := m.Confi("term", "begin_row")
begin_col := m.Confi("term", "begin_col") 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 { if len(args) > 0 {
n = kit.Int(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++ { 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_row", begin_row)
m.Conf("term", "begin_col", begin_col) 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 { if y >= bottom {
m.Log("fuck", "scroll %v", m.Option("scroll"))
if !m.Options("scroll") { if !m.Options("scroll") {
nfs.Term(m, "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 != "" { if v := m.Conf("input", []interface{}{which, "line"}); v != "" {
what = []rune(v) what = []rune(v)
m.Log("term", "what %v %v", which, what) m.Log("debug", "what %v %v", which, what)
nfs.prompt(what) nfs.prompt(what)
} }
case termbox.KeyCtrlN: 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 != "" { if v := m.Conf("input", []interface{}{which, "line"}); v != "" {
what = []rune(v) what = []rune(v)
m.Log("term", "what %v %v", which, what) m.Log("debug", "what %v %v", which, what)
nfs.prompt(what) nfs.prompt(what)
} }
@ -577,7 +573,7 @@ func (nfs *NFS) printf(arg ...interface{}) *NFS {
if !strings.HasSuffix(line, "\n") { if !strings.HasSuffix(line, "\n") {
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}) m.Confv("output", -2, map[string]interface{}{"time": time.Now().Unix(), "line": line})
if m.Caps("termbox") { if m.Caps("termbox") {
@ -1268,6 +1264,7 @@ var Index = &ctx.Context{Name: "nfs", Help: "存储中心",
if _, ok := m.Target().Server.(*NFS); m.Assert(ok) { //{{{ if _, ok := m.Target().Server.(*NFS); m.Assert(ok) { //{{{
m.Sess("tcp").Call(func(sub *ctx.Message) *ctx.Message { m.Sess("tcp").Call(func(sub *ctx.Message) *ctx.Message {
sub.Start(fmt.Sprintf("file%d", m.Capi("nfile", 1)), "远程文件") sub.Start(fmt.Sprintf("file%d", m.Capi("nfile", 1)), "远程文件")
return sub.Sess("target", m.Source()).Call(func(sub1 *ctx.Message) *ctx.Message { return sub.Sess("target", m.Source()).Call(func(sub1 *ctx.Message) *ctx.Message {
nfs, _ := sub.Target().Server.(*NFS) nfs, _ := sub.Target().Server.(*NFS)
sub1.Remote = make(chan bool, 1) 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) { if _, ok := m.Target().Server.(*NFS); m.Assert(ok) {
m.Sess("tcp").Call(func(sub *ctx.Message) *ctx.Message { m.Sess("tcp").Call(func(sub *ctx.Message) *ctx.Message {
sub.Start(fmt.Sprintf("file%d", m.Capi("nfile", 1)), "远程文件") sub.Start(fmt.Sprintf("file%d", m.Capi("nfile", 1)), "远程文件")
return sub.Sess("target", m.Source()).Call(func(sub1 *ctx.Message) *ctx.Message { return sub.Sess("target", m.Source()).Call(func(sub1 *ctx.Message) *ctx.Message {
nfs, _ := sub.Target().Server.(*NFS) nfs, _ := sub.Target().Server.(*NFS)
sub1.Remote = make(chan bool, 1) sub1.Remote = make(chan bool, 1)

View File

@ -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) { "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 { m.Sess("nfs").Call(func(sub *ctx.Message) *ctx.Message {
sub.Start(fmt.Sprintf("host%d", m.Capi("nhost", 1)), "远程主机") sub.Start(fmt.Sprintf("host%d", m.Capi("nhost", 1)), "远程主机")
sub.Spawn().Cmd("pwd", "") // sub.Spawn().Cmd("pwd", "")
return sub return sub
}, m.Meta["detail"]) }, m.Meta["detail"])
if !m.Caps("domain") { if !m.Caps("domain") {
m.Cap("domain", m.Cap("hostname", m.Conf("hostname"))) m.Cap("domain", m.Cap("hostname", m.Conf("hostname")))
} }

View File

@ -122,29 +122,19 @@ func (tcp *TCP) Start(m *ctx.Message, arg ...string) bool {
return true return true
} }
func (tcp *TCP) Close(m *ctx.Message, arg ...string) bool { func (tcp *TCP) Close(m *ctx.Message, arg ...string) bool {
return false
switch tcp.Context { switch tcp.Context {
case m.Target(): case m.Target():
if tcp.Listener != nil { 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.Close()
tcp.Listener = nil tcp.Listener = nil
} }
if tcp.Conn != 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.Close()
tcp.Conn = nil tcp.Conn = nil
} }
case m.Source(): 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 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) { "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) { if ifs, e := net.Interfaces(); m.Assert(e) {
for _, v := range ifs { for _, v := range ifs {
if ips, e := v.Addrs(); m.Assert(e) { if ips, e := v.Addrs(); m.Assert(e) {
for _, x := range ips { for _, x := range ips {
ip := x.String() ip := x.String()
if !strings.Contains(ip, ":") && len(ip) > 0 && len(v.HardwareAddr) > 0 { if !strings.Contains(ip, ":") && len(ip) > 0 && len(v.HardwareAddr) > 0 {
m.Add("append", "index", v.Index) m.Add("append", "index", v.Index)
m.Add("append", "name", v.Name) m.Add("append", "name", v.Name)