mirror of
https://github.com/shtorm-7/sing-box-extended.git
synced 2026-05-14 00:51:12 +03:00
Avoid opening log output before start &
Replace tracing logs with task monitor
This commit is contained in:
66
log/log.go
66
log/log.go
@@ -7,35 +7,9 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/sagernet/sing-box/option"
|
||||
"github.com/sagernet/sing/common"
|
||||
E "github.com/sagernet/sing/common/exceptions"
|
||||
"github.com/sagernet/sing/service/filemanager"
|
||||
)
|
||||
|
||||
type factoryWithFile struct {
|
||||
Factory
|
||||
file *os.File
|
||||
}
|
||||
|
||||
func (f *factoryWithFile) Close() error {
|
||||
return common.Close(
|
||||
f.Factory,
|
||||
common.PtrOrNil(f.file),
|
||||
)
|
||||
}
|
||||
|
||||
type observableFactoryWithFile struct {
|
||||
ObservableFactory
|
||||
file *os.File
|
||||
}
|
||||
|
||||
func (f *observableFactoryWithFile) Close() error {
|
||||
return common.Close(
|
||||
f.ObservableFactory,
|
||||
common.PtrOrNil(f.file),
|
||||
)
|
||||
}
|
||||
|
||||
type Options struct {
|
||||
Context context.Context
|
||||
Options option.LogOptions
|
||||
@@ -52,8 +26,8 @@ func New(options Options) (Factory, error) {
|
||||
return NewNOPFactory(), nil
|
||||
}
|
||||
|
||||
var logFile *os.File
|
||||
var logWriter io.Writer
|
||||
var logFilePath string
|
||||
|
||||
switch logOptions.Output {
|
||||
case "":
|
||||
@@ -66,26 +40,23 @@ func New(options Options) (Factory, error) {
|
||||
case "stdout":
|
||||
logWriter = os.Stdout
|
||||
default:
|
||||
var err error
|
||||
logFile, err = filemanager.OpenFile(options.Context, logOptions.Output, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
logWriter = logFile
|
||||
logFilePath = logOptions.Output
|
||||
}
|
||||
logFormatter := Formatter{
|
||||
BaseTime: options.BaseTime,
|
||||
DisableColors: logOptions.DisableColor || logFile != nil,
|
||||
DisableTimestamp: !logOptions.Timestamp && logFile != nil,
|
||||
DisableColors: logOptions.DisableColor || logFilePath != "",
|
||||
DisableTimestamp: !logOptions.Timestamp && logFilePath != "",
|
||||
FullTimestamp: logOptions.Timestamp,
|
||||
TimestampFormat: "-0700 2006-01-02 15:04:05",
|
||||
}
|
||||
var factory Factory
|
||||
if options.Observable {
|
||||
factory = NewObservableFactory(logFormatter, logWriter, options.PlatformWriter)
|
||||
} else {
|
||||
factory = NewFactory(logFormatter, logWriter, options.PlatformWriter)
|
||||
}
|
||||
factory := NewDefaultFactory(
|
||||
options.Context,
|
||||
logFormatter,
|
||||
logWriter,
|
||||
logFilePath,
|
||||
options.PlatformWriter,
|
||||
options.Observable,
|
||||
)
|
||||
if logOptions.Level != "" {
|
||||
logLevel, err := ParseLevel(logOptions.Level)
|
||||
if err != nil {
|
||||
@@ -95,18 +66,5 @@ func New(options Options) (Factory, error) {
|
||||
} else {
|
||||
factory.SetLevel(LevelTrace)
|
||||
}
|
||||
if logFile != nil {
|
||||
if options.Observable {
|
||||
factory = &observableFactoryWithFile{
|
||||
ObservableFactory: factory.(ObservableFactory),
|
||||
file: logFile,
|
||||
}
|
||||
} else {
|
||||
factory = &factoryWithFile{
|
||||
Factory: factory,
|
||||
file: logFile,
|
||||
}
|
||||
}
|
||||
}
|
||||
return factory, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user