1
0
mirror of https://shylinux.com/x/icebergs synced 2025-04-25 17:18:05 +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)
}
}},
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 {
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) {
case func(*sync.Mutex, string, map[string]interface{}):
mu := &sync.Mutex{}
wg := &sync.WaitGroup{}
defer wg.Wait()
res = miss.Richs(kit.Keys(prefix, chain), cache, raw, func(key string, value map[string]interface{}) {
wg.Add(1)
m.Go(func() {
defer wg.Done()
cb(mu, key, value)
})
})
wg.Wait()
default:
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"
)
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() {
switch e := recover(); e {
case io.EOF:
@ -19,13 +19,13 @@ func (m *Message) TryCatch(msg *Message, safe bool, hand ...func(msg *Message))
default:
fileline := kit.FileLine(4, 5)
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("stack", msg.Format("stack"))
m.Log("stack", msg.FormatStack())
m.Log(LOG_WARN, "catch: %s %s", e, fileline)
if len(hand) > 1 { // 捕获异常
m.TryCatch(msg, safe, hand[1:]...)
} else if !safe { // 抛出异常
m.TryCatch(msg, silent, hand[1:]...)
} else if !silent { // 抛出异常
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")) {
m.Start(k)
}
m.Cmd("ssh.source", "etc/init.shy", "init.shy", "启动配置")
m.Cmd("ssh.source", ETC_INIT, "init.shy", "启动配置")
m.Cmdy(arg)
}},
"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"))
}},
"exit": {Name: "exit restart:button", Help: "结束", Action: map[string]*Action{
"restart": {Name: "restart", Help: "重启", Hand: func(m *Message, arg ...string) {
}},
}, Hand: func(m *Message, c *Context, cmd string, arg ...string) {
"exit": {Name: "exit restart:button", Help: "结束", Hand: func(m *Message, c *Context, cmd string, arg ...string) {
m.root.target.server.(*Frame).code = kit.Int(kit.Select("0", arg, 0))
m.Cmd("ssh.source", ETC_EXIT, "exit.shy", "退出配置")
m.root.Cmd(CTX_EXIT)

View File

@ -25,8 +25,7 @@ func (m *Message) log(level string, str string, arg ...interface{}) *Message {
return m // 禁用日志
}
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 {
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 {
if err {
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, m.Format("chain"))
m.log(LOG_ERROR, m.FormatChain())
return true
}
return false

115
misc.go
View File

@ -101,6 +101,64 @@ func (m *Message) Split(str string, field string, space string, enter string) *M
}
return m
}
func (m *Message) FormatStack() string {
// 调用栈
pc := make([]uintptr, 100)
pc = pc[:runtime.Callers(5, pc)]
frames := runtime.CallersFrames(pc)
meta := []string{}
for {
frame, more := frames.Next()
file := strings.Split(frame.File, "/")
name := strings.Split(frame.Function, "/")
meta = append(meta, fmt.Sprintf("\n%s:%d\t%s", file[len(file)-1], frame.Line, name[len(name)-1]))
if !more {
break
}
}
return strings.Join(meta, "")
}
func (m *Message) FormatChain() string {
ms := []*Message{}
for msg := m; msg != nil; msg = msg.message {
ms = append(ms, msg)
}
meta := append([]string{}, "\n\n")
for i := len(ms) - 1; i >= 0; i-- {
msg := ms[i]
meta = append(meta, fmt.Sprintf("%s ", msg.Format("prefix")))
if len(msg.meta[MSG_DETAIL]) > 0 {
meta = append(meta, fmt.Sprintf("detail:%d %v", len(msg.meta[MSG_DETAIL]), msg.meta[MSG_DETAIL]))
}
if len(msg.meta[MSG_OPTION]) > 0 {
meta = append(meta, fmt.Sprintf("option:%d %v\n", len(msg.meta[MSG_OPTION]), msg.meta[MSG_OPTION]))
for _, k := range msg.meta[MSG_OPTION] {
if v, ok := msg.meta[k]; ok {
meta = append(meta, fmt.Sprintf(" %s: %d %v\n", k, len(v), v))
}
}
} else {
meta = append(meta, "\n")
}
if len(msg.meta[MSG_APPEND]) > 0 {
meta = append(meta, fmt.Sprintf(" append:%d %v\n", len(msg.meta[MSG_APPEND]), msg.meta[MSG_APPEND]))
for _, k := range msg.meta[MSG_APPEND] {
if v, ok := msg.meta[k]; ok {
meta = append(meta, fmt.Sprintf(" %s: %d %v\n", k, len(v), v))
}
}
}
if len(msg.meta[MSG_RESULT]) > 0 {
meta = append(meta, fmt.Sprintf(" result:%d %v\n", len(msg.meta[MSG_RESULT]), msg.meta[MSG_RESULT]))
}
}
return strings.Join(meta, "")
}
func (m *Message) Format(key interface{}) string {
switch key := key.(type) {
case []byte:
@ -132,62 +190,9 @@ func (m *Message) Format(key interface{}) string {
return fmt.Sprintf("%s %d %s->%s", m.Time(), m.code, m.source.Name, m.target.Name)
case "chain":
// 调用链
ms := []*Message{}
for msg := m; msg != nil; msg = msg.message {
ms = append(ms, msg)
}
meta := append([]string{}, "\n\n")
for i := len(ms) - 1; i >= 0; i-- {
msg := ms[i]
meta = append(meta, fmt.Sprintf("%s ", msg.Format("prefix")))
if len(msg.meta[MSG_DETAIL]) > 0 {
meta = append(meta, fmt.Sprintf("detail:%d %v", len(msg.meta[MSG_DETAIL]), msg.meta[MSG_DETAIL]))
}
if len(msg.meta[MSG_OPTION]) > 0 {
meta = append(meta, fmt.Sprintf("option:%d %v\n", len(msg.meta[MSG_OPTION]), msg.meta[MSG_OPTION]))
for _, k := range msg.meta[MSG_OPTION] {
if v, ok := msg.meta[k]; ok {
meta = append(meta, fmt.Sprintf(" %s: %d %v\n", k, len(v), v))
}
}
} else {
meta = append(meta, "\n")
}
if len(msg.meta[MSG_APPEND]) > 0 {
meta = append(meta, fmt.Sprintf(" append:%d %v\n", len(msg.meta[MSG_APPEND]), msg.meta[MSG_APPEND]))
for _, k := range msg.meta[MSG_APPEND] {
if v, ok := msg.meta[k]; ok {
meta = append(meta, fmt.Sprintf(" %s: %d %v\n", k, len(v), v))
}
}
}
if len(msg.meta[MSG_RESULT]) > 0 {
meta = append(meta, fmt.Sprintf(" result:%d %v\n", len(msg.meta[MSG_RESULT]), msg.meta[MSG_RESULT]))
}
}
return strings.Join(meta, "")
return m.FormatChain()
case "stack":
// 调用栈
pc := make([]uintptr, 100)
pc = pc[:runtime.Callers(5, pc)]
frames := runtime.CallersFrames(pc)
meta := []string{}
for {
frame, more := frames.Next()
file := strings.Split(frame.File, "/")
name := strings.Split(frame.Function, "/")
meta = append(meta, fmt.Sprintf("\n%s:%d\t%s", file[len(file)-1], frame.Line, name[len(name)-1]))
if !more {
break
}
}
return strings.Join(meta, "")
return m.FormatStack()
}
}
return m.time.Format(MOD_TIME)