2022-07-12 15:17:29 +08:00
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
2023-02-22 20:52:57 +08:00
|
|
|
C "github.com/sagernet/sing-box/constant"
|
2022-07-12 15:17:29 +08:00
|
|
|
F "github.com/sagernet/sing/common/format"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ Factory = (*simpleFactory)(nil)
|
|
|
|
|
|
|
|
type simpleFactory struct {
|
2022-10-25 12:55:00 +08:00
|
|
|
formatter Formatter
|
|
|
|
platformFormatter Formatter
|
|
|
|
writer io.Writer
|
|
|
|
platformWriter io.Writer
|
|
|
|
level Level
|
2022-07-12 15:17:29 +08:00
|
|
|
}
|
|
|
|
|
2022-10-25 12:55:00 +08:00
|
|
|
func NewFactory(formatter Formatter, writer io.Writer, platformWriter io.Writer) Factory {
|
2022-07-12 15:17:29 +08:00
|
|
|
return &simpleFactory{
|
|
|
|
formatter: formatter,
|
2022-10-25 12:55:00 +08:00
|
|
|
platformFormatter: Formatter{
|
2023-02-22 20:52:57 +08:00
|
|
|
BaseTime: formatter.BaseTime,
|
2023-02-26 19:16:28 +08:00
|
|
|
DisableColors: C.IsDarwin || C.IsIos,
|
2022-10-25 12:55:00 +08:00
|
|
|
},
|
|
|
|
writer: writer,
|
|
|
|
platformWriter: platformWriter,
|
|
|
|
level: LevelTrace,
|
2022-07-12 15:17:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *simpleFactory) Level() Level {
|
|
|
|
return f.level
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *simpleFactory) SetLevel(level Level) {
|
|
|
|
f.level = level
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *simpleFactory) Logger() ContextLogger {
|
|
|
|
return f.NewLogger("")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *simpleFactory) NewLogger(tag string) ContextLogger {
|
|
|
|
return &simpleLogger{f, tag}
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ ContextLogger = (*simpleLogger)(nil)
|
|
|
|
|
|
|
|
type simpleLogger struct {
|
|
|
|
*simpleFactory
|
|
|
|
tag string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *simpleLogger) Log(ctx context.Context, level Level, args []any) {
|
2022-08-03 18:55:39 +08:00
|
|
|
level = OverrideLevelFromContext(level, ctx)
|
2022-07-12 15:17:29 +08:00
|
|
|
if level > l.level {
|
|
|
|
return
|
|
|
|
}
|
2022-10-25 12:55:00 +08:00
|
|
|
nowTime := time.Now()
|
|
|
|
message := l.formatter.Format(ctx, level, l.tag, F.ToString(args...), nowTime)
|
2022-07-12 15:17:29 +08:00
|
|
|
if level == LevelPanic {
|
|
|
|
panic(message)
|
|
|
|
}
|
|
|
|
l.writer.Write([]byte(message))
|
|
|
|
if level == LevelFatal {
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2022-10-25 12:55:00 +08:00
|
|
|
if l.platformWriter != nil {
|
|
|
|
l.platformWriter.Write([]byte(l.platformFormatter.Format(ctx, level, l.tag, F.ToString(args...), nowTime)))
|
|
|
|
}
|
2022-07-12 15:17:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *simpleLogger) Trace(args ...any) {
|
2022-07-13 19:01:20 +08:00
|
|
|
l.TraceContext(context.Background(), args...)
|
2022-07-12 15:17:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *simpleLogger) Debug(args ...any) {
|
2022-07-13 19:01:20 +08:00
|
|
|
l.DebugContext(context.Background(), args...)
|
2022-07-12 15:17:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *simpleLogger) Info(args ...any) {
|
2022-07-13 19:01:20 +08:00
|
|
|
l.InfoContext(context.Background(), args...)
|
2022-07-12 15:17:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *simpleLogger) Warn(args ...any) {
|
2022-07-13 19:01:20 +08:00
|
|
|
l.WarnContext(context.Background(), args...)
|
2022-07-12 15:17:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *simpleLogger) Error(args ...any) {
|
2022-07-13 19:01:20 +08:00
|
|
|
l.ErrorContext(context.Background(), args...)
|
2022-07-12 15:17:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *simpleLogger) Fatal(args ...any) {
|
2022-07-13 19:01:20 +08:00
|
|
|
l.FatalContext(context.Background(), args...)
|
2022-07-12 15:17:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *simpleLogger) Panic(args ...any) {
|
2022-07-13 19:01:20 +08:00
|
|
|
l.PanicContext(context.Background(), args...)
|
2022-07-12 15:17:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *simpleLogger) TraceContext(ctx context.Context, args ...any) {
|
|
|
|
l.Log(ctx, LevelTrace, args)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *simpleLogger) DebugContext(ctx context.Context, args ...any) {
|
|
|
|
l.Log(ctx, LevelDebug, args)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *simpleLogger) InfoContext(ctx context.Context, args ...any) {
|
|
|
|
l.Log(ctx, LevelInfo, args)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *simpleLogger) WarnContext(ctx context.Context, args ...any) {
|
|
|
|
l.Log(ctx, LevelWarn, args)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *simpleLogger) ErrorContext(ctx context.Context, args ...any) {
|
|
|
|
l.Log(ctx, LevelError, args)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *simpleLogger) FatalContext(ctx context.Context, args ...any) {
|
|
|
|
l.Log(ctx, LevelFatal, args)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *simpleLogger) PanicContext(ctx context.Context, args ...any) {
|
|
|
|
l.Log(ctx, LevelPanic, args)
|
|
|
|
}
|