forked from moby/moby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
factory.go
57 lines (46 loc) · 1.37 KB
/
factory.go
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
50
51
52
53
54
55
56
57
package logger
import (
"fmt"
"sync"
)
// Creator is a method that builds a logging driver instance with given context
type Creator func(Context) (Logger, error)
// Context provides enough information for a logging driver to do its function
type Context struct {
Config map[string]string
ContainerID string
ContainerName string
LogPath string
}
type logdriverFactory struct {
registry map[string]Creator
m sync.Mutex
}
func (lf *logdriverFactory) register(name string, c Creator) error {
lf.m.Lock()
defer lf.m.Unlock()
if _, ok := lf.registry[name]; ok {
return fmt.Errorf("logger: log driver named '%s' is already registered", name)
}
lf.registry[name] = c
return nil
}
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
}
var factory = &logdriverFactory{registry: make(map[string]Creator)} // global factory instance
// RegisterLogDriver registers the given logging driver builder with given logging
// driver name.
func RegisterLogDriver(name string, c Creator) error {
return factory.register(name, c)
}
// GetLogDriver provides the logging driver builder for a logging driver name.
func GetLogDriver(name string) (Creator, error) {
return factory.get(name)
}