添加仿真panic恢复,使用slog打印

This commit is contained in:
walker 2023-10-10 18:26:51 +08:00
parent 47f78cb54d
commit e779734672
3 changed files with 28 additions and 5 deletions

View File

@ -1,10 +1,26 @@
package debug package debug
import ( import (
"bytes"
"github.com/yohamta/donburi"
"github.com/yohamta/donburi/features/debug" "github.com/yohamta/donburi/features/debug"
"joylink.club/ecs"
) )
func PrintEntityCounts(w ecs.World) { type EntityCounts = debug.EntityCounts
debug.PrintEntityCounts(w)
func GetEntityCounts(w donburi.World) []EntityCounts {
return debug.GetEntityCounts(w)
}
// 打印所有实体数量
func SPrintEntityCounts(w donburi.World) string {
var out bytes.Buffer
out.WriteString("Entity Counts:\n")
for _, c := range GetEntityCounts(w) {
out.WriteString(c.String())
out.WriteString("\n")
}
out.WriteString("\n")
return out.String()
} }

2
go.mod
View File

@ -1,6 +1,6 @@
module joylink.club/ecs module joylink.club/ecs
go 1.20 go 1.21
require github.com/yohamta/donburi v1.3.8 require github.com/yohamta/donburi v1.3.8

View File

@ -2,6 +2,7 @@ package ecs
import ( import (
"fmt" "fmt"
"log/slog"
"math" "math"
"time" "time"
@ -173,6 +174,12 @@ func (w *world) executeTodos() {
} }
} }
func (w *world) run() { func (w *world) run() {
defer func() {
if err := recover(); err != nil {
slog.Error("世界运行异常:", "stacks", err)
w.state = Error
}
}()
for { for {
if w.state == Error { if w.state == Error {
// 世界错误,关闭世界 // 世界错误,关闭世界
@ -204,6 +211,6 @@ func (w *world) run() {
w.times += w.speed w.times += w.speed
} }
// dt := time.Since(start) // dt := time.Since(start)
// fmt.Println("仿真系统执行耗时:", dt.Milliseconds(), "ms") // slog.Info("仿真系统执行耗时:" + dt.Milliseconds() + "ms")
} }
} }