platform: Refactor log interface

This commit is contained in:
世界 2023-11-15 13:05:33 +08:00
parent 3cadc90375
commit 1825869124
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
7 changed files with 35 additions and 19 deletions

View file

@ -16,11 +16,11 @@ type simpleFactory struct {
formatter Formatter
platformFormatter Formatter
writer io.Writer
platformWriter io.Writer
platformWriter PlatformWriter
level Level
}
func NewFactory(formatter Formatter, writer io.Writer, platformWriter io.Writer) Factory {
func NewFactory(formatter Formatter, writer io.Writer, platformWriter PlatformWriter) Factory {
return &simpleFactory{
formatter: formatter,
platformFormatter: Formatter{
@ -76,7 +76,7 @@ func (l *simpleLogger) Log(ctx context.Context, level Level, args []any) {
os.Exit(1)
}
if l.platformWriter != nil {
l.platformWriter.Write([]byte(l.platformFormatter.Format(ctx, level, l.tag, F.ToString(args...), nowTime)))
l.platformWriter.WriteMessage(level, l.platformFormatter.Format(ctx, level, l.tag, F.ToString(args...), nowTime))
}
}

View file

@ -42,7 +42,7 @@ type Options struct {
Observable bool
DefaultWriter io.Writer
BaseTime time.Time
PlatformWriter io.Writer
PlatformWriter PlatformWriter
}
func New(options Options) (Factory, error) {

View file

@ -6,7 +6,6 @@ import (
"os"
"time"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing/common"
F "github.com/sagernet/sing/common/format"
"github.com/sagernet/sing/common/observable"
@ -18,18 +17,17 @@ type observableFactory struct {
formatter Formatter
platformFormatter Formatter
writer io.Writer
platformWriter io.Writer
platformWriter PlatformWriter
level Level
subscriber *observable.Subscriber[Entry]
observer *observable.Observer[Entry]
}
func NewObservableFactory(formatter Formatter, writer io.Writer, platformWriter io.Writer) ObservableFactory {
func NewObservableFactory(formatter Formatter, writer io.Writer, platformWriter PlatformWriter) ObservableFactory {
factory := &observableFactory{
formatter: formatter,
platformFormatter: Formatter{
BaseTime: formatter.BaseTime,
DisableColors: C.IsDarwin || C.IsIos,
DisableLineBreak: true,
},
writer: writer,
@ -37,6 +35,9 @@ func NewObservableFactory(formatter Formatter, writer io.Writer, platformWriter
level: LevelTrace,
subscriber: observable.NewSubscriber[Entry](128),
}
if platformWriter != nil {
factory.platformFormatter.DisableColors = platformWriter.DisableColors()
}
factory.observer = observable.NewObserver[Entry](factory.subscriber, 64)
return factory
}
@ -94,7 +95,7 @@ func (l *observableLogger) Log(ctx context.Context, level Level, args []any) {
}
l.subscriber.Emit(Entry{level, messageSimple})
if l.platformWriter != nil {
l.platformWriter.Write([]byte(l.platformFormatter.Format(ctx, level, l.tag, F.ToString(args...), nowTime)))
l.platformWriter.WriteMessage(level, l.platformFormatter.Format(ctx, level, l.tag, F.ToString(args...), nowTime))
}
}

6
log/platform.go Normal file
View file

@ -0,0 +1,6 @@
package log
type PlatformWriter interface {
DisableColors() bool
WriteMessage(level Level, message string)
}