github.com/Uhtred009/v2ray-core-1@v4.31.2+incompatible/app/log/log_creator.go (about)

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