Hunter0x7c7
2022-08-11 a82f9cb69f63aaeba40c024960deda7d75b9fece
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package log
 
import (
    "github.com/v2fly/v2ray-core/v5/common"
    "github.com/v2fly/v2ray-core/v5/common/log"
)
 
type HandlerCreatorOptions struct {
    Path string
}
 
type HandlerCreator func(LogType, HandlerCreatorOptions) (log.Handler, error)
 
var handlerCreatorMap = make(map[LogType]HandlerCreator)
 
func RegisterHandlerCreator(logType LogType, f HandlerCreator) error {
    if f == nil {
        return newError("nil HandlerCreator")
    }
 
    handlerCreatorMap[logType] = f
    return nil
}
 
func createHandler(logType LogType, options HandlerCreatorOptions) (log.Handler, error) {
    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
    }))
}