From e5bac606f16bcd4f69858a843909e4c34fe16e0e Mon Sep 17 00:00:00 2001 From: shaoying Date: Fri, 15 Oct 2021 14:47:03 +0800 Subject: [PATCH] opt some --- base/log/log.go | 4 ++-- info.go | 20 ++++++++++++++++---- init.go | 14 +++----------- logs.go | 9 +++------ misc/ssh/channel.go | 3 ++- misc/ssh/connect.go | 4 +++- misc/ssh/service.go | 5 +++-- misc/ssh/session.go | 3 ++- render.go | 6 ++---- 9 files changed, 36 insertions(+), 32 deletions(-) diff --git a/base/log/log.go b/base/log/log.go index 5ac0b357..e1626e13 100644 --- a/base/log/log.go +++ b/base/log/log.go @@ -22,7 +22,7 @@ func (f *Frame) Spawn(m *ice.Message, c *ice.Context, arg ...string) ice.Server } func (f *Frame) Begin(m *ice.Message, arg ...string) ice.Server { f.p = make(chan *Log, ice.MOD_BUFS) - ice.Log = func(msg *ice.Message, p, l, s string) { + ice.Info.Log = func(msg *ice.Message, p, l, s string) { f.p <- &Log{m: msg, p: p, l: l, s: s} } return f @@ -132,7 +132,7 @@ var Index = &ice.Context{Name: "log", Help: "日志模块", ice.CTX_EXIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { if f, ok := m.Target().Server().(*Frame); ok { // 关闭日志 - ice.Log = nil + ice.Info.Log = nil close(f.p) } }}, diff --git a/info.go b/info.go index 87ec7514..5657cb0f 100644 --- a/info.go +++ b/info.go @@ -28,11 +28,23 @@ var Info = struct { UserName string } - Pack map[string][]byte - names map[string]interface{} + Help string + Pack map[string][]byte + names map[string]interface{} + render map[string]func(*Message, string, ...interface{}) string + Log func(m *Message, p, l, s string) }{ - Pack: map[string][]byte{}, - names: map[string]interface{}{}, + Help: ` +^_^ 欢迎使用冰山框架 ^_^ +^_^ Welcome to Icebergs World ^_^ + +report: shylinuxc@gmail.com +server: https://shylinux.com +source: https://shylinux.com/x/icebergs +`, + Pack: map[string][]byte{}, + names: map[string]interface{}{}, + render: map[string]func(*Message, string, ...interface{}) string{}, } func Dump(w io.Writer, name string, cb func(string)) bool { diff --git a/init.go b/init.go index 3b37511f..6f3b6c97 100644 --- a/init.go +++ b/init.go @@ -7,6 +7,7 @@ import ( "time" kit "shylinux.com/x/toolkits" + log "shylinux.com/x/toolkits/logs" ) type Frame struct { @@ -59,7 +60,7 @@ func (f *Frame) Close(m *Message, arg ...string) bool { var Index = &Context{Name: "ice", Help: "冰山模块", Caches: map[string]*Cache{ CTX_FOLLOW: {Value: ICE}, CTX_STREAM: {Value: SHY}, CTX_STATUS: {Value: CTX_BEGIN}, }, Configs: map[string]*Config{ - HELP: {Value: kit.Data("index", _help)}, + HELP: {Value: kit.Data("index", Info.Help)}, }, Commands: map[string]*Command{ CTX_INIT: {Hand: func(m *Message, c *Context, cmd string, arg ...string) { defer m.Cost(CTX_INIT) @@ -124,7 +125,7 @@ func Run(arg ...string) string { switch kit.Select("", arg, 0) { case "space", "serve": - if _log_disable = false; frame.Begin(Pulse.Spawn(), arg...).Start(Pulse, arg...) { + if log.LogDisable = false; frame.Begin(Pulse.Spawn(), arg...).Start(Pulse, arg...) { frame.Close(Pulse.Spawn(), arg...) } @@ -142,12 +143,3 @@ func Run(arg ...string) string { return Pulse.Result() } - -var _help = ` -^_^ 欢迎使用冰山框架 ^_^ -^_^ Welcome to Icebergs World ^_^ - -report: shylinuxc@gmail.com -server: https://shylinux.com -source: https://shylinux.com/x/icebergs -` diff --git a/logs.go b/logs.go index 1decacf9..d7543d20 100644 --- a/logs.go +++ b/logs.go @@ -10,15 +10,12 @@ import ( log "shylinux.com/x/toolkits/logs" ) -var _log_disable = true -var Log func(m *Message, p, l, s string) - func (m *Message) log(level string, str string, arg ...interface{}) *Message { - if _log_disable { + if log.LogDisable { return m // 禁用日志 } - if str = strings.TrimSpace(kit.Format(str, arg...)); Log != nil { - Log(m, m.Format(kit.MDB_PREFIX), level, str) // 日志分流 + if str = strings.TrimSpace(kit.Format(str, arg...)); Info.Log != nil { + Info.Log(m, m.Format(kit.MDB_PREFIX), level, str) // 日志分流 } // 日志颜色 diff --git a/misc/ssh/channel.go b/misc/ssh/channel.go index 103ccab6..fd4fcad3 100644 --- a/misc/ssh/channel.go +++ b/misc/ssh/channel.go @@ -11,6 +11,7 @@ import ( "shylinux.com/x/icebergs/base/aaa" "shylinux.com/x/icebergs/base/ctx" "shylinux.com/x/icebergs/base/mdb" + psh "shylinux.com/x/icebergs/base/ssh" "shylinux.com/x/icebergs/base/tcp" kit "shylinux.com/x/toolkits" ) @@ -66,7 +67,7 @@ func _ssh_watch(m *ice.Message, meta map[string]string, h string, input io.Reade const CHANNEL = "channel" func init() { - Index.Merge(&ice.Context{ + psh.Index.Merge(&ice.Context{ Configs: map[string]*ice.Config{ CHANNEL: {Name: "channel", Help: "通道", Value: kit.Data()}, }, diff --git a/misc/ssh/connect.go b/misc/ssh/connect.go index b9fa3054..fc5a1e53 100644 --- a/misc/ssh/connect.go +++ b/misc/ssh/connect.go @@ -16,6 +16,7 @@ import ( "shylinux.com/x/icebergs/base/gdb" "shylinux.com/x/icebergs/base/mdb" "shylinux.com/x/icebergs/base/nfs" + psh "shylinux.com/x/icebergs/base/ssh" "shylinux.com/x/icebergs/base/tcp" kit "shylinux.com/x/toolkits" ) @@ -150,10 +151,11 @@ func _ssh_conn(m *ice.Message, cb func(*ssh.Client), arg ...string) { tcp.PORT, m.Option(tcp.PORT), tcp.HOST, m.Option(tcp.HOST), arg) } +const SSH = "ssh" const CONNECT = "connect" func init() { - Index.Merge(&ice.Context{ + psh.Index.Merge(&ice.Context{ Configs: map[string]*ice.Config{ CONNECT: {Name: CONNECT, Help: "连接", Value: kit.Data()}, }, diff --git a/misc/ssh/service.go b/misc/ssh/service.go index 36ee6b57..fec8573a 100644 --- a/misc/ssh/service.go +++ b/misc/ssh/service.go @@ -10,14 +10,15 @@ import ( "path" "strings" + "golang.org/x/crypto/ssh" ice "shylinux.com/x/icebergs" "shylinux.com/x/icebergs/base/aaa" "shylinux.com/x/icebergs/base/cli" "shylinux.com/x/icebergs/base/mdb" "shylinux.com/x/icebergs/base/nfs" + psh "shylinux.com/x/icebergs/base/ssh" "shylinux.com/x/icebergs/base/tcp" kit "shylinux.com/x/toolkits" - "golang.org/x/crypto/ssh" ) func _ssh_meta(conn ssh.ConnMetadata) map[string]string { @@ -99,7 +100,7 @@ const ( const SERVICE = "service" func init() { - Index.Merge(&ice.Context{ + psh.Index.Merge(&ice.Context{ Configs: map[string]*ice.Config{ SERVICE: {Name: SERVICE, Help: "服务", Value: kit.Data( WELCOME, "\r\nwelcome to context world\r\n", diff --git a/misc/ssh/session.go b/misc/ssh/session.go index ab130ea7..664defc6 100644 --- a/misc/ssh/session.go +++ b/misc/ssh/session.go @@ -7,6 +7,7 @@ import ( ice "shylinux.com/x/icebergs" "shylinux.com/x/icebergs/base/ctx" "shylinux.com/x/icebergs/base/mdb" + psh "shylinux.com/x/icebergs/base/ssh" "shylinux.com/x/icebergs/base/tcp" kit "shylinux.com/x/toolkits" ) @@ -57,7 +58,7 @@ const ( const SESSION = "session" func init() { - Index.Merge(&ice.Context{ + psh.Index.Merge(&ice.Context{ Configs: map[string]*ice.Config{ SESSION: {Name: SESSION, Help: "会话", Value: kit.Data()}, }, diff --git a/render.go b/render.go index f5dfff46..04d4d861 100644 --- a/render.go +++ b/render.go @@ -8,13 +8,11 @@ import ( kit "shylinux.com/x/toolkits" ) -var renderList = map[string]func(*Message, string, ...interface{}) string{} - func AddRender(key string, render func(*Message, string, ...interface{}) string) { - renderList[key] = render + Info.render[key] = render } func Render(m *Message, cmd string, args ...interface{}) string { - if render, ok := renderList[cmd]; ok { + if render, ok := Info.render[cmd]; ok { m.Debug("render: %v %v", cmd, kit.FileLine(render, 3)) return render(m, cmd, args...) }