mirror of
https://github.com/Monibuca/engine.git
synced 2025-10-05 16:46:58 +08:00
feat: make diffrent fatal.log
This commit is contained in:
18
io.go
18
io.go
@@ -167,17 +167,12 @@ func (io *IO) receive(streamPath string, specific IIO) error {
|
||||
if v, ok := specific.(ISubscriber); ok {
|
||||
wt = v.GetSubscriber().Config.WaitTimeout
|
||||
}
|
||||
io.Context, io.CancelFunc = context.WithCancel(util.Conditoinal[context.Context](io.Context == nil, Engine, io.Context))
|
||||
s, create := findOrCreateStream(u.Path, wt)
|
||||
if s == nil {
|
||||
return ErrBadStreamName
|
||||
}
|
||||
io.Stream = s
|
||||
io.Spesific = specific
|
||||
io.StartTime = time.Now()
|
||||
if io.Type == "" {
|
||||
io.Type = reflect.TypeOf(specific).Elem().Name()
|
||||
}
|
||||
if io.Stream == nil { //初次
|
||||
io.Context, io.CancelFunc = context.WithCancel(util.Conditoinal[context.Context](io.Context == nil, Engine, io.Context))
|
||||
logFeilds := []zapcore.Field{zap.String("type", io.Type)}
|
||||
if io.ID != "" {
|
||||
logFeilds = append(logFeilds, zap.String("ID", io.ID))
|
||||
@@ -188,6 +183,13 @@ func (io *IO) receive(streamPath string, specific IIO) error {
|
||||
logFeilds = append(logFeilds, zap.String("streamPath", s.Path))
|
||||
io.Logger = io.Logger.With(logFeilds...)
|
||||
}
|
||||
}
|
||||
io.Stream = s
|
||||
io.Spesific = specific
|
||||
io.StartTime = time.Now()
|
||||
if io.Type == "" {
|
||||
io.Type = reflect.TypeOf(specific).Elem().Name()
|
||||
}
|
||||
if v, ok := specific.(IPublisher); ok {
|
||||
conf := v.GetPublisher().Config
|
||||
io.Type = strings.TrimSuffix(io.Type, "Publisher")
|
||||
@@ -195,7 +197,7 @@ func (io *IO) receive(streamPath string, specific IIO) error {
|
||||
s.pubLocker.Lock()
|
||||
defer s.pubLocker.Unlock()
|
||||
oldPublisher := s.Publisher
|
||||
if oldPublisher != nil && !oldPublisher.IsClosed() {
|
||||
if oldPublisher != nil {
|
||||
zot := zap.String("old type", oldPublisher.GetPublisher().Type)
|
||||
if oldPublisher == specific { // 断线重连
|
||||
s.Info("republish", zot)
|
||||
|
@@ -54,8 +54,6 @@ func (pub *Puller) startPull(puller IPuller) {
|
||||
}
|
||||
}()
|
||||
puber := puller.GetPublisher()
|
||||
originContext := puber.Context // 保存原始的Context
|
||||
logger := puber.Logger // 保存原始的Logger
|
||||
for puller.Info("start pull"); puller.Reconnect(); puller.Warn("restart pull") {
|
||||
if err = puller.Connect(); err != nil {
|
||||
if err == io.EOF {
|
||||
@@ -68,8 +66,6 @@ func (pub *Puller) startPull(puller IPuller) {
|
||||
}
|
||||
time.Sleep(time.Second * 5)
|
||||
} else {
|
||||
puber.Context = originContext // 每次重连都需要恢复原始的Context
|
||||
puber.Logger = logger // 每次重连都需要恢复原始的Logger
|
||||
if err = puller.Publish(pub.StreamPath, puller); err != nil {
|
||||
puller.Error("pull publish", zap.Error(err))
|
||||
return
|
||||
|
@@ -2,12 +2,14 @@ package util
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
var Null = struct{}{}
|
||||
@@ -16,6 +18,21 @@ func Clone[T any](x T) *T {
|
||||
return &x
|
||||
}
|
||||
|
||||
func initFatalLog() *os.File {
|
||||
fatal_log := "./fatal"
|
||||
if _fatal_log := os.Getenv("M7S_FATAL_LOG"); _fatal_log != "" {
|
||||
fatal_log = _fatal_log
|
||||
}
|
||||
os.MkdirAll(fatal_log, 0666)
|
||||
fatal_log = filepath.Join(fatal_log, time.Now().Format("2006-01-02 15:04:05")+".log")
|
||||
logFile, err := os.OpenFile(fatal_log, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666)
|
||||
if err != nil {
|
||||
log.Println("服务启动出错", "打开异常日志文件失败", err)
|
||||
return nil
|
||||
}
|
||||
return logFile
|
||||
}
|
||||
|
||||
func CurrentDir(path ...string) string {
|
||||
if _, currentFilePath, _, _ := runtime.Caller(1); len(path) == 0 {
|
||||
return filepath.Dir(currentFilePath)
|
||||
|
@@ -3,24 +3,14 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
func init() {
|
||||
fatal_log := "./fatal.log"
|
||||
if _fatal_log := os.Getenv("M7S_FATAL_LOG"); _fatal_log != "" {
|
||||
fatal_log = _fatal_log
|
||||
}
|
||||
logFile, err := os.OpenFile(fatal_log, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666)
|
||||
if err != nil {
|
||||
log.Println("服务启动出错", "打开异常日志文件失败", err)
|
||||
return
|
||||
}
|
||||
logFile := initFatalLog()
|
||||
if logFile != nil {
|
||||
// 将进程标准出错重定向至文件,进程崩溃时运行时将向该文件记录协程调用栈信息
|
||||
syscall.Dup2(int(logFile.Fd()), int(os.Stderr.Fd()))
|
||||
|
||||
os.Stderr.WriteString("\n" + time.Now().Format("2006-01-02 15:04:05") + "--------------------------------\n")
|
||||
}
|
||||
}
|
||||
|
@@ -3,23 +3,14 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
func init() {
|
||||
fatal_log := "./fatal.log"
|
||||
if _fatal_log := os.Getenv("M7S_FATAL_LOG"); _fatal_log != "" {
|
||||
fatal_log = _fatal_log
|
||||
}
|
||||
logFile, err := os.OpenFile(fatal_log, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666)
|
||||
if err != nil {
|
||||
log.Println("服务启动出错", "打开异常日志文件失败", err)
|
||||
return
|
||||
}
|
||||
logFile := initFatalLog()
|
||||
if logFile != nil {
|
||||
// 将进程标准出错重定向至文件,进程崩溃时运行时将向该文件记录协程调用栈信息
|
||||
syscall.Dup3(int(logFile.Fd()), int(os.Stderr.Fd()), 0)
|
||||
os.Stderr.WriteString("\n" + time.Now().Format("2006-01-02 15:04:05") + "--------------------------------\n")
|
||||
}
|
||||
}
|
||||
|
@@ -6,7 +6,6 @@ import (
|
||||
"log"
|
||||
"os"
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -27,16 +26,13 @@ func setStdHandle(stdhandle int32, handle syscall.Handle) error {
|
||||
|
||||
// redirectStderr to the file passed in
|
||||
func init() {
|
||||
fatal_log := "./fatal.log"
|
||||
if _fatal_log := os.Getenv("M7S_FATAL_LOG"); _fatal_log != "" {
|
||||
fatal_log = _fatal_log
|
||||
}
|
||||
logFile, err := os.OpenFile(fatal_log, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666)
|
||||
logFile := initFatalLog()
|
||||
if logFile != nil {
|
||||
err = setStdHandle(syscall.STD_ERROR_HANDLE, syscall.Handle(logFile.Fd()))
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to redirect stderr to file: %v", err)
|
||||
}
|
||||
// SetStdHandle does not affect prior references to stderr
|
||||
os.Stderr = logFile
|
||||
os.Stderr.WriteString("\n" + time.Now().Format("2006-01-02 15:04:05") + "--------------------------------\n")
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user