优化异常捕获日志

This commit is contained in:
boyce
2019-03-25 18:12:29 +08:00
parent 738bf3d5b8
commit a74c45c248
6 changed files with 21 additions and 31 deletions

View File

@@ -180,16 +180,10 @@ func (slf *BaseMessageReciver) startReadMsg(pclient *WSClient) {
defer func() {
if r := recover(); r != nil {
var coreInfo string
str, ok := r.(string)
if ok {
coreInfo = string(debug.Stack())
} else {
coreInfo = "Panic!"
}
coreInfo += "\n" + fmt.Sprintf("core information is %s\n", str)
coreInfo = string(debug.Stack())
coreInfo += "\n" + fmt.Sprintf("Core information is %v\n", r)
service.GetLogger().Printf(service.LEVER_FATAL, coreInfo)
slf.messageReciver.OnDisconnect(pclient.clientid, errors.New("core dump"))
slf.messageReciver.OnDisconnect(pclient.clientid, errors.New("Core dump"))
slf.WsServer.ReleaseClient(pclient)
}
}()

View File

@@ -4,6 +4,8 @@ import (
"fmt"
"log"
"github.com/duanhf2012/origin/util"
"net/http"
_ "net/http/pprof"
"os"
@@ -37,6 +39,7 @@ func (s *COriginNode) Init() {
os.Exit(-1)
}
util.Log = logger.Printf
s.sigs = make(chan os.Signal, 1)
signal.Notify(s.sigs, syscall.SIGINT, syscall.SIGTERM)
}

View File

@@ -130,6 +130,7 @@ import (
"bufio"
"encoding/gob"
"errors"
"fmt"
"io"
"log"
"net"
@@ -396,14 +397,10 @@ func (s *service) call(server *Server, sending *sync.Mutex, wg *sync.WaitGroup,
defer func() {
if r := recover(); r != nil {
var coreInfo string
str, ok := r.(string)
if ok {
coreInfo += str + "\n" + string(runtimedebug.Stack())
} else {
coreInfo = "Panic!"
}
coreInfo = string(runtimedebug.Stack())
coreInfo += "\nCore Request RPC Name:" + req.ServiceMethod
coreInfo += "\n" + fmt.Sprintf("Core information is %v\n", r)
orginservice.GetLogger().Printf(orginservice.LEVER_FATAL, coreInfo)
}
}()

View File

@@ -218,12 +218,14 @@ func (slf *BaseModule) AddModule(module IModule) uint32 {
slf.mapModule[module.GetModuleId()] = module
//运行模块
GetLogger().Printf(LEVER_INFO, "Start Init module %T.", module)
err := module.OnInit()
if err != nil {
GetLogger().Printf(LEVER_ERROR, "Init module %T id is %d is fail,reason:%v...", module, module.GetModuleId(), err)
GetLogger().Printf(LEVER_ERROR, "End Init module %T id is %d is fail,reason:%v...", module, module.GetModuleId(), err)
os.Exit(-1)
}
module.getBaseModule().OnInit()
GetLogger().Printf(LEVER_INFO, "End Init module %T.", module)
go module.RunModule(module)
return module.GetModuleId()
@@ -286,15 +288,12 @@ func (slf *BaseModule) RunModule(module IModule) {
defer func() {
if r := recover(); r != nil {
var coreInfo string
str, ok := r.(string)
if ok {
coreInfo = string(debug.Stack())
} else {
coreInfo = "Panic!"
}
coreInfo += "\n" + fmt.Sprintf("Core module is %T, try count %d. core information is %s\n", module, slf.recoverCount, str)
coreInfo = string(debug.Stack())
coreInfo += "\n" + fmt.Sprintf("Core module is %T, try count %d. core information is %v\n", module, slf.recoverCount, r)
GetLogger().Printf(LEVER_FATAL, coreInfo)
slf.recoverCount += 1
//重试3次
if slf.recoverCount < 4 {
go slf.RunModule(slf.GetSelf())

View File

@@ -60,12 +60,15 @@ func (slf *CServiceManager) Init(logger ILogger, exit chan bool, pwaitGroup *syn
func (slf *CServiceManager) Start() bool {
for _, sname := range slf.orderLocalService {
s := slf.FindService(sname)
GetLogger().Printf(LEVER_INFO, "Start Init module %T.", s.(IModule))
err := s.(IModule).OnInit()
if err != nil {
GetLogger().Printf(LEVER_ERROR, "Init module %T id is %d is fail,reason:%v...", s.(IModule), s.(IModule).GetModuleId(), err)
os.Exit(-1)
}
s.(IModule).getBaseModule().OnInit()
GetLogger().Printf(LEVER_INFO, "End Init module %T.", s.(IModule))
}
for _, sname := range slf.orderLocalService {

View File

@@ -10,14 +10,8 @@ func F(callback interface{}, args ...interface{}) {
defer func() {
if r := recover(); r != nil {
var coreInfo string
str, ok := r.(string)
if ok {
coreInfo = string(debug.Stack())
} else {
coreInfo = "Panic!"
}
coreInfo += "\n" + fmt.Sprintf("core information is %s\n", str)
coreInfo = string(debug.Stack())
coreInfo += "\n" + fmt.Sprintf("Core information is %v\n", r)
if Log != nil {
Log(5, coreInfo)
} else {