observable.go 3.9 KB

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