1
0
mirror of https://shylinux.com/x/icebergs synced 2025-04-26 09:34:05 +08:00
This commit is contained in:
shaoying 2020-09-30 13:21:55 +08:00
parent 3c7ac779c3
commit b5d5de5f3a
23 changed files with 208 additions and 231 deletions

View File

@ -10,6 +10,9 @@ import (
"strings" "strings"
) )
const (
ErrStart = "daemon start: "
)
const ( const (
StatusError = "error" StatusError = "error"
StatusStart = "start" 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"))) 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 return
} }
@ -42,7 +45,7 @@ func _daemon_show(m *ice.Message, cmd *exec.Cmd, out, err string) {
m.Gos(m, func(m *ice.Message) { m.Gos(m, func(m *ice.Message) {
if e := cmd.Wait(); e != nil { 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{}) { m.Richs(DAEMON, nil, h, func(key string, value map[string]interface{}) {
kit.Value(value, kit.MDB_STATUS, StatusError) kit.Value(value, kit.MDB_STATUS, StatusError)
kit.Value(value, kit.Keys(kit.MDB_EXTRA, kit.MDB_ERROR), e) kit.Value(value, kit.Keys(kit.MDB_EXTRA, kit.MDB_ERROR), e)

View File

@ -1,32 +1,29 @@
package log package log
import ( import (
"github.com/shylinux/icebergs" ice "github.com/shylinux/icebergs"
"github.com/shylinux/toolkits" kit "github.com/shylinux/toolkits"
"bufio" "bufio"
"os"
"path" "path"
) )
type Log struct { type Log struct {
m *ice.Message m *ice.Message
p string
l string l string
s string s string
} }
type Frame struct { type Frame struct{ p chan *Log }
p chan *Log
}
func (f *Frame) Spawn(m *ice.Message, c *ice.Context, arg ...string) ice.Server { func (f *Frame) Spawn(m *ice.Message, c *ice.Context, arg ...string) ice.Server {
return &Frame{} return &Frame{}
} }
func (f *Frame) Begin(m *ice.Message, arg ...string) ice.Server { func (f *Frame) Begin(m *ice.Message, arg ...string) ice.Server {
// 日志管道 f.p = make(chan *Log, 4096)
f.p = make(chan *Log, 1024) ice.Log = func(msg *ice.Message, p, l, s string) {
ice.Log = func(msg *ice.Message, level string, str string) { f.p <- &Log{m: msg, p: p, l: l, s: s}
f.p <- &Log{m: msg, l: level, s: str}
} }
return f return f
} }
@ -37,14 +34,12 @@ func (f *Frame) Start(m *ice.Message, arg ...string) bool {
if !ok { if !ok {
break 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 := m.Confv("file", file+".file").(*bufio.Writer)
bio.WriteString(l.m.Format("prefix"))
bio.WriteString(l.p)
bio.WriteString(" ") bio.WriteString(" ")
if p, ok := view["prefix"].(string); ok { if p, ok := view["prefix"].(string); ok {
bio.WriteString(p) bio.WriteString(p)
@ -65,72 +60,54 @@ func (f *Frame) Close(m *ice.Message, arg ...string) bool {
return true return true
} }
const (
ERROR = "error"
TRACE = "trace"
)
var Index = &ice.Context{Name: "log", Help: "日志模块", var Index = &ice.Context{Name: "log", Help: "日志模块",
Configs: map[string]*ice.Config{ Configs: map[string]*ice.Config{
"file": &ice.Config{Name: "file", Help: "日志文件", Value: kit.Dict( "file": {Name: "file", Help: "日志文件", Value: kit.Dict(
"watch", kit.Dict("path", "var/log/watch.log"), "watch", kit.Dict("path", "var/log/watch.log", "list", []string{
"bench", kit.Dict("path", "var/log/bench.log"), ice.LOG_CREATE, ice.LOG_REMOVE,
"error", kit.Dict("path", "var/log/error.log"), ice.LOG_INSERT, ice.LOG_DELETE,
"trace", kit.Dict("path", "var/log/trace.log"), 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( "view": {Name: "view", Help: "日志格式", Value: kit.Dict(
"red", kit.Dict("prefix", "\033[31m", "suffix", "\033[0m"), "green", kit.Dict("prefix", "\033[32m", "suffix", "\033[0m", "list", []string{
"green", kit.Dict("prefix", "\033[32m", "suffix", "\033[0m"), ice.LOG_START, ice.LOG_SERVE,
"yellow", kit.Dict("prefix", "\033[33m", "suffix", "\033[0m"), ice.LOG_CMDS,
)}, }),
"show": &ice.Config{Name: "show", Help: "日志分流", Value: kit.Dict( "yellow", kit.Dict("prefix", "\033[33m", "suffix", "\033[0m", "list", []string{
// 数据 ice.LOG_AUTH, ice.LOG_COST,
ice.LOG_ENABLE, kit.Dict("file", "watch"), }),
ice.LOG_IMPORT, kit.Dict("file", "watch"), "red", kit.Dict("prefix", "\033[31m", "suffix", "\033[0m", "list", []string{
ice.LOG_EXPORT, kit.Dict("file", "watch"), ice.LOG_WARN, ice.LOG_CLOSE,
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"),
)}, )},
"show": {Name: "show", Help: "日志分流", Value: kit.Dict()},
}, },
Commands: map[string]*ice.Command{ Commands: map[string]*ice.Command{
ice.CTX_INIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { ice.CTX_INIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
if os.Getenv("ctx_mod") != "" { m.Confm("view", nil, func(key string, value map[string]interface{}) {
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, "view"), 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)
}
}) })
} })
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) { ice.CTX_EXIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
if f, ok := m.Target().Server().(*Frame); ok { if f, ok := m.Target().Server().(*Frame); ok {

View File

@ -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) { 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)) m.Cmdy(mdb.DELETE, CONNECT, "", mdb.HASH, kit.MDB_HASH, m.Option(kit.MDB_HASH))
}}, }},

View File

@ -17,6 +17,60 @@ import (
"time" "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{}) { func Render(msg *ice.Message, cmd string, args ...interface{}) {
defer func() { msg.Log_EXPORT(mdb.RENDER, cmd, kit.MDB_TEXT, args) }() 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 var r io.Reader
switch m.Cap(ice.CTX_STREAM, f.source) { switch m.Cap(ice.CTX_STREAM, f.source) {
case STDIO: // 终端交互 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("_option", ice.MSG_USERNAME)
m.Option(ice.MSG_USERNAME, cli.UserName) m.Option(ice.MSG_USERNAME, cli.UserName)

View File

@ -1,14 +1,17 @@
package ssh package ssh
import ( import (
"os"
ice "github.com/shylinux/icebergs" ice "github.com/shylinux/icebergs"
"github.com/shylinux/icebergs/base/ctx" "github.com/shylinux/icebergs/base/ctx"
"github.com/shylinux/icebergs/base/mdb" "github.com/shylinux/icebergs/base/mdb"
"github.com/shylinux/icebergs/base/tcp" "github.com/shylinux/icebergs/base/tcp"
kit "github.com/shylinux/toolkits" kit "github.com/shylinux/toolkits"
"golang.org/x/crypto/ssh"
"io" "io"
"golang.org/x/crypto/ssh"
) )
func _ssh_sess(m *ice.Message, h string, client *ssh.Client) (*ssh.Session, error) { 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 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 ( const (
TTY = "tty" TTY = "tty"
ENV = "env" ENV = "env"
@ -72,6 +90,22 @@ func init() {
m.Sleep("300ms") m.Sleep("300ms")
m.Cmdy(SESSION, m.Option(kit.MDB_HASH)) 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) { 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)) 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") m.Option(mdb.FIELDS, "time,hash,status,count,connect")
if m.Cmdy(mdb.SELECT, SESSION, "", mdb.HASH, kit.MDB_HASH, arg); len(arg) == 0 { 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.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 return

View File

@ -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) { func _bench_show(m *ice.Message, nconn, nreq int64, list []*http.Request) {
m.Log_CONF(NCONN, nconn, NREQS, nreq)
var body int64 var body int64
s, e := bench.HTTP(nconn, nreq, list, func(req *http.Request, res *http.Response) { s, e := bench.HTTP(nconn, nreq, list, func(req *http.Request, res *http.Response) {
n, _ := io.Copy(ioutil.Discard, res.Body) n, _ := io.Copy(ioutil.Discard, res.Body)

4
go.mod
View File

@ -3,10 +3,6 @@ module github.com/shylinux/icebergs
go 1.13 go 1.13
require ( 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/gorilla/websocket v1.4.2
github.com/kr/pty v1.1.1 github.com/kr/pty v1.1.1
github.com/nareix/joy4 v0.0.0-20200507095837-05a4ffbb5369 github.com/nareix/joy4 v0.0.0-20200507095837-05a4ffbb5369

98
go.sum
View File

@ -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=

View File

@ -138,7 +138,7 @@ func Run(arg ...string) string {
Pulse.root = Pulse Pulse.root = Pulse
Pulse.Option("cache.limit", "30") Pulse.Option("cache.limit", "30")
Pulse.Option("begin_time", Pulse.Time()) Pulse.Option("begin_time", Pulse.Time())
_log_disable = false
switch kit.Select("", arg, 0) { switch kit.Select("", arg, 0) {
case "space", "serve": case "space", "serve":
if _log_disable = false; frame.Begin(Pulse.Spawns(), arg...).Start(Pulse, arg...) { if _log_disable = false; frame.Begin(Pulse.Spawns(), arg...).Start(Pulse, arg...) {
@ -150,6 +150,7 @@ func Run(arg ...string) string {
default: default:
if m := Pulse.Cmdy(arg); m.Result() == "" { if m := Pulse.Cmdy(arg); m.Result() == "" {
m.Table() m.Table()
m.Sleep("100ms")
} }
} }
@ -157,6 +158,7 @@ func Run(arg ...string) string {
} }
var names = map[string]interface{}{} var names = map[string]interface{}{}
var ErrNameExists = "name already exists: "
func Name(name string, value interface{}) string { func Name(name string, value interface{}) string {
if s, ok := names[name]; ok { if s, ok := names[name]; ok {
@ -165,7 +167,7 @@ func Name(name string, value interface{}) string {
case *Context: case *Context:
last = s.Name last = s.Name
} }
panic(NewError(4, ErrNameExists, name, "last:", last)) panic(NewError(1, ErrNameExists, name, "last: ", last))
} }
names[name] = value names[name] = value

77
logs.go
View File

@ -9,14 +9,10 @@ import (
) )
var ErrWarn = "warn: " var ErrWarn = "warn: "
var ErrNotFound = "not found: "
var ErrNotLogin = "not login: " var ErrNotLogin = "not login: "
var ErrNotAuth = "not auth: " var ErrNotAuth = "not auth: "
var ErrNotJoin = "not join: " var ErrNotJoin = "not join: "
var ErrNotFound = "not found: "
var ErrStart = "err start: "
var ErrNameExists = "name already exists:"
var _log_disable = true
type Error struct { type Error struct {
Arg []interface{} Arg []interface{}
@ -27,74 +23,67 @@ func (e *Error) Error() string {
return e.FileLine + " " + strings.Join(kit.Simple(e.Arg), " ") return e.FileLine + " " + strings.Join(kit.Simple(e.Arg), " ")
} }
func NewError(n int, arg ...interface{}) *Error { 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 { 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 { 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 := "", "" prefix, suffix := "", ""
switch level { 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" prefix, suffix = "\033[36;44m", "\033[0m"
case LOG_ENABLE, LOG_LISTEN, LOG_SIGNAL, LOG_TIMERS, LOG_EVENTS:
case LOG_LISTEN, LOG_SIGNAL, LOG_TIMERS, LOG_EVENTS:
prefix, suffix = "\033[33m", "\033[0m" prefix, suffix = "\033[33m", "\033[0m"
case LOG_CMDS, LOG_START, LOG_SERVE: case LOG_CMDS, LOG_START, LOG_SERVE:
prefix, suffix = "\033[32m", "\033[0m" 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: case LOG_WARN, LOG_ERROR, LOG_CLOSE:
prefix, suffix = "\033[31m", "\033[0m" prefix, suffix = "\033[31m", "\033[0m"
case LOG_AUTH, LOG_COST:
prefix, suffix = "\033[33m", "\033[0m"
} }
// 文件行号 // 文件行号
switch level { switch level {
// case LOG_CMDS, LOG_INFO, LOG_WARN, "refer", "form":
case LOG_CMDS, LOG_INFO, "refer", "form": case LOG_CMDS, LOG_INFO, "refer", "form":
case "register", "begin": case "begin":
default: default:
suffix += " " + kit.FileLine(3, 3) suffix += " " + kit.FileLine(3, 3)
} }
if len(BinPack) > 0 {
prefix, suffix = "", ""
}
// 长度截断
switch level { switch level {
case LOG_INFO, "send", "recv": case LOG_INFO, "send", "recv":
if len(str) > 1000 { if len(str) > 1024 {
str = str[:1000] 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), log.Info(fmt.Sprintf("%02d %9s %s%s %s%s", m.code,
prefix, level, str, suffix)) fmt.Sprintf("%4s->%-4s", m.source.Name, m.target.Name), prefix, level, str, suffix))
return m return m
} }
func (m *Message) Log(level string, str string, arg ...interface{}) *Message { func (m *Message) Log(level string, str string, arg ...interface{}) *Message {
return m.log(level, str, arg...) 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 { func (m *Message) Info(str string, arg ...interface{}) *Message {
return m.log(LOG_INFO, str, arg...) 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 { func (m *Message) Warn(err bool, arg ...interface{}) bool {
if err { if err {
list := kit.Simple(arg...) list := kit.Simple(arg...)
@ -126,11 +115,11 @@ func log_fields(arg ...interface{}) string {
} }
return strings.Join(list, " ") return strings.Join(list, " ")
} }
func (m *Message) Log_IMPORT(arg ...interface{}) *Message { func (m *Message) Logs(level string, arg ...interface{}) *Message {
return m.log(LOG_IMPORT, log_fields(arg...)) return m.log(level, log_fields(arg...))
} }
func (m *Message) Log_EXPORT(arg ...interface{}) *Message { func (m *Message) Log_AUTH(arg ...interface{}) *Message {
return m.log(LOG_EXPORT, log_fields(arg...)) return m.log(LOG_AUTH, log_fields(arg...))
} }
func (m *Message) Log_CREATE(arg ...interface{}) *Message { func (m *Message) Log_CREATE(arg ...interface{}) *Message {
return m.log(LOG_CREATE, log_fields(arg...)) 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 { func (m *Message) Log_MODIFY(arg ...interface{}) *Message {
return m.log(LOG_MODIFY, log_fields(arg...)) return m.log(LOG_MODIFY, log_fields(arg...))
} }
func (m *Message) Log_IMPORT(arg ...interface{}) *Message {
func (m *Message) Log_CONF(arg ...interface{}) *Message { return m.log(LOG_IMPORT, log_fields(arg...))
return m.log(LOG_CONF, log_fields(arg...))
} }
func (m *Message) Log_AUTH(arg ...interface{}) *Message { func (m *Message) Log_EXPORT(arg ...interface{}) *Message {
return m.log(LOG_AUTH, log_fields(arg...)) return m.log(LOG_EXPORT, 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...))
} }

View File

@ -70,6 +70,9 @@ type Server interface {
} }
func (c *Context) ID() int32 { func (c *Context) ID() int32 {
if c == nil {
return 1
}
return atomic.AddInt32(&c.id, 1) return atomic.AddInt32(&c.id, 1)
} }
func (c *Context) Cap(key string, arg ...interface{}) string { func (c *Context) Cap(key string, arg ...interface{}) string {