1
0

default.go 2.9 KB

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