服务器事件日志优化

This commit is contained in:
kercylan98 2023-04-20 16:40:07 +08:00
parent ba0b9f7508
commit 644c520454
2 changed files with 22 additions and 0 deletions

View File

@ -3,6 +3,8 @@ package server
import (
"go.uber.org/zap"
"minotaur/utils/log"
"minotaur/utils/runtimes"
"reflect"
)
type ConnectionReceivePacketEventHandle func(conn *Conn, packet []byte)
@ -22,6 +24,7 @@ func (slf *event) RegConnectionClosedEvent(handle ConnectionClosedEventHandle) {
panic(ErrNetworkIncompatibleHttp)
}
slf.connectionClosedEventHandles = append(slf.connectionClosedEventHandles, handle)
log.Info("Server", zap.String("RegEvent", runtimes.CurrentRunningFuncName()), zap.String("handle", reflect.TypeOf(handle).String()))
}
func (slf *event) OnConnectionClosedEvent(conn *Conn) {
@ -36,6 +39,7 @@ func (slf *event) RegConnectionOpenedEvent(handle ConnectionOpenedEventHandle) {
panic(ErrNetworkIncompatibleHttp)
}
slf.connectionOpenedEventHandles = append(slf.connectionOpenedEventHandles, handle)
log.Info("Server", zap.String("RegEvent", runtimes.CurrentRunningFuncName()), zap.String("handle", reflect.TypeOf(handle).String()))
}
func (slf *event) OnConnectionOpenedEvent(conn *Conn) {
@ -50,6 +54,7 @@ func (slf *event) RegConnectionReceivePacketEvent(handle ConnectionReceivePacket
panic(ErrNetworkIncompatibleHttp)
}
slf.connectionReceivePacketEventHandles = append(slf.connectionReceivePacketEventHandles, handle)
log.Info("Server", zap.String("RegEvent", runtimes.CurrentRunningFuncName()), zap.String("handle", reflect.TypeOf(handle).String()))
}
func (slf *event) OnConnectionReceivePacketEvent(conn *Conn, packet []byte) {

17
utils/runtimes/runtime.go Normal file
View File

@ -0,0 +1,17 @@
package runtimes
import (
"runtime"
)
// CurrentRunningFuncName 获取正在运行的函数名
func CurrentRunningFuncName(skip ...int) string {
pc := make([]uintptr, 1)
var s = 2
if len(skip) > 0 {
s += skip[0]
}
runtime.Callers(s, pc)
f := runtime.FuncForPC(pc[0])
return f.Name()
}