2015-04-09 00:23:30 -04:00
|
|
|
package logger
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2015-07-21 18:26:52 -04:00
|
|
|
// Creator builds a logging driver instance with given context.
|
2015-04-09 00:23:30 -04:00
|
|
|
type Creator func(Context) (Logger, error)
|
|
|
|
|
2015-07-21 18:26:52 -04:00
|
|
|
// LogOptValidator checks the options specific to the underlying
|
|
|
|
// logging implementation.
|
2015-06-30 20:40:13 -04:00
|
|
|
type LogOptValidator func(cfg map[string]string) error
|
|
|
|
|
2015-04-09 00:23:30 -04:00
|
|
|
type logdriverFactory struct {
|
2015-06-30 20:40:13 -04:00
|
|
|
registry map[string]Creator
|
|
|
|
optValidator map[string]LogOptValidator
|
|
|
|
m sync.Mutex
|
2015-04-09 00:23:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (lf *logdriverFactory) register(name string, c Creator) error {
|
2016-03-12 07:50:37 -05:00
|
|
|
if lf.driverRegistered(name) {
|
2015-04-09 00:23:30 -04:00
|
|
|
return fmt.Errorf("logger: log driver named '%s' is already registered", name)
|
|
|
|
}
|
2016-03-12 07:50:37 -05:00
|
|
|
|
|
|
|
lf.m.Lock()
|
2015-04-09 00:23:30 -04:00
|
|
|
lf.registry[name] = c
|
2016-03-12 07:50:37 -05:00
|
|
|
lf.m.Unlock()
|
2015-04-09 00:23:30 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-03-12 07:50:37 -05:00
|
|
|
func (lf *logdriverFactory) driverRegistered(name string) bool {
|
|
|
|
lf.m.Lock()
|
|
|
|
_, ok := lf.registry[name]
|
|
|
|
lf.m.Unlock()
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2015-06-30 20:40:13 -04:00
|
|
|
func (lf *logdriverFactory) registerLogOptValidator(name string, l LogOptValidator) error {
|
|
|
|
lf.m.Lock()
|
|
|
|
defer lf.m.Unlock()
|
|
|
|
|
|
|
|
if _, ok := lf.optValidator[name]; ok {
|
2015-07-21 18:26:52 -04:00
|
|
|
return fmt.Errorf("logger: log validator named '%s' is already registered", name)
|
2015-06-30 20:40:13 -04:00
|
|
|
}
|
|
|
|
lf.optValidator[name] = l
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-04-09 00:23:30 -04:00
|
|
|
func (lf *logdriverFactory) get(name string) (Creator, error) {
|
|
|
|
lf.m.Lock()
|
|
|
|
defer lf.m.Unlock()
|
|
|
|
|
|
|
|
c, ok := lf.registry[name]
|
|
|
|
if !ok {
|
|
|
|
return c, fmt.Errorf("logger: no log driver named '%s' is registered", name)
|
|
|
|
}
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2015-06-30 20:40:13 -04:00
|
|
|
func (lf *logdriverFactory) getLogOptValidator(name string) LogOptValidator {
|
|
|
|
lf.m.Lock()
|
|
|
|
defer lf.m.Unlock()
|
|
|
|
|
|
|
|
c, _ := lf.optValidator[name]
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
var factory = &logdriverFactory{registry: make(map[string]Creator), optValidator: make(map[string]LogOptValidator)} // global factory instance
|
2015-04-09 00:23:30 -04:00
|
|
|
|
|
|
|
// RegisterLogDriver registers the given logging driver builder with given logging
|
|
|
|
// driver name.
|
|
|
|
func RegisterLogDriver(name string, c Creator) error {
|
|
|
|
return factory.register(name, c)
|
|
|
|
}
|
|
|
|
|
2015-07-21 18:26:52 -04:00
|
|
|
// RegisterLogOptValidator registers the logging option validator with
|
|
|
|
// the given logging driver name.
|
2015-06-30 20:40:13 -04:00
|
|
|
func RegisterLogOptValidator(name string, l LogOptValidator) error {
|
|
|
|
return factory.registerLogOptValidator(name, l)
|
|
|
|
}
|
|
|
|
|
2015-04-09 00:23:30 -04:00
|
|
|
// GetLogDriver provides the logging driver builder for a logging driver name.
|
|
|
|
func GetLogDriver(name string) (Creator, error) {
|
|
|
|
return factory.get(name)
|
|
|
|
}
|
2015-06-30 20:40:13 -04:00
|
|
|
|
2015-07-21 18:26:52 -04:00
|
|
|
// ValidateLogOpts checks the options for the given log driver. The
|
|
|
|
// options supported are specific to the LogDriver implementation.
|
2015-06-30 20:40:13 -04:00
|
|
|
func ValidateLogOpts(name string, cfg map[string]string) error {
|
2016-03-12 07:50:37 -05:00
|
|
|
if name == "none" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if !factory.driverRegistered(name) {
|
|
|
|
return fmt.Errorf("logger: no log driver named '%s' is registered", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
validator := factory.getLogOptValidator(name)
|
|
|
|
if validator != nil {
|
|
|
|
return validator(cfg)
|
2015-06-30 20:40:13 -04:00
|
|
|
}
|
2015-07-24 12:00:50 -04:00
|
|
|
return nil
|
2015-06-30 20:40:13 -04:00
|
|
|
}
|