github.com/chenchun/docker@v1.3.2-0.20150629222414-20467faf132b/daemon/logger/factory.go (about) 1 package logger 2 3 import ( 4 "fmt" 5 "os" 6 "strings" 7 "sync" 8 "time" 9 ) 10 11 // Creator is a method that builds a logging driver instance with given context 12 type Creator func(Context) (Logger, error) 13 14 // Context provides enough information for a logging driver to do its function 15 type Context struct { 16 Config map[string]string 17 ContainerID string 18 ContainerName string 19 ContainerEntrypoint string 20 ContainerArgs []string 21 ContainerImageID string 22 ContainerImageName string 23 ContainerCreated time.Time 24 LogPath string 25 } 26 27 func (ctx *Context) Hostname() (string, error) { 28 hostname, err := os.Hostname() 29 if err != nil { 30 return "", fmt.Errorf("logger: can not resolve hostname: %v", err) 31 } 32 return hostname, nil 33 } 34 35 func (ctx *Context) Command() string { 36 terms := []string{ctx.ContainerEntrypoint} 37 for _, arg := range ctx.ContainerArgs { 38 terms = append(terms, arg) 39 } 40 command := strings.Join(terms, " ") 41 return command 42 } 43 44 type logdriverFactory struct { 45 registry map[string]Creator 46 m sync.Mutex 47 } 48 49 func (lf *logdriverFactory) register(name string, c Creator) error { 50 lf.m.Lock() 51 defer lf.m.Unlock() 52 53 if _, ok := lf.registry[name]; ok { 54 return fmt.Errorf("logger: log driver named '%s' is already registered", name) 55 } 56 lf.registry[name] = c 57 return nil 58 } 59 60 func (lf *logdriverFactory) get(name string) (Creator, error) { 61 lf.m.Lock() 62 defer lf.m.Unlock() 63 64 c, ok := lf.registry[name] 65 if !ok { 66 return c, fmt.Errorf("logger: no log driver named '%s' is registered", name) 67 } 68 return c, nil 69 } 70 71 var factory = &logdriverFactory{registry: make(map[string]Creator)} // global factory instance 72 73 // RegisterLogDriver registers the given logging driver builder with given logging 74 // driver name. 75 func RegisterLogDriver(name string, c Creator) error { 76 return factory.register(name, c) 77 } 78 79 // GetLogDriver provides the logging driver builder for a logging driver name. 80 func GetLogDriver(name string) (Creator, error) { 81 return factory.get(name) 82 }