2015-02-03 18:41:26 -05:00
|
|
|
package logger
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2015-05-28 16:59:25 -04:00
|
|
|
"bytes"
|
2015-02-03 18:41:26 -05:00
|
|
|
"io"
|
2015-03-18 14:44:14 -04:00
|
|
|
"sync"
|
2015-02-03 18:41:26 -05:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Copier can copy logs from specified sources to Logger and attach
|
|
|
|
// ContainerID and Timestamp.
|
|
|
|
// Writes are concurrent, so you need implement some sync in your logger
|
|
|
|
type Copier struct {
|
2015-07-28 14:17:25 -04:00
|
|
|
// cid is the container id for which we are copying logs
|
2015-02-03 18:41:26 -05:00
|
|
|
cid string
|
|
|
|
// srcs is map of name -> reader pairs, for example "stdout", "stderr"
|
2015-03-18 14:44:14 -04:00
|
|
|
srcs map[string]io.Reader
|
|
|
|
dst Logger
|
|
|
|
copyJobs sync.WaitGroup
|
2016-01-15 08:42:23 -05:00
|
|
|
closed chan struct{}
|
2015-02-03 18:41:26 -05:00
|
|
|
}
|
|
|
|
|
2015-07-28 14:17:25 -04:00
|
|
|
// NewCopier creates a new Copier
|
|
|
|
func NewCopier(cid string, srcs map[string]io.Reader, dst Logger) *Copier {
|
2015-02-03 18:41:26 -05:00
|
|
|
return &Copier{
|
2016-01-15 08:42:23 -05:00
|
|
|
cid: cid,
|
|
|
|
srcs: srcs,
|
|
|
|
dst: dst,
|
|
|
|
closed: make(chan struct{}),
|
2015-07-28 14:17:25 -04:00
|
|
|
}
|
2015-02-03 18:41:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run starts logs copying
|
|
|
|
func (c *Copier) Run() {
|
|
|
|
for src, w := range c.srcs {
|
2015-03-18 14:44:14 -04:00
|
|
|
c.copyJobs.Add(1)
|
2015-02-03 18:41:26 -05:00
|
|
|
go c.copySrc(src, w)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Copier) copySrc(name string, src io.Reader) {
|
2015-03-18 14:44:14 -04:00
|
|
|
defer c.copyJobs.Done()
|
2015-05-28 16:59:25 -04:00
|
|
|
reader := bufio.NewReader(src)
|
|
|
|
|
|
|
|
for {
|
2016-01-15 08:42:23 -05:00
|
|
|
select {
|
|
|
|
case <-c.closed:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
line, err := reader.ReadBytes('\n')
|
|
|
|
line = bytes.TrimSuffix(line, []byte{'\n'})
|
2015-05-28 16:59:25 -04:00
|
|
|
|
2016-01-15 08:42:23 -05:00
|
|
|
// ReadBytes can return full or partial output even when it failed.
|
|
|
|
// e.g. it can return a full entry and EOF.
|
|
|
|
if err == nil || len(line) > 0 {
|
|
|
|
if logErr := c.dst.Log(&Message{ContainerID: c.cid, Line: line, Source: name, Timestamp: time.Now().UTC()}); logErr != nil {
|
|
|
|
logrus.Errorf("Failed to log msg %q for logger %s: %s", line, c.dst.Name(), logErr)
|
|
|
|
}
|
2015-05-28 16:59:25 -04:00
|
|
|
}
|
|
|
|
|
2016-01-15 08:42:23 -05:00
|
|
|
if err != nil {
|
|
|
|
if err != io.EOF {
|
|
|
|
logrus.Errorf("Error scanning log stream: %s", err)
|
|
|
|
}
|
|
|
|
return
|
2015-05-28 16:59:25 -04:00
|
|
|
}
|
|
|
|
}
|
2015-02-03 18:41:26 -05:00
|
|
|
}
|
|
|
|
}
|
2015-03-18 14:44:14 -04:00
|
|
|
|
|
|
|
// Wait waits until all copying is done
|
|
|
|
func (c *Copier) Wait() {
|
|
|
|
c.copyJobs.Wait()
|
|
|
|
}
|
2016-01-15 08:42:23 -05:00
|
|
|
|
|
|
|
// Close closes the copier
|
|
|
|
func (c *Copier) Close() {
|
|
|
|
select {
|
|
|
|
case <-c.closed:
|
|
|
|
default:
|
|
|
|
close(c.closed)
|
|
|
|
}
|
|
|
|
}
|