错误类型消息补充打印分发前堆栈

This commit is contained in:
kercylan98 2023-05-20 17:02:22 +08:00
parent 27fa694d38
commit abe9a7b57c
3 changed files with 27 additions and 7 deletions

View File

@ -90,8 +90,8 @@ func (slf MessageType) deconstructPacket(attrs ...any) (conn *Conn, packet []byt
return return
} }
func (slf MessageType) deconstructError(attrs ...any) (err error, action MessageErrorAction) { func (slf MessageType) deconstructError(attrs ...any) (err error, action MessageErrorAction, stack string) {
if len(attrs) != 2 { if len(attrs) != 3 {
panic(ErrMessageTypeErrorAttrs) panic(ErrMessageTypeErrorAttrs)
} }
var ok bool var ok bool
@ -101,6 +101,7 @@ func (slf MessageType) deconstructError(attrs ...any) (err error, action Message
if action, ok = attrs[1].(MessageErrorAction); !ok { if action, ok = attrs[1].(MessageErrorAction); !ok {
panic(ErrMessageTypeErrorAttrs) panic(ErrMessageTypeErrorAttrs)
} }
stack = attrs[2].(string)
return return
} }

View File

@ -16,6 +16,7 @@ import (
"net/http" "net/http"
"os" "os"
"os/signal" "os/signal"
"runtime/debug"
"strings" "strings"
"sync/atomic" "sync/atomic"
"syscall" "syscall"
@ -330,7 +331,7 @@ func (slf *Server) Ticker() *timer.Ticker {
} }
// Shutdown 停止运行服务器 // Shutdown 停止运行服务器
func (slf *Server) Shutdown(err error) { func (slf *Server) Shutdown(err error, stack ...string) {
slf.isShutdown.Store(true) slf.isShutdown.Store(true)
if slf.ticker != nil { if slf.ticker != nil {
slf.ticker.Release() slf.ticker.Release()
@ -364,7 +365,11 @@ func (slf *Server) Shutdown(err error) {
} }
if err != nil { if err != nil {
log.Error("Server", zap.Any("network", slf.network), zap.String("listen", slf.addr), var s string
if len(stack) > 0 {
s = stack[0]
}
log.ErrorWithStack("Server", s, zap.Any("network", slf.network), zap.String("listen", slf.addr),
zap.String("action", "shutdown"), zap.String("state", "exception"), zap.Error(err)) zap.String("action", "shutdown"), zap.String("state", "exception"), zap.Error(err))
slf.closeChannel <- struct{}{} slf.closeChannel <- struct{}{}
} else { } else {
@ -393,6 +398,9 @@ func (slf *Server) PushMessage(messageType MessageType, attrs ...any) {
msg := slf.messagePool.Get() msg := slf.messagePool.Get()
msg.t = messageType msg.t = messageType
msg.attrs = attrs msg.attrs = attrs
if msg.t == MessageTypeError {
msg.attrs = append(msg.attrs, string(debug.Stack()))
}
for _, channel := range slf.messageChannel { for _, channel := range slf.messageChannel {
channel <- msg channel <- msg
break break
@ -437,12 +445,12 @@ func (slf *Server) dispatchMessage(msg *message) {
slf.OnConnectionReceivePacketEvent(conn, packet) slf.OnConnectionReceivePacketEvent(conn, packet)
} }
case MessageTypeError: case MessageTypeError:
err, action := msg.t.deconstructError(msg.attrs...) err, action, stack := msg.t.deconstructError(msg.attrs...)
switch action { switch action {
case MessageErrorActionNone: case MessageErrorActionNone:
log.Error("Server", zap.Error(err)) log.ErrorWithStack("Server", stack, zap.Error(err))
case MessageErrorActionShutdown: case MessageErrorActionShutdown:
slf.Shutdown(err) slf.Shutdown(err, stack)
default: default:
log.Warn("Server", zap.String("not support message error action", action.String())) log.Warn("Server", zap.String("not support message error action", action.String()))
} }

View File

@ -103,6 +103,17 @@ func Error(msg string, fields ...zap.Field) {
fmt.Println(string(debug.Stack())) fmt.Println(string(debug.Stack()))
} }
// ErrorWithStack 通过额外的堆栈信息打印错误日志
func ErrorWithStack(msg, stack string, fields ...zap.Field) {
logger.Error(msg, fields...)
var stackMerge string
if len(stack) > 0 {
stackMerge = stack
}
stackMerge += string(debug.Stack())
fmt.Println(stackMerge)
}
func SetProd() { func SetProd() {
prod = true prod = true
} }