2022-07-12 07:17:29 +00:00
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
2022-07-30 06:50:33 +00:00
|
|
|
"github.com/sagernet/sing/common"
|
2022-07-12 07:17:29 +00:00
|
|
|
F "github.com/sagernet/sing/common/format"
|
|
|
|
"github.com/sagernet/sing/common/observable"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ Factory = (*observableFactory)(nil)
|
|
|
|
|
|
|
|
type observableFactory struct {
|
2022-10-25 04:55:00 +00:00
|
|
|
formatter Formatter
|
|
|
|
platformFormatter Formatter
|
|
|
|
writer io.Writer
|
|
|
|
platformWriter io.Writer
|
|
|
|
level Level
|
|
|
|
subscriber *observable.Subscriber[Entry]
|
|
|
|
observer *observable.Observer[Entry]
|
2022-07-12 07:17:29 +00:00
|
|
|
}
|
|
|
|
|
2022-10-25 04:55:00 +00:00
|
|
|
func NewObservableFactory(formatter Formatter, writer io.Writer, platformWriter io.Writer) ObservableFactory {
|
2022-07-12 07:17:29 +00:00
|
|
|
factory := &observableFactory{
|
2022-10-25 04:55:00 +00:00
|
|
|
formatter: formatter,
|
|
|
|
platformFormatter: Formatter{
|
|
|
|
BaseTime: formatter.BaseTime,
|
|
|
|
},
|
|
|
|
writer: writer,
|
|
|
|
platformWriter: platformWriter,
|
|
|
|
level: LevelTrace,
|
|
|
|
subscriber: observable.NewSubscriber[Entry](128),
|
2022-07-12 07:17:29 +00:00
|
|
|
}
|
|
|
|
factory.observer = observable.NewObserver[Entry](factory.subscriber, 64)
|
|
|
|
return factory
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *observableFactory) Level() Level {
|
|
|
|
return f.level
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *observableFactory) SetLevel(level Level) {
|
|
|
|
f.level = level
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *observableFactory) Logger() ContextLogger {
|
|
|
|
return f.NewLogger("")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *observableFactory) NewLogger(tag string) ContextLogger {
|
|
|
|
return &observableLogger{f, tag}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *observableFactory) Subscribe() (subscription observable.Subscription[Entry], done <-chan struct{}, err error) {
|
|
|
|
return f.observer.Subscribe()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *observableFactory) UnSubscribe(sub observable.Subscription[Entry]) {
|
|
|
|
f.observer.UnSubscribe(sub)
|
|
|
|
}
|
|
|
|
|
2022-07-30 06:50:33 +00:00
|
|
|
func (f *observableFactory) Close() error {
|
|
|
|
return common.Close(
|
|
|
|
f.observer,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-07-12 07:17:29 +00:00
|
|
|
var _ ContextLogger = (*observableLogger)(nil)
|
|
|
|
|
|
|
|
type observableLogger struct {
|
|
|
|
*observableFactory
|
|
|
|
tag string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *observableLogger) Log(ctx context.Context, level Level, args []any) {
|
2022-08-03 10:55:39 +00:00
|
|
|
level = OverrideLevelFromContext(level, ctx)
|
2022-07-12 07:17:29 +00:00
|
|
|
if level > l.level {
|
|
|
|
return
|
|
|
|
}
|
2022-10-25 04:55:00 +00:00
|
|
|
nowTime := time.Now()
|
|
|
|
message, messageSimple := l.formatter.FormatWithSimple(ctx, level, l.tag, F.ToString(args...), nowTime)
|
2022-07-12 07:17:29 +00:00
|
|
|
if level == LevelPanic {
|
|
|
|
panic(message)
|
|
|
|
}
|
|
|
|
l.writer.Write([]byte(message))
|
|
|
|
if level == LevelFatal {
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2022-07-19 14:16:49 +00:00
|
|
|
l.subscriber.Emit(Entry{level, messageSimple})
|
2022-10-25 04:55:00 +00:00
|
|
|
if l.platformWriter != nil {
|
|
|
|
l.platformWriter.Write([]byte(l.formatter.Format(ctx, level, l.tag, F.ToString(args...), nowTime)))
|
|
|
|
}
|
2022-07-12 07:17:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *observableLogger) Trace(args ...any) {
|
2022-07-13 11:01:20 +00:00
|
|
|
l.TraceContext(context.Background(), args...)
|
2022-07-12 07:17:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *observableLogger) Debug(args ...any) {
|
2022-07-13 11:01:20 +00:00
|
|
|
l.DebugContext(context.Background(), args...)
|
2022-07-12 07:17:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *observableLogger) Info(args ...any) {
|
2022-07-13 11:01:20 +00:00
|
|
|
l.InfoContext(context.Background(), args...)
|
2022-07-12 07:17:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *observableLogger) Warn(args ...any) {
|
2022-07-13 11:01:20 +00:00
|
|
|
l.WarnContext(context.Background(), args...)
|
2022-07-12 07:17:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *observableLogger) Error(args ...any) {
|
2022-07-13 11:01:20 +00:00
|
|
|
l.ErrorContext(context.Background(), args...)
|
2022-07-12 07:17:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *observableLogger) Fatal(args ...any) {
|
2022-07-13 11:01:20 +00:00
|
|
|
l.FatalContext(context.Background(), args...)
|
2022-07-12 07:17:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *observableLogger) Panic(args ...any) {
|
2022-07-13 11:01:20 +00:00
|
|
|
l.PanicContext(context.Background(), args...)
|
2022-07-12 07:17:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *observableLogger) TraceContext(ctx context.Context, args ...any) {
|
|
|
|
l.Log(ctx, LevelTrace, args)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *observableLogger) DebugContext(ctx context.Context, args ...any) {
|
|
|
|
l.Log(ctx, LevelDebug, args)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *observableLogger) InfoContext(ctx context.Context, args ...any) {
|
|
|
|
l.Log(ctx, LevelInfo, args)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *observableLogger) WarnContext(ctx context.Context, args ...any) {
|
|
|
|
l.Log(ctx, LevelWarn, args)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *observableLogger) ErrorContext(ctx context.Context, args ...any) {
|
|
|
|
l.Log(ctx, LevelError, args)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *observableLogger) FatalContext(ctx context.Context, args ...any) {
|
|
|
|
l.Log(ctx, LevelFatal, args)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *observableLogger) PanicContext(ctx context.Context, args ...any) {
|
|
|
|
l.Log(ctx, LevelPanic, args)
|
|
|
|
}
|