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

3
box.go
View file

@ -41,6 +41,7 @@ type Options struct {
option.Options option.Options
Context context.Context Context context.Context
PlatformInterface platform.Interface PlatformInterface platform.Interface
PlatformLogWriter log.PlatformWriter
} }
func New(options Options) (*Box, error) { func New(options Options) (*Box, error) {
@ -71,7 +72,7 @@ func New(options Options) (*Box, error) {
Observable: needClashAPI, Observable: needClashAPI,
DefaultWriter: defaultLogWriter, DefaultWriter: defaultLogWriter,
BaseTime: createdAt, BaseTime: createdAt,
PlatformWriter: options.PlatformInterface, PlatformWriter: options.PlatformLogWriter,
}) })
if err != nil { if err != nil {
return nil, E.Cause(err, "create log factory") return nil, E.Cause(err, "create log factory")

View file

@ -2,7 +2,6 @@ package platform
import ( import (
"context" "context"
"io"
"net/netip" "net/netip"
"github.com/sagernet/sing-box/adapter" "github.com/sagernet/sing-box/adapter"
@ -25,7 +24,6 @@ type Interface interface {
UnderNetworkExtension() bool UnderNetworkExtension() bool
ClearDNSCache() ClearDNSCache()
process.Searcher process.Searcher
io.Writer
} }
type NetworkInterface struct { type NetworkInterface struct {

View file

@ -3,6 +3,7 @@ package libbox
import ( import (
"context" "context"
"net/netip" "net/netip"
"runtime"
runtimeDebug "runtime/debug" runtimeDebug "runtime/debug"
"syscall" "syscall"
@ -12,6 +13,7 @@ import (
"github.com/sagernet/sing-box/common/urltest" "github.com/sagernet/sing-box/common/urltest"
"github.com/sagernet/sing-box/experimental/libbox/internal/procfs" "github.com/sagernet/sing-box/experimental/libbox/internal/procfs"
"github.com/sagernet/sing-box/experimental/libbox/platform" "github.com/sagernet/sing-box/experimental/libbox/platform"
"github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/option" "github.com/sagernet/sing-box/option"
"github.com/sagernet/sing-tun" "github.com/sagernet/sing-tun"
"github.com/sagernet/sing/common" "github.com/sagernet/sing/common"
@ -44,10 +46,12 @@ func NewService(configContent string, platformInterface PlatformInterface) (*Box
ctx = service.ContextWithPtr(ctx, urlTestHistoryStorage) ctx = service.ContextWithPtr(ctx, urlTestHistoryStorage)
pauseManager := pause.NewDefaultManager(ctx) pauseManager := pause.NewDefaultManager(ctx)
ctx = pause.ContextWithManager(ctx, pauseManager) ctx = pause.ContextWithManager(ctx, pauseManager)
platformWrapper := &platformInterfaceWrapper{iif: platformInterface, useProcFS: platformInterface.UseProcFS()}
instance, err := box.New(box.Options{ instance, err := box.New(box.Options{
Context: ctx, Context: ctx,
Options: options, Options: options,
PlatformInterface: &platformInterfaceWrapper{iif: platformInterface, useProcFS: platformInterface.UseProcFS()}, PlatformInterface: platformWrapper,
PlatformLogWriter: platformWrapper,
}) })
if err != nil { if err != nil {
cancel() cancel()
@ -83,7 +87,10 @@ func (s *BoxService) Wake() {
_ = s.instance.Router().ResetNetwork() _ = s.instance.Router().ResetNetwork()
} }
var _ platform.Interface = (*platformInterfaceWrapper)(nil) var (
_ platform.Interface = (*platformInterfaceWrapper)(nil)
_ log.PlatformWriter = (*platformInterfaceWrapper)(nil)
)
type platformInterfaceWrapper struct { type platformInterfaceWrapper struct {
iif PlatformInterface iif PlatformInterface
@ -131,11 +138,6 @@ func (w *platformInterfaceWrapper) OpenTun(options *tun.Options, platformOptions
return tun.New(*options) return tun.New(*options)
} }
func (w *platformInterfaceWrapper) Write(p []byte) (n int, err error) {
w.iif.WriteLog(string(p))
return len(p), nil
}
func (w *platformInterfaceWrapper) FindProcessInfo(ctx context.Context, network string, source netip.AddrPort, destination netip.AddrPort) (*process.Info, error) { func (w *platformInterfaceWrapper) FindProcessInfo(ctx context.Context, network string, source netip.AddrPort, destination netip.AddrPort) (*process.Info, error) {
var uid int32 var uid int32
if w.useProcFS { if w.useProcFS {
@ -203,3 +205,11 @@ func (w *platformInterfaceWrapper) UnderNetworkExtension() bool {
func (w *platformInterfaceWrapper) ClearDNSCache() { func (w *platformInterfaceWrapper) ClearDNSCache() {
w.iif.ClearDNSCache() w.iif.ClearDNSCache()
} }
func (w *platformInterfaceWrapper) DisableColors() bool {
return runtime.GOOS != "android"
}
func (w *platformInterfaceWrapper) WriteMessage(level log.Level, message string) {
w.iif.WriteLog(message)
}

View file

@ -16,11 +16,11 @@ type simpleFactory struct {
formatter Formatter formatter Formatter
platformFormatter Formatter platformFormatter Formatter
writer io.Writer writer io.Writer
platformWriter io.Writer platformWriter PlatformWriter
level Level 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{ return &simpleFactory{
formatter: formatter, formatter: formatter,
platformFormatter: Formatter{ platformFormatter: Formatter{
@ -76,7 +76,7 @@ func (l *simpleLogger) Log(ctx context.Context, level Level, args []any) {
os.Exit(1) os.Exit(1)
} }
if l.platformWriter != nil { 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 Observable bool
DefaultWriter io.Writer DefaultWriter io.Writer
BaseTime time.Time BaseTime time.Time
PlatformWriter io.Writer PlatformWriter PlatformWriter
} }
func New(options Options) (Factory, error) { func New(options Options) (Factory, error) {

View file

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