github.com/circular-dark/docker@v1.7.0/daemon/logger/factory.go (about)

     1  package logger
     2  
     3  import (
     4  	"fmt"
     5  	"sync"
     6  )
     7  
     8  // Creator is a method that builds a logging driver instance with given context
     9  type Creator func(Context) (Logger, error)
    10  
    11  // Context provides enough information for a logging driver to do its function
    12  type Context struct {
    13  	Config        map[string]string
    14  	ContainerID   string
    15  	ContainerName string
    16  	LogPath       string
    17  }
    18  
    19  type logdriverFactory struct {
    20  	registry map[string]Creator
    21  	m        sync.Mutex
    22  }
    23  
    24  func (lf *logdriverFactory) register(name string, c Creator) error {
    25  	lf.m.Lock()
    26  	defer lf.m.Unlock()
    27  
    28  	if _, ok := lf.registry[name]; ok {
    29  		return fmt.Errorf("logger: log driver named '%s' is already registered", name)
    30  	}
    31  	lf.registry[name] = c
    32  	return nil
    33  }
    34  
    35  func (lf *logdriverFactory) get(name string) (Creator, error) {
    36  	lf.m.Lock()
    37  	defer lf.m.Unlock()
    38  
    39  	c, ok := lf.registry[name]
    40  	if !ok {
    41  		return c, fmt.Errorf("logger: no log driver named '%s' is registered", name)
    42  	}
    43  	return c, nil
    44  }
    45  
    46  var factory = &logdriverFactory{registry: make(map[string]Creator)} // global factory instance
    47  
    48  // RegisterLogDriver registers the given logging driver builder with given logging
    49  // driver name.
    50  func RegisterLogDriver(name string, c Creator) error {
    51  	return factory.register(name, c)
    52  }
    53  
    54  // GetLogDriver provides the logging driver builder for a logging driver name.
    55  func GetLogDriver(name string) (Creator, error) {
    56  	return factory.get(name)
    57  }