2021-08-21 01:20:40 -04:00
|
|
|
//go:build !confonly
|
2019-02-01 14:08:21 -05:00
|
|
|
// +build !confonly
|
|
|
|
|
2018-12-06 11:37:05 -05:00
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
2021-07-19 11:13:11 -04:00
|
|
|
"sync"
|
|
|
|
|
2021-02-16 15:31:50 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v4/common"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common/log"
|
2018-12-06 11:37:05 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type HandlerCreatorOptions struct {
|
|
|
|
Path string
|
|
|
|
}
|
|
|
|
|
|
|
|
type HandlerCreator func(LogType, HandlerCreatorOptions) (log.Handler, error)
|
|
|
|
|
2021-05-19 17:28:52 -04:00
|
|
|
var handlerCreatorMap = make(map[LogType]HandlerCreator)
|
2018-12-06 11:37:05 -05:00
|
|
|
|
2021-07-19 11:13:11 -04:00
|
|
|
var handlerCreatorMapLock = &sync.RWMutex{}
|
|
|
|
|
2018-12-06 11:37:05 -05:00
|
|
|
func RegisterHandlerCreator(logType LogType, f HandlerCreator) error {
|
|
|
|
if f == nil {
|
|
|
|
return newError("nil HandlerCreator")
|
|
|
|
}
|
|
|
|
|
2021-07-19 11:13:11 -04:00
|
|
|
handlerCreatorMapLock.Lock()
|
|
|
|
defer handlerCreatorMapLock.Unlock()
|
|
|
|
|
2018-12-06 11:37:05 -05:00
|
|
|
handlerCreatorMap[logType] = f
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func createHandler(logType LogType, options HandlerCreatorOptions) (log.Handler, error) {
|
2021-07-19 11:13:11 -04:00
|
|
|
handlerCreatorMapLock.RLock()
|
|
|
|
defer handlerCreatorMapLock.RUnlock()
|
|
|
|
|
2018-12-06 11:37:05 -05:00
|
|
|
creator, found := handlerCreatorMap[logType]
|
|
|
|
if !found {
|
|
|
|
return nil, newError("unable to create log handler for ", logType)
|
|
|
|
}
|
|
|
|
return creator(logType, options)
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
common.Must(RegisterHandlerCreator(LogType_Console, func(lt LogType, options HandlerCreatorOptions) (log.Handler, error) {
|
|
|
|
return log.NewLogger(log.CreateStdoutLogWriter()), nil
|
|
|
|
}))
|
|
|
|
|
|
|
|
common.Must(RegisterHandlerCreator(LogType_File, func(lt LogType, options HandlerCreatorOptions) (log.Handler, error) {
|
|
|
|
creator, err := log.CreateFileLogWriter(options.Path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return log.NewLogger(creator), nil
|
|
|
|
}))
|
|
|
|
|
|
|
|
common.Must(RegisterHandlerCreator(LogType_None, func(lt LogType, options HandlerCreatorOptions) (log.Handler, error) {
|
|
|
|
return nil, nil
|
|
|
|
}))
|
|
|
|
}
|