2014-07-31 17:03:21 -04:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"strconv"
|
2015-04-14 00:36:12 -04:00
|
|
|
"time"
|
2014-07-31 17:03:21 -04:00
|
|
|
|
2015-03-26 18:22:04 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
2015-06-30 20:40:13 -04:00
|
|
|
"github.com/docker/docker/daemon/logger"
|
2015-04-11 17:49:14 -04:00
|
|
|
"github.com/docker/docker/pkg/stdcopy"
|
2014-07-31 17:03:21 -04:00
|
|
|
)
|
|
|
|
|
2015-04-11 17:49:14 -04:00
|
|
|
type ContainerLogsConfig struct {
|
|
|
|
Follow, Timestamps bool
|
|
|
|
Tail string
|
2015-04-14 00:36:12 -04:00
|
|
|
Since time.Time
|
2015-04-11 17:49:14 -04:00
|
|
|
UseStdout, UseStderr bool
|
|
|
|
OutStream io.Writer
|
2015-06-04 15:15:33 -04:00
|
|
|
Stop <-chan bool
|
2015-04-11 17:49:14 -04:00
|
|
|
}
|
2014-07-31 17:03:21 -04:00
|
|
|
|
2015-07-03 09:50:06 -04:00
|
|
|
func (daemon *Daemon) ContainerLogs(container *Container, config *ContainerLogsConfig) error {
|
2015-04-11 17:49:14 -04:00
|
|
|
if !(config.UseStdout || config.UseStderr) {
|
2015-03-25 03:44:12 -04:00
|
|
|
return fmt.Errorf("You must choose at least one stream")
|
2014-07-31 17:03:21 -04:00
|
|
|
}
|
2015-04-11 17:49:14 -04:00
|
|
|
|
2015-07-03 09:50:06 -04:00
|
|
|
outStream := config.OutStream
|
|
|
|
errStream := outStream
|
2015-04-11 17:49:14 -04:00
|
|
|
if !container.Config.Tty {
|
|
|
|
errStream = stdcopy.NewStdWriter(outStream, stdcopy.Stderr)
|
|
|
|
outStream = stdcopy.NewStdWriter(outStream, stdcopy.Stdout)
|
2015-06-30 20:40:13 -04:00
|
|
|
}
|
|
|
|
|
2015-07-03 09:50:06 -04:00
|
|
|
cLog, err := container.getLogger()
|
2015-04-09 00:23:30 -04:00
|
|
|
if err != nil {
|
2015-06-30 20:40:13 -04:00
|
|
|
return err
|
|
|
|
}
|
2015-07-03 09:50:06 -04:00
|
|
|
logReader, ok := cLog.(logger.LogReader)
|
2015-06-30 20:40:13 -04:00
|
|
|
if !ok {
|
2015-07-03 09:50:06 -04:00
|
|
|
return logger.ErrReadLogsNotSupported
|
2014-07-31 17:03:21 -04:00
|
|
|
}
|
2015-05-06 21:09:27 -04:00
|
|
|
|
2015-07-03 09:50:06 -04:00
|
|
|
follow := config.Follow && container.IsRunning()
|
|
|
|
tailLines, err := strconv.Atoi(config.Tail)
|
|
|
|
if err != nil {
|
|
|
|
tailLines = -1
|
|
|
|
}
|
2015-04-23 18:08:41 -04:00
|
|
|
|
2015-07-03 09:50:06 -04:00
|
|
|
logrus.Debug("logs: begin stream")
|
|
|
|
readConfig := logger.ReadConfig{
|
|
|
|
Since: config.Since,
|
|
|
|
Tail: tailLines,
|
|
|
|
Follow: follow,
|
|
|
|
}
|
|
|
|
logs := logReader.ReadLogs(readConfig)
|
2014-10-30 15:33:26 -04:00
|
|
|
|
2015-07-03 09:50:06 -04:00
|
|
|
for {
|
2015-06-04 15:15:33 -04:00
|
|
|
select {
|
2015-07-03 09:50:06 -04:00
|
|
|
case err := <-logs.Err:
|
|
|
|
logrus.Errorf("Error streaming logs: %v", err)
|
|
|
|
return nil
|
2015-06-04 15:15:33 -04:00
|
|
|
case <-config.Stop:
|
2015-07-03 09:50:06 -04:00
|
|
|
logs.Close()
|
|
|
|
return nil
|
|
|
|
case msg, ok := <-logs.Msg:
|
|
|
|
if !ok {
|
|
|
|
logrus.Debugf("logs: end stream")
|
|
|
|
return nil
|
2015-06-04 15:15:33 -04:00
|
|
|
}
|
2015-07-03 09:50:06 -04:00
|
|
|
logLine := msg.Line
|
|
|
|
if config.Timestamps {
|
|
|
|
logLine = append([]byte(msg.Timestamp.Format(logger.TimeFormat)+" "), logLine...)
|
2015-06-04 15:15:33 -04:00
|
|
|
}
|
2015-07-03 09:50:06 -04:00
|
|
|
if msg.Source == "stdout" && config.UseStdout {
|
|
|
|
outStream.Write(logLine)
|
|
|
|
}
|
|
|
|
if msg.Source == "stderr" && config.UseStderr {
|
|
|
|
errStream.Write(logLine)
|
2014-10-30 15:33:26 -04:00
|
|
|
}
|
2014-07-31 17:03:21 -04:00
|
|
|
}
|
|
|
|
}
|
2015-06-30 20:40:13 -04:00
|
|
|
}
|