github.com/flavio/docker@v0.1.3-0.20170117145210-f63d1a6eec47/daemon/logger/factory.go (about) 1 package logger 2 3 import ( 4 "fmt" 5 "sync" 6 ) 7 8 // Creator builds a logging driver instance with given context. 9 type Creator func(Info) (Logger, error) 10 11 // LogOptValidator checks the options specific to the underlying 12 // logging implementation. 13 type LogOptValidator func(cfg map[string]string) error 14 15 type logdriverFactory struct { 16 registry map[string]Creator 17 optValidator map[string]LogOptValidator 18 m sync.Mutex 19 } 20 21 func (lf *logdriverFactory) register(name string, c Creator) error { 22 if lf.driverRegistered(name) { 23 return fmt.Errorf("logger: log driver named '%s' is already registered", name) 24 } 25 26 lf.m.Lock() 27 lf.registry[name] = c 28 lf.m.Unlock() 29 return nil 30 } 31 32 func (lf *logdriverFactory) driverRegistered(name string) bool { 33 lf.m.Lock() 34 _, ok := lf.registry[name] 35 lf.m.Unlock() 36 return ok 37 } 38 39 func (lf *logdriverFactory) registerLogOptValidator(name string, l LogOptValidator) error { 40 lf.m.Lock() 41 defer lf.m.Unlock() 42 43 if _, ok := lf.optValidator[name]; ok { 44 return fmt.Errorf("logger: log validator named '%s' is already registered", name) 45 } 46 lf.optValidator[name] = l 47 return nil 48 } 49 50 func (lf *logdriverFactory) get(name string) (Creator, error) { 51 lf.m.Lock() 52 defer lf.m.Unlock() 53 54 c, ok := lf.registry[name] 55 if !ok { 56 return c, fmt.Errorf("logger: no log driver named '%s' is registered", name) 57 } 58 return c, nil 59 } 60 61 func (lf *logdriverFactory) getLogOptValidator(name string) LogOptValidator { 62 lf.m.Lock() 63 defer lf.m.Unlock() 64 65 c, _ := lf.optValidator[name] 66 return c 67 } 68 69 var factory = &logdriverFactory{registry: make(map[string]Creator), optValidator: make(map[string]LogOptValidator)} // global factory instance 70 71 // RegisterLogDriver registers the given logging driver builder with given logging 72 // driver name. 73 func RegisterLogDriver(name string, c Creator) error { 74 return factory.register(name, c) 75 } 76 77 // RegisterLogOptValidator registers the logging option validator with 78 // the given logging driver name. 79 func RegisterLogOptValidator(name string, l LogOptValidator) error { 80 return factory.registerLogOptValidator(name, l) 81 } 82 83 // GetLogDriver provides the logging driver builder for a logging driver name. 84 func GetLogDriver(name string) (Creator, error) { 85 return factory.get(name) 86 } 87 88 // ValidateLogOpts checks the options for the given log driver. The 89 // options supported are specific to the LogDriver implementation. 90 func ValidateLogOpts(name string, cfg map[string]string) error { 91 if name == "none" { 92 return nil 93 } 94 95 if !factory.driverRegistered(name) { 96 return fmt.Errorf("logger: no log driver named '%s' is registered", name) 97 } 98 99 validator := factory.getLogOptValidator(name) 100 if validator != nil { 101 return validator(cfg) 102 } 103 return nil 104 }