github.com/v2fly/v2ray-core/v4@v4.45.2/app/log/log_creator.go (about) 1 //go:build !confonly 2 // +build !confonly 3 4 package log 5 6 import ( 7 "sync" 8 9 "github.com/v2fly/v2ray-core/v4/common" 10 "github.com/v2fly/v2ray-core/v4/common/log" 11 ) 12 13 type HandlerCreatorOptions struct { 14 Path string 15 } 16 17 type HandlerCreator func(LogType, HandlerCreatorOptions) (log.Handler, error) 18 19 var handlerCreatorMap = make(map[LogType]HandlerCreator) 20 21 var handlerCreatorMapLock = &sync.RWMutex{} 22 23 func RegisterHandlerCreator(logType LogType, f HandlerCreator) error { 24 if f == nil { 25 return newError("nil HandlerCreator") 26 } 27 28 handlerCreatorMapLock.Lock() 29 defer handlerCreatorMapLock.Unlock() 30 31 handlerCreatorMap[logType] = f 32 return nil 33 } 34 35 func createHandler(logType LogType, options HandlerCreatorOptions) (log.Handler, error) { 36 handlerCreatorMapLock.RLock() 37 defer handlerCreatorMapLock.RUnlock() 38 39 creator, found := handlerCreatorMap[logType] 40 if !found { 41 return nil, newError("unable to create log handler for ", logType) 42 } 43 return creator(logType, options) 44 } 45 46 func init() { 47 common.Must(RegisterHandlerCreator(LogType_Console, func(lt LogType, options HandlerCreatorOptions) (log.Handler, error) { 48 return log.NewLogger(log.CreateStdoutLogWriter()), nil 49 })) 50 51 common.Must(RegisterHandlerCreator(LogType_File, func(lt LogType, options HandlerCreatorOptions) (log.Handler, error) { 52 creator, err := log.CreateFileLogWriter(options.Path) 53 if err != nil { 54 return nil, err 55 } 56 return log.NewLogger(creator), nil 57 })) 58 59 common.Must(RegisterHandlerCreator(LogType_None, func(lt LogType, options HandlerCreatorOptions) (log.Handler, error) { 60 return nil, nil 61 })) 62 }