github.com/sagernet/sing-box@v1.2.7/log/default.go (about)

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