-
Notifications
You must be signed in to change notification settings - Fork 5
/
driver.go
97 lines (84 loc) · 2.04 KB
/
driver.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main
import (
"context"
"encoding/binary"
"fmt"
"io"
"os"
"sync"
"syscall"
"github.com/docker/docker/api/types/plugins/logdriver"
"github.com/docker/docker/daemon/logger"
protoio "github.com/gogo/protobuf/io"
"github.com/pkg/errors"
"github.com/tonistiigi/fifo"
)
type driver struct {
mu sync.Mutex
logs map[string]*dockerInput
}
type dockerInput struct {
stream io.ReadCloser
info logger.Info
}
func newDriver() *driver {
return &driver{
logs: make(map[string]*dockerInput),
}
}
func (d *driver) StartLogging(file string, logCtx logger.Info) error {
d.mu.Lock()
if _, exists := d.logs[file]; exists {
d.mu.Unlock()
return fmt.Errorf("logger for %q already exists", file)
}
d.mu.Unlock()
f, err := fifo.OpenFifo(context.Background(), file, syscall.O_RDONLY, 0700)
if err != nil {
return errors.Wrapf(err, "error opening logger fifo: %q", file)
}
d.mu.Lock()
lf := &dockerInput{f, logCtx}
d.logs[file] = lf
d.mu.Unlock()
d.PrintState()
go consumeLog(lf)
return nil
}
func (d *driver) PrintState() {
fmt.Fprintln(os.Stdout, "New Container added for logging : >")
for k, v := range d.logs {
fmt.Fprintf(os.Stdout, " %s = %s\n", k, v.info.ContainerID)
}
}
func (d *driver) StopLogging(file string) error {
d.mu.Lock()
lf, ok := d.logs[file]
if ok {
lf.stream.Close()
delete(d.logs, file)
}
d.mu.Unlock()
return nil
}
func consumeLog(lf *dockerInput) {
dec := protoio.NewUint32DelimitedReader(lf.stream, binary.BigEndian, 1e6)
defer dec.Close()
var buf logdriver.LogEntry
for {
if err := dec.ReadMsg(&buf); err != nil {
if err == io.EOF {
fmt.Fprintf(os.Stderr, "FIFO Stream closed %s", err.Error())
lf.stream.Close()
return
}
dec = protoio.NewUint32DelimitedReader(lf.stream, binary.BigEndian, 1e6)
}
//write message to stdout
fmt.Fprintln(os.Stdout, fmt.Sprintf("%s: [%s] [%d] %s", lf.info.ContainerID, buf.Source, buf.TimeNano, buf.Line))
buf.Reset()
}
}
func (d *driver) ReadLogs(info logger.Info, config logger.ReadConfig) (io.ReadCloser, error) {
return nil, nil
}