Skip to content

Commit

Permalink
feat : display the print logs of the driver side #26
Browse files Browse the repository at this point in the history
  • Loading branch information
taotao committed May 17, 2024
1 parent 0f99849 commit ccfc876
Show file tree
Hide file tree
Showing 10 changed files with 227 additions and 55 deletions.
5 changes: 5 additions & 0 deletions driver/bot/behavior/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ func (a *Node) ID() string {
return a.id
}

// Name 返回节点的名称
func (a *Node) Name() string {
return ""
}

func (a *Node) Type() string {
return a.ty
}
Expand Down
22 changes: 11 additions & 11 deletions driver/bot/behavior/tick.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ ext:
return state, changestr, err
}

func (t *Tick) Do(mod Mode) (state string, end bool) {
func (t *Tick) Do(mod Mode) (state string, end bool, logs []string) {

nods := t.blackboard.GetOpenNods()
t.blackboard.ThreadInfoReset()
Expand All @@ -94,33 +94,33 @@ func (t *Tick) Do(mod Mode) (state string, end bool) {
err = n.onTick(t)

state, msg, parseerr = t.stateCheck(mod, n.getType())

// thread 信息用于编辑器标记运行时节点信息(展示项
threadInfo := ThreadInfo{
Number: n.getBase().getThread(),
CurNod: n.getBase().ID(),
Change: msg,
}

if err != nil {
threadInfo.ErrMsg = fmt.Sprintf("tick err %v", err.Error())
fmt.Println("tick err", threadInfo.ErrMsg)
logs = append(logs, fmt.Sprintf("<b><u>check err</u></b> thread:%v name:%v id:%v\n%v",
n.getBase().getThread(),
n.getBase().ID(),
n.getBase().Name(),
err.Error()),
)
}
if parseerr != nil {
threadInfo.ErrMsg = fmt.Sprintf("%v parse err %v", threadInfo.ErrMsg, parseerr.Error())
fmt.Println("tick parse err", threadInfo.ErrMsg)
//threadInfo.ErrMsg = fmt.Sprintf("%v parse err %v", threadInfo.ErrMsg, parseerr.Error())
}

if state != Succ {
if state == Exit {
end = true
} else if state == Break {
end = true
threadInfo.ErrMsg = fmt.Sprintf("script break err %v", msg)
fmt.Println("tick break err", threadInfo.ErrMsg)
} else if state == Error {
// 节点脚本出错,脚本逻辑自行抛出的错误
threadInfo.ErrMsg = fmt.Sprintf("script err %v", msg)
fmt.Println("tick script err", threadInfo.ErrMsg)
//threadInfo.ErrMsg = fmt.Sprintf("script err %v", msg)
}
}

Expand All @@ -143,5 +143,5 @@ func (t *Tick) Do(mod Mode) (state string, end bool) {
}

ext:
return state, end
return state, end, logs
}
45 changes: 41 additions & 4 deletions driver/bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,21 @@ func NewWithBehaviorTree(path string, bt *behavior.Tree, mode behavior.Mode, nam
fmt.Println("set bot name", err.Error())
}

bot.addLog(fmt.Sprintf("create bot id %v name %v success", bot.id, bot.name))

return bot
}

func (b *Bot) loopThread(doneCh chan<- string, errch chan<- ErrInfo) {

for {
state, end := b.tick.Do(b.mod)
state, end, logs := b.tick.Do(b.mod)
if len(logs) != 0 {
for _, log := range logs {
b.addLog(log)
}
}

if end {
doneCh <- b.id
goto ext
Expand Down Expand Up @@ -208,7 +216,13 @@ func (b *Bot) RunByBlock() error {
}()

for {
state, end := b.tick.Do(b.mod)
state, end, logs := b.tick.Do(b.mod)
if len(logs) != 0 {
for _, log := range logs {
b.addLog(log)
}
}

if end {
return nil
}
Expand Down Expand Up @@ -262,8 +276,24 @@ func (b *Bot) close() {
pool.FreeState(b.bs)
}

fmt.Println("bot", b.Name(), "batch", b.batch, "idx", b.id, "close")
b.addLog(fmt.Sprintf("close bot id %v name %v success", b.id, b.name))
}

// PopLog - 弹出一条日志
func (b *Bot) PopLog() string {
line := b.bs.LogMod.Pop()

return line
}

func (b *Bot) addLog(log string) {
fmt.Println("=>", log)

log = time.Now().Format("2006-01-02 15:04:05") + " =================>\n" + log

if b.mod != behavior.Thread {
b.bs.LogMod.Push(log)
}
}

type State int32
Expand All @@ -281,7 +311,14 @@ func (b *Bot) RunByStep() State {
stepmu.Lock()
defer stepmu.Unlock()

state, end := b.tick.Do(b.mod)
// 这边的错误日志需要记录下
state, end, logs := b.tick.Do(b.mod)
if len(logs) != 0 {
for _, log := range logs {
b.addLog(log)
}
}

if end {
return SEnd
}
Expand Down
12 changes: 12 additions & 0 deletions driver/bot/pool/lua_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type BotState struct {
base64Mod *script.Base64Module
mgoMod *script.MgoModule
md5Mod *script.MD5Module
LogMod *script.LogModule
}

func (pl *lStatePool) Get() *BotState {
Expand Down Expand Up @@ -50,6 +51,7 @@ func _new_state() *BotState {
base64Mod: &script.Base64Module{},
mgoMod: &script.MgoModule{},
md5Mod: &script.MD5Module{},
LogMod: &script.LogModule{},
}

b.L.PreloadModule("proto", b.protoMod.Loader)
Expand All @@ -60,6 +62,7 @@ func _new_state() *BotState {
b.L.PreloadModule("base64", b.base64Mod.Loader)
b.L.PreloadModule("mgo", b.mgoMod.Loader)
b.L.PreloadModule("md5", b.md5Mod.Loader)
b.L.PreloadModule("log", b.LogMod.Loader)

return b
}
Expand All @@ -80,7 +83,14 @@ func GetState() *BotState {
return luaPool.Get()
}

func (bs *BotState) Clean() {
// module clean
bs.LogMod.Clean()
}

func PutState(state *BotState) {
state.Clean()

luaPool.Put(state)
}

Expand All @@ -89,6 +99,8 @@ func NewState() *BotState {
}

func FreeState(state *BotState) {
state.Clean()

state.L.Close()
}

Expand Down
9 changes: 9 additions & 0 deletions driver/openapi/api_def.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,12 @@ type PrefabSetTagsReq struct {
Name string `json:"name"`
Tags []string `json:"tags"`
}

// ------------------------ runtime ------------------------
type RuntimeInfoReq struct {
ID string
}

type RuntimeInfoRes struct {
Msg string
}
39 changes: 39 additions & 0 deletions driver/openapi/api_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,43 @@ EXT:
return nil
}

func BotRuntimeInfo(ctx echo.Context) error {
code := Succ
var b *bot.Bot
req := &RuntimeInfoReq{}
res := &response{}
body := &RuntimeInfoRes{}

bts, err := ioutil.ReadAll(ctx.Request().Body)
if err != nil {
code = ErrContentRead // tmp
fmt.Println(err.Error())
goto EXT
}
err = json.Unmarshal(bts, &req)
if err != nil {
code = ErrContentRead // tmp
fmt.Println(err.Error())
goto EXT
}

b = factory.Global.FindBot(req.ID)
if b == nil {
code = ErrCantFindBot
goto EXT
}

body.Msg = b.PopLog()

EXT:
res.Code = int(code)
res.Msg = errmap[code]
res.Body = body

ctx.JSON(http.StatusOK, res)
return nil
}

func ReqPrint() echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
Expand All @@ -774,6 +811,8 @@ func Route(e *echo.Echo) {
return nil
})

e.POST("/runtime.info", BotRuntimeInfo)

e.POST("/file.uploadTxt", FileTextUpload)
e.POST("/file.uploadBlob", FileBlobUpload)
e.POST("/file.remove", FileRemove)
Expand Down
58 changes: 58 additions & 0 deletions driver/script/module/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package script

import (
"sync"

lua "github.com/yuin/gopher-lua"
)

type LogModule struct {
logs []string
sync.Mutex
}

func (lm *LogModule) Loader(L *lua.LState) int {
mod := L.SetFuncs(L.NewTable(), map[string]lua.LGFunction{
"info": lm.info,
})
L.Push(mod)
return 1
}

// info - 脚本端添加一行日志
func (lm *LogModule) info(L *lua.LState) int {
lm.Lock()
defer lm.Unlock()

info := L.ToString(1)
lm.logs = append(lm.logs, info)

return 0
}

// Push - golang端添加一行日志
func (lm *LogModule) Push(log string) {
lm.Lock()
lm.logs = append(lm.logs, log)
lm.Unlock()
}

func (lm *LogModule) Pop() string {
lm.Lock()
defer lm.Unlock()

n := len(lm.logs)
if n == 0 {
return ""
}
x := lm.logs[n-1]
lm.logs = lm.logs[0 : n-1]

return x
}

func (lm *LogModule) Clean() {
lm.Lock()
lm.logs = []string{}
lm.Unlock()
}
5 changes: 4 additions & 1 deletion editor/src/constant/api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ const Api = {
ConfigSystemInfo : "config.sys.info",
ConfigSystemSet : "config.sys.set",
ConfigGlobalInfo : "config.global.info",
ConfigGlobalSet : "config.global.set"
ConfigGlobalSet : "config.global.set",

//
RuntimeInfo : "runtime.info",
}


Expand Down
Loading

0 comments on commit ccfc876

Please sign in to comment.