github.com/inazumav/sing-box@v0.0.0-20230926072359-ab51429a14f1/log/default.go (about) 1 package log 2 3 import ( 4 "context" 5 "io" 6 "os" 7 "time" 8 9 C "github.com/inazumav/sing-box/constant" 10 F "github.com/sagernet/sing/common/format" 11 ) 12 13 var _ Factory = (*simpleFactory)(nil) 14 15 type simpleFactory struct { 16 formatter Formatter 17 platformFormatter Formatter 18 writer io.Writer 19 platformWriter io.Writer 20 level Level 21 } 22 23 func NewFactory(formatter Formatter, writer io.Writer, platformWriter io.Writer) Factory { 24 return &simpleFactory{ 25 formatter: formatter, 26 platformFormatter: Formatter{ 27 BaseTime: formatter.BaseTime, 28 DisableColors: C.IsDarwin || C.IsIos, 29 DisableLineBreak: true, 30 }, 31 writer: writer, 32 platformWriter: platformWriter, 33 level: LevelTrace, 34 } 35 } 36 37 func (f *simpleFactory) Level() Level { 38 return f.level 39 } 40 41 func (f *simpleFactory) SetLevel(level Level) { 42 f.level = level 43 } 44 45 func (f *simpleFactory) Logger() ContextLogger { 46 return f.NewLogger("") 47 } 48 49 func (f *simpleFactory) NewLogger(tag string) ContextLogger { 50 return &simpleLogger{f, tag} 51 } 52 53 func (f *simpleFactory) Close() error { 54 return nil 55 } 56 57 var _ ContextLogger = (*simpleLogger)(nil) 58 59 type simpleLogger struct { 60 *simpleFactory 61 tag string 62 } 63 64 func (l *simpleLogger) Log(ctx context.Context, level Level, args []any) { 65 level = OverrideLevelFromContext(level, ctx) 66 if level > l.level { 67 return 68 } 69 nowTime := time.Now() 70 message := l.formatter.Format(ctx, level, l.tag, F.ToString(args...), nowTime) 71 if level == LevelPanic { 72 panic(message) 73 } 74 l.writer.Write([]byte(message)) 75 if level == LevelFatal { 76 os.Exit(1) 77 } 78 if l.platformWriter != nil { 79 l.platformWriter.Write([]byte(l.platformFormatter.Format(ctx, level, l.tag, F.ToString(args...), nowTime))) 80 } 81 } 82 83 func (l *simpleLogger) Trace(args ...any) { 84 l.TraceContext(context.Background(), args...) 85 } 86 87 func (l *simpleLogger) Debug(args ...any) { 88 l.DebugContext(context.Background(), args...) 89 } 90 91 func (l *simpleLogger) Info(args ...any) { 92 l.InfoContext(context.Background(), args...) 93 } 94 95 func (l *simpleLogger) Warn(args ...any) { 96 l.WarnContext(context.Background(), args...) 97 } 98 99 func (l *simpleLogger) Error(args ...any) { 100 l.ErrorContext(context.Background(), args...) 101 } 102 103 func (l *simpleLogger) Fatal(args ...any) { 104 l.FatalContext(context.Background(), args...) 105 } 106 107 func (l *simpleLogger) Panic(args ...any) { 108 l.PanicContext(context.Background(), args...) 109 } 110 111 func (l *simpleLogger) TraceContext(ctx context.Context, args ...any) { 112 l.Log(ctx, LevelTrace, args) 113 } 114 115 func (l *simpleLogger) DebugContext(ctx context.Context, args ...any) { 116 l.Log(ctx, LevelDebug, args) 117 } 118 119 func (l *simpleLogger) InfoContext(ctx context.Context, args ...any) { 120 l.Log(ctx, LevelInfo, args) 121 } 122 123 func (l *simpleLogger) WarnContext(ctx context.Context, args ...any) { 124 l.Log(ctx, LevelWarn, args) 125 } 126 127 func (l *simpleLogger) ErrorContext(ctx context.Context, args ...any) { 128 l.Log(ctx, LevelError, args) 129 } 130 131 func (l *simpleLogger) FatalContext(ctx context.Context, args ...any) { 132 l.Log(ctx, LevelFatal, args) 133 } 134 135 func (l *simpleLogger) PanicContext(ctx context.Context, args ...any) { 136 l.Log(ctx, LevelPanic, args) 137 }