mirror of
https://github.com/koho/frpmgr.git
synced 2025-10-20 07:33:47 +08:00
Remove the use of reflection to close log files
This commit is contained in:
@ -10,6 +10,7 @@ import (
|
||||
"github.com/fatedier/frp/pkg/config"
|
||||
"github.com/fatedier/frp/pkg/config/v1"
|
||||
"github.com/fatedier/frp/pkg/util/log"
|
||||
glog "github.com/fatedier/golib/log"
|
||||
|
||||
"github.com/koho/frpmgr/pkg/consts"
|
||||
)
|
||||
@ -20,6 +21,7 @@ type FrpClientService struct {
|
||||
cfg *v1.ClientCommonConfig
|
||||
done chan struct{}
|
||||
statusExporter client.StatusExporter
|
||||
logger *glog.RotateFileWriter
|
||||
}
|
||||
|
||||
func NewFrpClientService(cfgFile string) (*FrpClientService, error) {
|
||||
@ -36,13 +38,14 @@ func NewFrpClientService(cfgFile string) (*FrpClientService, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
log.InitLogger(cfg.Log.To, cfg.Log.Level, int(cfg.Log.MaxDays), cfg.Log.DisablePrintColor)
|
||||
logger := initLogger(cfg.Log.To, cfg.Log.Level, int(cfg.Log.MaxDays))
|
||||
return &FrpClientService{
|
||||
svr: svr,
|
||||
file: cfgFile,
|
||||
cfg: cfg,
|
||||
done: make(chan struct{}),
|
||||
statusExporter: svr.StatusExporter(),
|
||||
logger: logger,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@ -102,3 +105,21 @@ func (s *FrpClientService) GetProxyStatus(name string) (status *proxy.WorkingSta
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func initLogger(logPath string, levelStr string, maxDays int) *glog.RotateFileWriter {
|
||||
var options []glog.Option
|
||||
writer := glog.NewRotateFileWriter(glog.RotateFileConfig{
|
||||
FileName: logPath,
|
||||
Mode: glog.RotateFileModeDaily,
|
||||
MaxDays: maxDays,
|
||||
})
|
||||
writer.Init()
|
||||
options = append(options, glog.WithOutput(writer))
|
||||
level, err := glog.ParseLevel(levelStr)
|
||||
if err != nil {
|
||||
level = glog.InfoLevel
|
||||
}
|
||||
options = append(options, glog.WithLevel(level))
|
||||
log.Logger = log.Logger.WithOptions(options...)
|
||||
return writer
|
||||
}
|
||||
|
@ -2,25 +2,16 @@ package services
|
||||
|
||||
import (
|
||||
"os"
|
||||
"reflect"
|
||||
"unsafe"
|
||||
|
||||
frpconfig "github.com/fatedier/frp/pkg/config"
|
||||
"github.com/fatedier/frp/pkg/config/v1/validation"
|
||||
"github.com/fatedier/frp/pkg/util/log"
|
||||
glog "github.com/fatedier/golib/log"
|
||||
|
||||
"github.com/koho/frpmgr/pkg/config"
|
||||
"github.com/koho/frpmgr/pkg/util"
|
||||
)
|
||||
|
||||
func deleteFrpConfig(serviceName string, configPath string, c *config.ClientConfig) {
|
||||
func deleteFrpFiles(serviceName, configPath, logFile string) {
|
||||
// Delete logs
|
||||
logWriter := reflect.ValueOf(log.Logger).Elem().FieldByName("out")
|
||||
if writer, ok := reflect.NewAt(logWriter.Type(), unsafe.Pointer(logWriter.UnsafeAddr())).Elem().Interface().(*glog.RotateFileWriter); ok {
|
||||
writer.Close()
|
||||
}
|
||||
if logs, _, err := util.FindLogFiles(c.LogFile); err == nil {
|
||||
if logs, _, err := util.FindLogFiles(logFile); err == nil {
|
||||
util.DeleteFiles(logs)
|
||||
}
|
||||
// Delete config file
|
||||
|
@ -53,7 +53,7 @@ func (service *frpService) Execute(args []string, r <-chan svc.ChangeRequest, ch
|
||||
switch err {
|
||||
case nil:
|
||||
if t <= 0 {
|
||||
deleteFrpConfig(args[0], service.configPath, cc)
|
||||
deleteFrpFiles(args[0], service.configPath, cc.LogFile)
|
||||
return
|
||||
}
|
||||
expired = time.After(t)
|
||||
@ -102,7 +102,8 @@ func (service *frpService) Execute(args []string, r <-chan svc.ChangeRequest, ch
|
||||
return
|
||||
case <-expired:
|
||||
svr.Stop(false)
|
||||
deleteFrpConfig(args[0], service.configPath, cc)
|
||||
svr.logger.Close()
|
||||
deleteFrpFiles(args[0], service.configPath, cc.LogFile)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user