func handleRoomLoadState(p *entity.Player, req interface{}) error { pck, ok := req.(*olbattle.RoomLoadState) if !ok { return protocol.ErrorInvalidProtocol } defer func() { pck.PlayerID = p.PlayerID p.SendClient(pck) }() room := GetPlayerRoom(p) if room != nil { room.OnLoadProgress(p, pck.Progress) } else { log.Warningf("[%s:%d]match scene load done, cannot find room", p.GetString(fds.AttrPlayerNameKey), p.GetEntityID()) return protocol.ErrorNotFindRoom } return nil } 如何打印该函数的堆栈回溯
时间: 2024-03-05 10:49:07 浏览: 84
要打印该函数的堆栈回溯,可以在函数内部使用 `panic` 函数,然后在 `defer` 函数中使用 `recover` 函数来获取堆栈信息并打印出来。具体实现如下:
```go
import (
"fmt"
"runtime/debug"
)
func handleRoomLoadState(p *entity.Player, req interface{}) error {
defer func() {
if r := recover(); r != nil {
fmt.Println("panic:", r)
debug.PrintStack()
}
}()
pck, ok := req.(*olbattle.RoomLoadState)
if !ok {
return protocol.ErrorInvalidProtocol
}
defer func() {
pck.PlayerID = p.PlayerID
p.SendClient(pck)
}()
room := GetPlayerRoom(p)
if room != nil {
room.OnLoadProgress(p, pck.Progress)
} else {
log.Warningf("[%s:%d]match scene load done, cannot find room", p.GetString(fds.AttrPlayerNameKey), p.GetEntityID())
return protocol.ErrorNotFindRoom
}
return nil
}
```
在函数内部使用 `defer` 函数来捕获 panic,然后使用 `recover` 函数获取 panic 信息。如果有 panic 发生,则会打印出错误信息和堆栈回溯。
阅读全文