diff --git a/base/cli/daemon.go b/base/cli/daemon.go index 4b32edcb..14dcf1d1 100644 --- a/base/cli/daemon.go +++ b/base/cli/daemon.go @@ -10,6 +10,9 @@ import ( "strings" ) +const ( + ErrStart = "daemon start: " +) const ( StatusError = "error" StatusStart = "start" @@ -28,7 +31,7 @@ func _daemon_show(m *ice.Message, cmd *exec.Cmd, out, err string) { } cmd.Env = append(cmd.Env, fmt.Sprintf("PATH=%s", os.Getenv("PATH"))) - if e := cmd.Start(); m.Warn(e != nil, ice.ErrStart, cmd.Args, " ", e) { + if e := cmd.Start(); m.Warn(e != nil, ErrStart, cmd.Args, " ", e) { return } @@ -42,7 +45,7 @@ func _daemon_show(m *ice.Message, cmd *exec.Cmd, out, err string) { m.Gos(m, func(m *ice.Message) { if e := cmd.Wait(); e != nil { - m.Warn(e != nil, ice.ErrStart, cmd.Args, " ", e) + m.Warn(e != nil, ErrStart, cmd.Args, " ", e) m.Richs(DAEMON, nil, h, func(key string, value map[string]interface{}) { kit.Value(value, kit.MDB_STATUS, StatusError) kit.Value(value, kit.Keys(kit.MDB_EXTRA, kit.MDB_ERROR), e) diff --git a/base/log/log.go b/base/log/log.go index 8721c4b8..de0604c4 100644 --- a/base/log/log.go +++ b/base/log/log.go @@ -1,32 +1,29 @@ package log import ( - "github.com/shylinux/icebergs" - "github.com/shylinux/toolkits" + ice "github.com/shylinux/icebergs" + kit "github.com/shylinux/toolkits" "bufio" - "os" "path" ) type Log struct { m *ice.Message + p string l string s string } -type Frame struct { - p chan *Log -} +type Frame struct{ p chan *Log } func (f *Frame) Spawn(m *ice.Message, c *ice.Context, arg ...string) ice.Server { return &Frame{} } func (f *Frame) Begin(m *ice.Message, arg ...string) ice.Server { - // 日志管道 - f.p = make(chan *Log, 1024) - ice.Log = func(msg *ice.Message, level string, str string) { - f.p <- &Log{m: msg, l: level, s: str} + f.p = make(chan *Log, 4096) + ice.Log = func(msg *ice.Message, p, l, s string) { + f.p <- &Log{m: msg, p: p, l: l, s: s} } return f } @@ -37,14 +34,12 @@ func (f *Frame) Start(m *ice.Message, arg ...string) bool { if !ok { break } - // 日志文件 - file := kit.Select("bench", m.Conf("show", kit.Keys(l.l, "file"))) - // 日志格式 - view := m.Confm("view", m.Conf("show", kit.Keys(l.l, "view"))) - // 输出日志 + file := kit.Select("bench", m.Conf("show", kit.Keys(l.l, "file"))) + view := m.Confm("view", m.Conf("show", kit.Keys(l.l, "view"))) bio := m.Confv("file", file+".file").(*bufio.Writer) - bio.WriteString(l.m.Format("prefix")) + + bio.WriteString(l.p) bio.WriteString(" ") if p, ok := view["prefix"].(string); ok { bio.WriteString(p) @@ -65,72 +60,54 @@ func (f *Frame) Close(m *ice.Message, arg ...string) bool { return true } -const ( - ERROR = "error" - TRACE = "trace" -) - var Index = &ice.Context{Name: "log", Help: "日志模块", Configs: map[string]*ice.Config{ - "file": &ice.Config{Name: "file", Help: "日志文件", Value: kit.Dict( - "watch", kit.Dict("path", "var/log/watch.log"), - "bench", kit.Dict("path", "var/log/bench.log"), - "error", kit.Dict("path", "var/log/error.log"), - "trace", kit.Dict("path", "var/log/trace.log"), + "file": {Name: "file", Help: "日志文件", Value: kit.Dict( + "watch", kit.Dict("path", "var/log/watch.log", "list", []string{ + ice.LOG_CREATE, ice.LOG_REMOVE, + ice.LOG_INSERT, ice.LOG_DELETE, + ice.LOG_SELECT, ice.LOG_MODIFY, + ice.LOG_EXPORT, ice.LOG_IMPORT, + ice.LOG_ENABLE, + }), + "bench", kit.Dict("path", "var/log/bench.log", "list", []string{}), + "error", kit.Dict("path", "var/log/error.log", "list", []string{ + ice.LOG_WARN, ice.LOG_ERROR, + }), + "trace", kit.Dict("path", "var/log/trace.log", "list", []string{}), )}, - "view": &ice.Config{Name: "view", Help: "日志格式", Value: kit.Dict( - "red", kit.Dict("prefix", "\033[31m", "suffix", "\033[0m"), - "green", kit.Dict("prefix", "\033[32m", "suffix", "\033[0m"), - "yellow", kit.Dict("prefix", "\033[33m", "suffix", "\033[0m"), - )}, - "show": &ice.Config{Name: "show", Help: "日志分流", Value: kit.Dict( - // 数据 - ice.LOG_ENABLE, kit.Dict("file", "watch"), - ice.LOG_IMPORT, kit.Dict("file", "watch"), - ice.LOG_EXPORT, kit.Dict("file", "watch"), - ice.LOG_CREATE, kit.Dict("file", "watch"), - ice.LOG_REMOVE, kit.Dict("file", "watch"), - ice.LOG_INSERT, kit.Dict("file", "watch"), - ice.LOG_DELETE, kit.Dict("file", "watch"), - ice.LOG_MODIFY, kit.Dict("file", "watch"), - ice.LOG_SELECT, kit.Dict("file", "watch"), - - // 事件 - ice.LOG_LISTEN, kit.Dict("file", "bench"), - ice.LOG_ACCEPT, kit.Dict("file", "bench", "view", "green"), - ice.LOG_FINISH, kit.Dict("file", "bench", "view", "red"), - ice.LOG_SIGNAL, kit.Dict("file", "bench"), - ice.LOG_EVENTS, kit.Dict("file", "bench"), - ice.LOG_TIMERS, kit.Dict("file", "bench"), - - // 状态 - ice.LOG_BEGIN, kit.Dict("file", "bench"), - ice.LOG_START, kit.Dict("file", "bench", "view", "green"), - ice.LOG_SERVE, kit.Dict("file", "bench", "view", "green"), - ice.LOG_CLOSE, kit.Dict("file", "bench", "view", "red"), - - // 分类 - ice.LOG_AUTH, kit.Dict("file", "bench", "view", "yellow"), - ice.LOG_CMDS, kit.Dict("file", "bench", "view", "green"), - ice.LOG_COST, kit.Dict("file", "bench", "view", "yellow"), - ice.LOG_INFO, kit.Dict("file", "bench"), - ice.LOG_WARN, kit.Dict("file", "bench", "view", "red"), - ice.LOG_ERROR, kit.Dict("file", "error"), - ice.LOG_TRACE, kit.Dict("file", "trace"), + "view": {Name: "view", Help: "日志格式", Value: kit.Dict( + "green", kit.Dict("prefix", "\033[32m", "suffix", "\033[0m", "list", []string{ + ice.LOG_START, ice.LOG_SERVE, + ice.LOG_CMDS, + }), + "yellow", kit.Dict("prefix", "\033[33m", "suffix", "\033[0m", "list", []string{ + ice.LOG_AUTH, ice.LOG_COST, + }), + "red", kit.Dict("prefix", "\033[31m", "suffix", "\033[0m", "list", []string{ + ice.LOG_WARN, ice.LOG_CLOSE, + }), )}, + "show": {Name: "show", Help: "日志分流", Value: kit.Dict()}, }, Commands: map[string]*ice.Command{ ice.CTX_INIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { - if os.Getenv("ctx_mod") != "" { - m.Confm("file", nil, func(key string, value map[string]interface{}) { - // 日志文件 - if f, p, e := kit.Create(kit.Format(value["path"])); m.Assert(e) { - m.Cap(ice.CTX_STREAM, path.Base(p)) - value["file"] = bufio.NewWriter(f) - m.Log_CREATE(kit.MDB_FILE, p) - } + m.Confm("view", nil, func(key string, value map[string]interface{}) { + kit.Fetch(value["list"], func(index int, k string) { + m.Conf("show", kit.Keys(k, "view"), key) }) - } + }) + m.Confm("file", nil, func(key string, value map[string]interface{}) { + kit.Fetch(value["list"], func(index int, k string) { + m.Conf("show", kit.Keys(k, "file"), key) + }) + // 日志文件 + if f, p, e := kit.Create(kit.Format(value["path"])); m.Assert(e) { + m.Cap(ice.CTX_STREAM, path.Base(p)) + value["file"] = bufio.NewWriter(f) + m.Log_CREATE(kit.MDB_FILE, p) + } + }) }}, ice.CTX_EXIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { if f, ok := m.Target().Server().(*Frame); ok { diff --git a/base/ssh/connect.go b/base/ssh/connect.go index 687f2c56..ae6577cc 100644 --- a/base/ssh/connect.go +++ b/base/ssh/connect.go @@ -79,6 +79,28 @@ func init() { }) }}, + "open": {Name: "dial username=shy host=shylinux.com port=22 private=.ssh/id_rsa", Help: "添加", Hand: func(m *ice.Message, arg ...string) { + m.Option(tcp.DIAL_CB, func(c net.Conn) { + client, e := _ssh_conn(m, c, kit.Select("shy", m.Option(aaa.USERNAME)), + kit.Select("shylinux.com", m.Option(tcp.HOST))+":"+kit.Select("22", m.Option(tcp.PORT)), + ) + m.Assert(e) + + m.Debug("what") + m.Debug("some") + session, e := client.NewSession() + m.Assert(e) + + session.Stdin = os.Stdin + session.Stdout = os.Stdout + session.Stderr = os.Stderr + session.Start("/bin/bash") + m.Debug("what") + m.Debug("some") + }) + + m.Cmd(tcp.CLIENT, tcp.DIAL, tcp.PORT, m.Option(tcp.PORT), tcp.HOST, m.Option(tcp.HOST), arg) + }}, mdb.DELETE: {Name: "delete", Help: "删除", Hand: func(m *ice.Message, arg ...string) { m.Cmdy(mdb.DELETE, CONNECT, "", mdb.HASH, kit.MDB_HASH, m.Option(kit.MDB_HASH)) }}, diff --git a/base/ssh/scripts.go b/base/ssh/scripts.go index 43473dba..7ef55b73 100644 --- a/base/ssh/scripts.go +++ b/base/ssh/scripts.go @@ -17,6 +17,60 @@ import ( "time" ) +type item struct { + c chan []byte + last []byte +} + +func (i item) Read(buf []byte) (int, error) { + b := <-i.c + n := copy(buf, b) + return n, nil +} + +type tmux struct { + input io.Reader + waits []chan []byte +} + +func (t *tmux) get() *item { + c := make(chan []byte) + t.waits = append(t.waits, c) + return &item{c: c} +} +func (t *tmux) loop() { + buf := make([]byte, 1024) + for { + n, e := t.input.Read(buf) + if e != nil { + break + } + if len(t.waits) > 0 { + wait := t.waits[len(t.waits)-1] + wait <- buf[:n] + } + } +} +func (t *tmux) read(cb func(buf []byte)) { + c := make(chan []byte) + t.waits = append(t.waits, c) + go func() { + for { + cb(<-c) + } + }() + return +} +func (t *tmux) over() { + if len(t.waits) > 0 { + t.waits = t.waits[:len(t.waits)-1] + } +} + +var stdin = &tmux{input: os.Stdin} + +func init() { go stdin.loop() } + func Render(msg *ice.Message, cmd string, args ...interface{}) { defer func() { msg.Log_EXPORT(mdb.RENDER, cmd, kit.MDB_TEXT, args) }() @@ -278,7 +332,8 @@ func (f *Frame) Start(m *ice.Message, arg ...string) bool { var r io.Reader switch m.Cap(ice.CTX_STREAM, f.source) { case STDIO: // 终端交互 - r, f.stdout = os.Stdin, os.Stdout + // r, f.stdout = os.Stdin, os.Stdout + r, f.stdout = stdin.get(), os.Stdout m.Option("_option", ice.MSG_USERNAME) m.Option(ice.MSG_USERNAME, cli.UserName) diff --git a/base/ssh/session.go b/base/ssh/session.go index c0f5f7e8..4a6f3145 100644 --- a/base/ssh/session.go +++ b/base/ssh/session.go @@ -1,14 +1,17 @@ package ssh import ( + "os" + ice "github.com/shylinux/icebergs" "github.com/shylinux/icebergs/base/ctx" "github.com/shylinux/icebergs/base/mdb" "github.com/shylinux/icebergs/base/tcp" kit "github.com/shylinux/toolkits" - "golang.org/x/crypto/ssh" "io" + + "golang.org/x/crypto/ssh" ) func _ssh_sess(m *ice.Message, h string, client *ssh.Client) (*ssh.Session, error) { @@ -44,6 +47,21 @@ func _ssh_sess(m *ice.Message, h string, client *ssh.Client) (*ssh.Session, erro return session, nil } +func _watch(m *ice.Message, from io.Reader, to io.Writer, cb func([]byte)) { + m.Go(func() { + buf := make([]byte, 1024) + for { + n, e := from.Read(buf) + if e != nil { + cb(nil) + break + } + cb(buf[:n]) + to.Write(buf[:n]) + } + }) +} + const ( TTY = "tty" ENV = "env" @@ -72,6 +90,22 @@ func init() { m.Sleep("300ms") m.Cmdy(SESSION, m.Option(kit.MDB_HASH)) }}, + "bind": {Name: "bind", Help: "绑定", Hand: func(m *ice.Message, arg ...string) { + m.Richs(SESSION, "", m.Option(kit.MDB_HASH), func(key string, value map[string]interface{}) { + value = kit.GetMeta(value) + + input := value["input"].(io.Writer) + stdin.read(func(buf []byte) { + m.Debug("input %v", string(buf)) + input.Write(buf) + }) + + output := value["output"].(io.Reader) + _watch(m, output, os.Stdout, func(buf []byte) { + m.Debug("output %v", string(buf)) + }) + }) + }}, mdb.DELETE: {Name: "delete", Help: "删除", Hand: func(m *ice.Message, arg ...string) { m.Cmdy(mdb.DELETE, SESSION, "", mdb.HASH, kit.MDB_HASH, m.Option(kit.MDB_HASH)) @@ -84,7 +118,7 @@ func init() { m.Option(mdb.FIELDS, "time,hash,status,count,connect") if m.Cmdy(mdb.SELECT, SESSION, "", mdb.HASH, kit.MDB_HASH, arg); len(arg) == 0 { m.Table(func(index int, value map[string]string, head []string) { - m.PushButton(kit.Select("", "删除", value[kit.MDB_STATUS] == tcp.CLOSE)) + m.PushButton(kit.Select("绑定", "删除", value[kit.MDB_STATUS] == tcp.CLOSE)) }) } return diff --git a/core/code/bench.go b/core/code/bench.go index b9ef2e96..cf770b22 100644 --- a/core/code/bench.go +++ b/core/code/bench.go @@ -38,8 +38,6 @@ func _bench_list(m *ice.Message, zone string, id string, field ...interface{}) { }) } func _bench_show(m *ice.Message, nconn, nreq int64, list []*http.Request) { - m.Log_CONF(NCONN, nconn, NREQS, nreq) - var body int64 s, e := bench.HTTP(nconn, nreq, list, func(req *http.Request, res *http.Response) { n, _ := io.Copy(ioutil.Discard, res.Body) diff --git a/go.mod b/go.mod index 9c3fe853..9eafe8f1 100644 --- a/go.mod +++ b/go.mod @@ -3,10 +3,6 @@ module github.com/shylinux/icebergs go 1.13 require ( - fyne.io/fyne v1.3.3 - github.com/gomarkdown/markdown v0.0.0-20200824053859-8c8b3816f167 - github.com/gomodule/redigo v2.0.0+incompatible // indirect - github.com/google/gitprotocolio v0.0.0-20180630173033-8d2b3b1c37f6 github.com/gorilla/websocket v1.4.2 github.com/kr/pty v1.1.1 github.com/nareix/joy4 v0.0.0-20200507095837-05a4ffbb5369 diff --git a/go.sum b/go.sum index 4e889416..e69de29b 100644 --- a/go.sum +++ b/go.sum @@ -1,98 +0,0 @@ -fyne.io/fyne v1.3.3 h1:jpdp9kTJ1ZOKQjyHZfzQGz/E3V35KVoMTawtii/1/j4= -fyne.io/fyne v1.3.3/go.mod h1:osD/JXxGf8AC7aB+Ek0YuFF2QXzdTFFzMRM8cdqrwvQ= -github.com/Kodeworks/golang-image-ico v0.0.0-20141118225523-73f0f4cfade9/go.mod h1:7uhhqiBaR4CpN0k9rMjOtjpcfGd6DG2m04zQxKnWQ0I= -github.com/akavel/rsrc v0.8.0/go.mod h1:uLoCtb9J+EyAqh+26kdrTgmzRBFPGOolLWKpdxkKq+c= -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= -github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= -github.com/fyne-io/mobile v0.0.2/go.mod h1:/kOrWrZB6sasLbEy2JIvr4arEzQTXBTZGb3Y96yWbHY= -github.com/go-gl/gl v0.0.0-20190320180904-bf2b1f2f34d7 h1:SCYMcCJ89LjRGwEa0tRluNRiMjZHalQZrVrvTbPh+qw= -github.com/go-gl/gl v0.0.0-20190320180904-bf2b1f2f34d7/go.mod h1:482civXOzJJCPzJ4ZOX/pwvXBWSnzD4OKMdH4ClKGbk= -github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200625191551-73d3c3675aa3 h1:q521PfSp5/z6/sD9FZZOWj4d1MLmfQW8PkRnI9M6PCE= -github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200625191551-73d3c3675aa3/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= -github.com/godbus/dbus/v5 v5.0.3 h1:ZqHaoEF7TBzh4jzPmqVhE/5A1z9of6orkAe5uHoAeME= -github.com/godbus/dbus/v5 v5.0.3/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= -github.com/goki/freetype v0.0.0-20181231101311-fa8a33aabaff h1:W71vTCKoxtdXgnm1ECDFkfQnpdqAO00zzGXLA5yaEX8= -github.com/goki/freetype v0.0.0-20181231101311-fa8a33aabaff/go.mod h1:wfqRWLHRBsRgkp5dmbG56SA0DmVtwrF5N3oPdI8t+Aw= -github.com/gomarkdown/markdown v0.0.0-20200824053859-8c8b3816f167 h1:LP/6EfrZ/LyCc+SXvANDrIJ4sP9u2NAtqyv6QknetNQ= -github.com/gomarkdown/markdown v0.0.0-20200824053859-8c8b3816f167/go.mod h1:aii0r/K0ZnHv7G0KF7xy1v0A7s2Ljrb5byB7MO5p6TU= -github.com/gomodule/redigo v2.0.0+incompatible h1:K/R+8tc58AaqLkqG2Ol3Qk+DR/TlNuhuh457pBFPtt0= -github.com/gomodule/redigo v2.0.0+incompatible/go.mod h1:B4C85qUVwatsJoIUNIfCRsp7qO0iAmpGFZ4EELWSbC4= -github.com/gomodule/redigo/redis v0.0.0-do-not-use h1:J7XIp6Kau0WoyT4JtXHT3Ei0gA1KkSc6bc87j9v9WIo= -github.com/google/gitprotocolio v0.0.0-20180630173033-8d2b3b1c37f6 h1:j5r184GepPynGzPVmz/nTUsZBW2Tl237S0WN9JcbOAU= -github.com/google/gitprotocolio v0.0.0-20180630173033-8d2b3b1c37f6/go.mod h1:ZWHBOUQ5S1VRRi7XG5FXPOxxIwwsK4suKZ+MSgq2zTg= -github.com/google/uuid v1.1.0 h1:Jf4mxPC/ziBnoPIdpQdPJ9OeiomAUHLvxmPRSPH9m4s= -github.com/google/uuid v1.1.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= -github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= -github.com/jackmordaunt/icns v0.0.0-20181231085925-4f16af745526/go.mod h1:UQkeMHVoNcyXYq9otUupF7/h/2tmHlhrS2zw7ZVvUqc= -github.com/josephspurrier/goversioninfo v0.0.0-20200309025242-14b0ab84c6ca/go.mod h1:eJTEwMjXb7kZ633hO3Ln9mBUCOjX2+FlTljvpl9SYdE= -github.com/kr/pty v1.1.1 h1:VkoXIwSboBpnk99O/KFauAEILuNHv5DVFKZMBN/gUgw= -github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= -github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= -github.com/lucor/goinfo v0.0.0-20200401173949-526b5363a13a/go.mod h1:ORP3/rB5IsulLEBwQZCJyyV6niqmI7P4EWSmkug+1Ng= -github.com/maruel/rs v0.0.0-20150922171536-2c81c4312fe4 h1:u9jwvcKbQpghIXgNl/EOL8hzhAFXh4ePrEP493W3tNA= -github.com/maruel/rs v0.0.0-20150922171536-2c81c4312fe4/go.mod h1:kcRFpEzolcEklV6rD7W95mG49/sbdX/PlFmd7ni3RvA= -github.com/nareix/joy4 v0.0.0-20200507095837-05a4ffbb5369 h1:Yp0zFEufLz0H7jzffb4UPXijavlyqlYeOg7dcyVUNnQ= -github.com/nareix/joy4 v0.0.0-20200507095837-05a4ffbb5369/go.mod h1:aFJ1ZwLjvHN4yEzE5Bkz8rD8/d8Vlj3UIuvz2yfET7I= -github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8= -github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= -github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= -github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= -github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/shylinux/contexts v1.1.1 h1:15+xC/IN0ycE+H8FGto13Em/MQph/Nfk6qcEw0ffvaU= -github.com/shylinux/toolkits v0.1.6 h1:x+Bs+oijuS11TWGAsdCEgwDbCS35uJvnP4jYDETJqig= -github.com/shylinux/toolkits v0.1.6/go.mod h1:Y68Ot6xOmo1bun67YvqC3chDGeU2gDxtsUnvVDGJm4g= -github.com/shylinux/toolkits v0.1.7 h1:RDUpZNTgnob6vaKJvJgNVaDE2UZRURjLG4StpSaAWF8= -github.com/shylinux/toolkits v0.1.7/go.mod h1:Y68Ot6xOmo1bun67YvqC3chDGeU2gDxtsUnvVDGJm4g= -github.com/shylinux/toolkits v0.1.8 h1:Lh5HkR1aRzhOOVu9eHwZ5y7dfW7hcFy29IR5tQ5qUeM= -github.com/shylinux/toolkits v0.1.8/go.mod h1:Y68Ot6xOmo1bun67YvqC3chDGeU2gDxtsUnvVDGJm4g= -github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e h1:MRM5ITcdelLK2j1vwZ3Je0FKVCfqOLp5zO6trqMLYs0= -github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e/go.mod h1:XV66xRDqSt+GTGFMVlhk3ULuV0y9ZmzeVGR4mloJI3M= -github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ= -github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= -github.com/srwiley/oksvg v0.0.0-20200311192757-870daf9aa564 h1:HunZiaEKNGVdhTRQOVpMmj5MQnGnv+e8uZNu3xFLgyM= -github.com/srwiley/oksvg v0.0.0-20200311192757-870daf9aa564/go.mod h1:afMbS0qvv1m5tfENCwnOdZGOF8RGR/FsZ7bvBxQGZG4= -github.com/srwiley/rasterx v0.0.0-20200120212402-85cb7272f5e9 h1:m59mIOBO4kfcNCEzJNy71UkeF4XIx2EVmL9KLwDQdmM= -github.com/srwiley/rasterx v0.0.0-20200120212402-85cb7272f5e9/go.mod h1:mvWM0+15UqyrFKqdRjY6LuAVJR0HOVhJlEgZ5JWtSWU= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= -github.com/tuotoo/qrcode v0.0.0-20190222102259-ac9c44189bf2 h1:BWVtt2VBY+lmVDu9MGKqLGKl04B+iRHcrW1Ptyi/8tg= -github.com/tuotoo/qrcode v0.0.0-20190222102259-ac9c44189bf2/go.mod h1:lPnW9HVS0vJdeYyQtOvIvlXgZPNhUAhwz+z5r8AJk0Y= -github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= -golang.org/dl v0.0.0-20190829154251-82a15e2f2ead/go.mod h1:IUMfjQLJQd4UTqG1Z90tenwKoCX93Gn3MAQJMOSBsDQ= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 h1:ObdrDkeb4kJdCP557AjRjq69pTHfNouLtWZG7j9rPN8= -golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= -golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= -golang.org/x/image v0.0.0-20200430140353-33d19683fad8 h1:6WW6V3x1P/jokJBpRQYUJnMHRP6isStQwCozxnU7XQw= -golang.org/x/image v0.0.0-20200430140353-33d19683fad8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= -golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= -golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e h1:3G+cUijn7XD+S4eJFddp53Pv7+slrESplyjG25HgL+k= -golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= -golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200327173247-9dae0f8f5775 h1:TC0v2RSO1u2kn1ZugjrFXkRZAEaqMN/RW+OTZkBzmLE= -golang.org/x/sys v0.0.0-20200327173247-9dae0f8f5775/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= -golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= -golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/tools v0.0.0-20190808195139-e713427fea3f/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.0.0-20200328031815-3db5fc6bac03/go.mod h1:Sl4aGygMT6LrqrWclx+PTx3U+LnKx/seiNR+3G19Ar8= -golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/init.go b/init.go index 5d821be1..f6040c3e 100644 --- a/init.go +++ b/init.go @@ -138,7 +138,7 @@ func Run(arg ...string) string { Pulse.root = Pulse Pulse.Option("cache.limit", "30") Pulse.Option("begin_time", Pulse.Time()) - + _log_disable = false switch kit.Select("", arg, 0) { case "space", "serve": if _log_disable = false; frame.Begin(Pulse.Spawns(), arg...).Start(Pulse, arg...) { @@ -150,6 +150,7 @@ func Run(arg ...string) string { default: if m := Pulse.Cmdy(arg); m.Result() == "" { m.Table() + m.Sleep("100ms") } } @@ -157,6 +158,7 @@ func Run(arg ...string) string { } var names = map[string]interface{}{} +var ErrNameExists = "name already exists: " func Name(name string, value interface{}) string { if s, ok := names[name]; ok { @@ -165,7 +167,7 @@ func Name(name string, value interface{}) string { case *Context: last = s.Name } - panic(NewError(4, ErrNameExists, name, "last:", last)) + panic(NewError(1, ErrNameExists, name, "last: ", last)) } names[name] = value diff --git a/logs.go b/logs.go index cb7f19e2..c861314e 100644 --- a/logs.go +++ b/logs.go @@ -9,14 +9,10 @@ import ( ) var ErrWarn = "warn: " +var ErrNotFound = "not found: " var ErrNotLogin = "not login: " var ErrNotAuth = "not auth: " var ErrNotJoin = "not join: " -var ErrNotFound = "not found: " -var ErrStart = "err start: " -var ErrNameExists = "name already exists:" - -var _log_disable = true type Error struct { Arg []interface{} @@ -27,74 +23,67 @@ func (e *Error) Error() string { return e.FileLine + " " + strings.Join(kit.Simple(e.Arg), " ") } func NewError(n int, arg ...interface{}) *Error { - return &Error{Arg: arg, FileLine: kit.FileLine(n, 3)} + return &Error{Arg: arg, FileLine: kit.FileLine(n+3, 3)} } -var Log func(*Message, string, string) +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 str = strings.TrimSpace(kit.Format(str, arg...)); Log != nil { - // 日志模块 - Log(m, level, str) - } if _log_disable { - return m + return m // 禁用日志 + } + if str = strings.TrimSpace(kit.Format(str, arg...)); Log != nil { + Log(m, m.Format("prefix"), level, str) + // 日志分流 } // 日志颜色 prefix, suffix := "", "" switch level { - case LOG_ENABLE, LOG_IMPORT, LOG_CREATE, LOG_INSERT, LOG_MODIFY, LOG_EXPORT: + case LOG_IMPORT, LOG_CREATE, LOG_INSERT, LOG_MODIFY, LOG_EXPORT: prefix, suffix = "\033[36;44m", "\033[0m" - - case LOG_LISTEN, LOG_SIGNAL, LOG_TIMERS, LOG_EVENTS: + case LOG_ENABLE, LOG_LISTEN, LOG_SIGNAL, LOG_TIMERS, LOG_EVENTS: prefix, suffix = "\033[33m", "\033[0m" case LOG_CMDS, LOG_START, LOG_SERVE: prefix, suffix = "\033[32m", "\033[0m" - case LOG_AUTH, LOG_CONF, LOG_COST: - prefix, suffix = "\033[33m", "\033[0m" case LOG_WARN, LOG_ERROR, LOG_CLOSE: prefix, suffix = "\033[31m", "\033[0m" + case LOG_AUTH, LOG_COST: + prefix, suffix = "\033[33m", "\033[0m" } // 文件行号 switch level { - // case LOG_CMDS, LOG_INFO, LOG_WARN, "refer", "form": case LOG_CMDS, LOG_INFO, "refer", "form": - case "register", "begin": + case "begin": default: suffix += " " + kit.FileLine(3, 3) } - if len(BinPack) > 0 { - prefix, suffix = "", "" - } + // 长度截断 switch level { case LOG_INFO, "send", "recv": - if len(str) > 1000 { - str = str[:1000] + if len(str) > 1024 { + str = str[:1024] } } + // 输出日志 - log.Info(fmt.Sprintf("%02d %9s %s%s %s%s", m.code, fmt.Sprintf("%4s->%-4s", m.source.Name, m.target.Name), - prefix, level, str, suffix)) + log.Info(fmt.Sprintf("%02d %9s %s%s %s%s", m.code, + fmt.Sprintf("%4s->%-4s", m.source.Name, m.target.Name), prefix, level, str, suffix)) return m } func (m *Message) Log(level string, str string, arg ...interface{}) *Message { return m.log(level, str, arg...) } -func (m *Message) Logs(level string, arg ...interface{}) *Message { - list := []string{} - for i := 0; i < len(arg)-1; i += 2 { - list = append(list, fmt.Sprintf("%v: %v", arg[i], arg[i+1])) - } - m.log(level, strings.Join(list, " ")) - return m -} func (m *Message) Info(str string, arg ...interface{}) *Message { return m.log(LOG_INFO, str, arg...) } +func (m *Message) Cost(str string, arg ...interface{}) *Message { + return m.log(LOG_COST, "%s: %s", m.Format("cost"), kit.Format(str, arg...)) +} func (m *Message) Warn(err bool, arg ...interface{}) bool { if err { list := kit.Simple(arg...) @@ -126,11 +115,11 @@ func log_fields(arg ...interface{}) string { } return strings.Join(list, " ") } -func (m *Message) Log_IMPORT(arg ...interface{}) *Message { - return m.log(LOG_IMPORT, log_fields(arg...)) +func (m *Message) Logs(level string, arg ...interface{}) *Message { + return m.log(level, log_fields(arg...)) } -func (m *Message) Log_EXPORT(arg ...interface{}) *Message { - return m.log(LOG_EXPORT, log_fields(arg...)) +func (m *Message) Log_AUTH(arg ...interface{}) *Message { + return m.log(LOG_AUTH, log_fields(arg...)) } func (m *Message) Log_CREATE(arg ...interface{}) *Message { return m.log(LOG_CREATE, log_fields(arg...)) @@ -150,13 +139,9 @@ func (m *Message) Log_SELECT(arg ...interface{}) *Message { func (m *Message) Log_MODIFY(arg ...interface{}) *Message { return m.log(LOG_MODIFY, log_fields(arg...)) } - -func (m *Message) Log_CONF(arg ...interface{}) *Message { - return m.log(LOG_CONF, log_fields(arg...)) +func (m *Message) Log_IMPORT(arg ...interface{}) *Message { + return m.log(LOG_IMPORT, log_fields(arg...)) } -func (m *Message) Log_AUTH(arg ...interface{}) *Message { - return m.log(LOG_AUTH, log_fields(arg...)) -} -func (m *Message) Cost(str string, arg ...interface{}) *Message { - return m.log(LOG_COST, "%s: %s", m.Format("cost"), kit.Format(str, arg...)) +func (m *Message) Log_EXPORT(arg ...interface{}) *Message { + return m.log(LOG_EXPORT, log_fields(arg...)) } diff --git a/trash/docker/docker.go b/misc/trash/docker/docker.go similarity index 100% rename from trash/docker/docker.go rename to misc/trash/docker/docker.go diff --git a/trash/fyne/fyne.go b/misc/trash/fyne/fyne.go similarity index 100% rename from trash/fyne/fyne.go rename to misc/trash/fyne/fyne.go diff --git a/trash/idc/idc.go b/misc/trash/idc/idc.go similarity index 100% rename from trash/idc/idc.go rename to misc/trash/idc/idc.go diff --git a/trash/idc/idc.js b/misc/trash/idc/idc.js similarity index 100% rename from trash/idc/idc.js rename to misc/trash/idc/idc.js diff --git a/trash/idc/idc.shy b/misc/trash/idc/idc.shy similarity index 100% rename from trash/idc/idc.shy rename to misc/trash/idc/idc.shy diff --git a/trash/md/md.go b/misc/trash/md/md.go similarity index 100% rename from trash/md/md.go rename to misc/trash/md/md.go diff --git a/trash/md/md.js b/misc/trash/md/md.js similarity index 100% rename from trash/md/md.js rename to misc/trash/md/md.js diff --git a/trash/md/md.shy b/misc/trash/md/md.shy similarity index 100% rename from trash/md/md.shy rename to misc/trash/md/md.shy diff --git a/trash/pi/pi.go b/misc/trash/pi/pi.go similarity index 100% rename from trash/pi/pi.go rename to misc/trash/pi/pi.go diff --git a/trash/railway/railway.go b/misc/trash/railway/railway.go similarity index 100% rename from trash/railway/railway.go rename to misc/trash/railway/railway.go diff --git a/trash/railway/railway.js b/misc/trash/railway/railway.js similarity index 100% rename from trash/railway/railway.js rename to misc/trash/railway/railway.js diff --git a/trash/railway/railway.shy b/misc/trash/railway/railway.shy similarity index 100% rename from trash/railway/railway.shy rename to misc/trash/railway/railway.shy diff --git a/type.go b/type.go index 77c2bbb5..dc4150f1 100644 --- a/type.go +++ b/type.go @@ -70,6 +70,9 @@ type Server interface { } func (c *Context) ID() int32 { + if c == nil { + return 1 + } return atomic.AddInt32(&c.id, 1) } func (c *Context) Cap(key string, arg ...interface{}) string {