添加仿真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
import (
"bytes"
"github.com/yohamta/donburi"
"github.com/yohamta/donburi/features/debug"
"joylink.club/ecs"
)
func PrintEntityCounts(w ecs.World) {
debug.PrintEntityCounts(w)
type EntityCounts = debug.EntityCounts
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
go 1.20
go 1.21
require github.com/yohamta/donburi v1.3.8

View File

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