github.com/yaling888/clash@v1.53.0/log/log_gvisor.go (about)

     1  //go:build !nogvisor
     2  
     3  package log
     4  
     5  import (
     6  	"io"
     7  	"time"
     8  
     9  	logger "github.com/phuslu/log"
    10  	gLog "gvisor.dev/gvisor/pkg/log"
    11  )
    12  
    13  func init() {
    14  	initGVisorLogger(true)
    15  }
    16  
    17  func initGVisorLogger(v bool) {
    18  	if v {
    19  		if _, ok := gLog.Log().Emitter.(*gVisorLogger); ok {
    20  			return
    21  		}
    22  		gLog.SetTarget(&gVisorLogger{})
    23  	} else {
    24  		gLog.SetTarget(&gLog.Writer{Next: io.Discard})
    25  	}
    26  }
    27  
    28  type gVisorLogger struct{}
    29  
    30  func (gVisorLogger) Emit(_ int, level gLog.Level, _ time.Time, format string, args ...any) {
    31  	switch level {
    32  	case gLog.Debug:
    33  		logger.Debug().Msgf("[GVisor] "+format, args)
    34  	case gLog.Info:
    35  		logger.Info().Msgf("[GVisor] "+format, args)
    36  	case gLog.Warning:
    37  		logger.Warn().Msgf("[GVisor] "+format, args)
    38  	}
    39  }
    40  
    41  func SetLevel(newLevel LogLevel) {
    42  	level = newLevel
    43  	(logger.DefaultLogger.Writer.(*multiWriter)).consoleLevel = logger.Level(newLevel)
    44  
    45  	if !jsonSource.HasSubscriber() && !textSource.HasSubscriber() {
    46  		logger.DefaultLogger.SetLevel(logger.Level(newLevel))
    47  	}
    48  
    49  	gLevel := gLog.Warning
    50  	if newLevel == DEBUG {
    51  		gLevel = gLog.Debug
    52  	} else if newLevel == INFO {
    53  		gLevel = gLog.Info
    54  	}
    55  	gLog.SetLevel(gLevel)
    56  	initGVisorLogger(newLevel != SILENT)
    57  }