1
0
mirror of https://shylinux.com/x/icebergs synced 2025-04-27 01:48:27 +08:00
This commit is contained in:
shaoying 2021-07-31 11:24:28 +08:00
parent 63ef3b6dab
commit 6c24cff428
6 changed files with 74 additions and 71 deletions

View File

@ -239,7 +239,7 @@ func init() {
m.Conf(SERVE, kit.Keys(kit.MDB_META, aaa.WHITE, k), true) m.Conf(SERVE, kit.Keys(kit.MDB_META, aaa.WHITE, k), true)
} }
}}, }},
tcp.START: {Name: "start dev= name=self proto=http host= port=9020", Help: "启动", Hand: func(m *ice.Message, arg ...string) { cli.START: {Name: "start dev= name=self proto=http host= port=9020", Help: "启动", Hand: func(m *ice.Message, arg ...string) {
if cli.NodeInfo(m, SERVER, ice.Info.HostName); m.Option(tcp.PORT) == tcp.RANDOM { if cli.NodeInfo(m, SERVER, ice.Info.HostName); m.Option(tcp.PORT) == tcp.RANDOM {
m.Option(tcp.PORT, m.Cmdx(tcp.PORT, aaa.RIGHT)) m.Option(tcp.PORT, m.Cmdx(tcp.PORT, aaa.RIGHT))
} }

View File

@ -45,15 +45,17 @@ func (m *Message) Richs(prefix string, chain interface{}, raw interface{}, cb in
switch cb := cb.(type) { switch cb := cb.(type) {
case func(*sync.Mutex, string, map[string]interface{}): case func(*sync.Mutex, string, map[string]interface{}):
mu := &sync.Mutex{} mu := &sync.Mutex{}
wg := &sync.WaitGroup{} wg := &sync.WaitGroup{}
defer wg.Wait()
res = miss.Richs(kit.Keys(prefix, chain), cache, raw, func(key string, value map[string]interface{}) { res = miss.Richs(kit.Keys(prefix, chain), cache, raw, func(key string, value map[string]interface{}) {
wg.Add(1) wg.Add(1)
m.Go(func() { m.Go(func() {
defer wg.Done() defer wg.Done()
cb(mu, key, value) cb(mu, key, value)
}) })
}) })
wg.Wait()
default: default:
res = miss.Richs(kit.Keys(prefix, chain), cache, raw, cb) res = miss.Richs(kit.Keys(prefix, chain), cache, raw, cb)
} }

10
exec.go
View File

@ -11,7 +11,7 @@ import (
"github.com/shylinux/toolkits/task" "github.com/shylinux/toolkits/task"
) )
func (m *Message) TryCatch(msg *Message, safe bool, hand ...func(msg *Message)) *Message { func (m *Message) TryCatch(msg *Message, silent bool, hand ...func(msg *Message)) *Message {
defer func() { defer func() {
switch e := recover(); e { switch e := recover(); e {
case io.EOF: case io.EOF:
@ -19,13 +19,13 @@ func (m *Message) TryCatch(msg *Message, safe bool, hand ...func(msg *Message))
default: default:
fileline := kit.FileLine(4, 5) fileline := kit.FileLine(4, 5)
m.Log(LOG_WARN, "catch: %s %s", e, fileline) m.Log(LOG_WARN, "catch: %s %s", e, fileline)
m.Log("chain", msg.Format("chain")) m.Log("chain", msg.FormatChain())
m.Log(LOG_WARN, "catch: %s %s", e, fileline) m.Log(LOG_WARN, "catch: %s %s", e, fileline)
m.Log("stack", msg.Format("stack")) m.Log("stack", msg.FormatStack())
m.Log(LOG_WARN, "catch: %s %s", e, fileline) m.Log(LOG_WARN, "catch: %s %s", e, fileline)
if len(hand) > 1 { // 捕获异常 if len(hand) > 1 { // 捕获异常
m.TryCatch(msg, safe, hand[1:]...) m.TryCatch(msg, silent, hand[1:]...)
} else if !safe { // 抛出异常 } else if !silent { // 抛出异常
m.Assert(e) m.Assert(e)
} }
} }

View File

@ -97,16 +97,13 @@ var Index = &Context{Name: "ice", Help: "冰山模块",
for _, k := range kit.Split(kit.Select("gdb,log,ssh,mdb")) { for _, k := range kit.Split(kit.Select("gdb,log,ssh,mdb")) {
m.Start(k) m.Start(k)
} }
m.Cmd("ssh.source", "etc/init.shy", "init.shy", "启动配置") m.Cmd("ssh.source", ETC_INIT, "init.shy", "启动配置")
m.Cmdy(arg) m.Cmdy(arg)
}}, }},
"help": {Name: "help", Help: "帮助", Hand: func(m *Message, c *Context, cmd string, arg ...string) { "help": {Name: "help", Help: "帮助", Hand: func(m *Message, c *Context, cmd string, arg ...string) {
m.Echo(strings.Join(kit.Simple(m.Confv("help", "index")), "\n")) m.Echo(strings.Join(kit.Simple(m.Confv("help", "index")), "\n"))
}}, }},
"exit": {Name: "exit restart:button", Help: "结束", Action: map[string]*Action{ "exit": {Name: "exit restart:button", Help: "结束", Hand: func(m *Message, c *Context, cmd string, arg ...string) {
"restart": {Name: "restart", Help: "重启", Hand: func(m *Message, arg ...string) {
}},
}, Hand: func(m *Message, c *Context, cmd string, arg ...string) {
m.root.target.server.(*Frame).code = kit.Int(kit.Select("0", arg, 0)) m.root.target.server.(*Frame).code = kit.Int(kit.Select("0", arg, 0))
m.Cmd("ssh.source", ETC_EXIT, "exit.shy", "退出配置") m.Cmd("ssh.source", ETC_EXIT, "exit.shy", "退出配置")
m.root.Cmd(CTX_EXIT) m.root.Cmd(CTX_EXIT)

View File

@ -25,8 +25,7 @@ func (m *Message) log(level string, str string, arg ...interface{}) *Message {
return m // 禁用日志 return m // 禁用日志
} }
if str = strings.TrimSpace(kit.Format(str, arg...)); Log != nil { if str = strings.TrimSpace(kit.Format(str, arg...)); Log != nil {
Log(m, m.Format(kit.MDB_PREFIX), level, str) Log(m, m.Format(kit.MDB_PREFIX), level, str) // 日志分流
// 日志分流
} }
if m.Option("_disable_log") == TRUE { if m.Option("_disable_log") == TRUE {
return m // 屏蔽日志 return m // 屏蔽日志
@ -103,9 +102,9 @@ func (m *Message) Warn(err bool, arg ...interface{}) bool {
func (m *Message) Error(err bool, str string, arg ...interface{}) bool { func (m *Message) Error(err bool, str string, arg ...interface{}) bool {
if err { if err {
m.Echo("error: ").Echo(str, arg...) m.Echo("error: ").Echo(str, arg...)
m.log(LOG_ERROR, m.Format("stack")) m.log(LOG_ERROR, m.FormatStack())
m.log(LOG_ERROR, str, arg...) m.log(LOG_ERROR, str, arg...)
m.log(LOG_ERROR, m.Format("chain")) m.log(LOG_ERROR, m.FormatChain())
return true return true
} }
return false return false

99
misc.go
View File

@ -101,38 +101,25 @@ func (m *Message) Split(str string, field string, space string, enter string) *M
} }
return m return m
} }
func (m *Message) Format(key interface{}) string { func (m *Message) FormatStack() string {
switch key := key.(type) { // 调用栈
case []byte: pc := make([]uintptr, 100)
json.Unmarshal(key, &m.meta) pc = pc[:runtime.Callers(5, pc)]
case string: frames := runtime.CallersFrames(pc)
switch key {
case "cost":
return kit.FmtTime(kit.Int64(time.Since(m.time)))
case "meta":
return kit.Format(m.meta)
case "size":
if len(m.meta["append"]) == 0 {
return fmt.Sprintf("%dx%d", 0, 0)
} else {
return fmt.Sprintf("%dx%d", len(m.meta[m.meta["append"][0]]), len(m.meta["append"]))
}
case "append":
if len(m.meta["append"]) == 0 {
return fmt.Sprintf("%dx%d %s", 0, 0, "[]")
} else {
return fmt.Sprintf("%dx%d %s", len(m.meta[m.meta["append"][0]]), len(m.meta["append"]), kit.Format(m.meta["append"]))
}
case "time": meta := []string{}
return m.Time() for {
case "ship": frame, more := frames.Next()
return fmt.Sprintf("%s->%s", m.source.Name, m.target.Name) file := strings.Split(frame.File, "/")
case "prefix": name := strings.Split(frame.Function, "/")
return fmt.Sprintf("%s %d %s->%s", m.Time(), m.code, m.source.Name, m.target.Name) meta = append(meta, fmt.Sprintf("\n%s:%d\t%s", file[len(file)-1], frame.Line, name[len(name)-1]))
if !more {
case "chain": break
// 调用链 }
}
return strings.Join(meta, "")
}
func (m *Message) FormatChain() string {
ms := []*Message{} ms := []*Message{}
for msg := m; msg != nil; msg = msg.message { for msg := m; msg != nil; msg = msg.message {
ms = append(ms, msg) ms = append(ms, msg)
@ -171,23 +158,41 @@ func (m *Message) Format(key interface{}) string {
} }
} }
return strings.Join(meta, "") return strings.Join(meta, "")
case "stack": }
// 调用栈 func (m *Message) Format(key interface{}) string {
pc := make([]uintptr, 100) switch key := key.(type) {
pc = pc[:runtime.Callers(5, pc)] case []byte:
frames := runtime.CallersFrames(pc) json.Unmarshal(key, &m.meta)
case string:
switch key {
case "cost":
return kit.FmtTime(kit.Int64(time.Since(m.time)))
case "meta":
return kit.Format(m.meta)
case "size":
if len(m.meta["append"]) == 0 {
return fmt.Sprintf("%dx%d", 0, 0)
} else {
return fmt.Sprintf("%dx%d", len(m.meta[m.meta["append"][0]]), len(m.meta["append"]))
}
case "append":
if len(m.meta["append"]) == 0 {
return fmt.Sprintf("%dx%d %s", 0, 0, "[]")
} else {
return fmt.Sprintf("%dx%d %s", len(m.meta[m.meta["append"][0]]), len(m.meta["append"]), kit.Format(m.meta["append"]))
}
meta := []string{} case "time":
for { return m.Time()
frame, more := frames.Next() case "ship":
file := strings.Split(frame.File, "/") return fmt.Sprintf("%s->%s", m.source.Name, m.target.Name)
name := strings.Split(frame.Function, "/") case "prefix":
meta = append(meta, fmt.Sprintf("\n%s:%d\t%s", file[len(file)-1], frame.Line, name[len(name)-1])) return fmt.Sprintf("%s %d %s->%s", m.Time(), m.code, m.source.Name, m.target.Name)
if !more {
break case "chain":
} return m.FormatChain()
} case "stack":
return strings.Join(meta, "") return m.FormatStack()
} }
} }
return m.time.Format(MOD_TIME) return m.time.Format(MOD_TIME)