Merge pull request #7329 from erikh/move_broadcastwriter

Cleanup: utils/broadcastwriter -> pkg/broadcastwriter
This commit is contained in:
Michael Crosby 2014-08-07 14:51:42 -07:00
commit bc9f5cdeb5
8 changed files with 58 additions and 48 deletions

View File

@ -8,6 +8,7 @@ import (
"time" "time"
"github.com/docker/docker/engine" "github.com/docker/docker/engine"
"github.com/docker/docker/pkg/jsonlog"
"github.com/docker/docker/utils" "github.com/docker/docker/utils"
) )
@ -57,7 +58,7 @@ func (daemon *Daemon) ContainerAttach(job *engine.Job) engine.Status {
} else { } else {
dec := json.NewDecoder(cLog) dec := json.NewDecoder(cLog)
for { for {
l := &utils.JSONLog{} l := &jsonlog.JSONLog{}
if err := dec.Decode(l); err == io.EOF { if err := dec.Decode(l); err == io.EOF {
break break

View File

@ -22,12 +22,12 @@ import (
"github.com/docker/docker/image" "github.com/docker/docker/image"
"github.com/docker/docker/links" "github.com/docker/docker/links"
"github.com/docker/docker/nat" "github.com/docker/docker/nat"
"github.com/docker/docker/pkg/broadcastwriter"
"github.com/docker/docker/pkg/networkfs/etchosts" "github.com/docker/docker/pkg/networkfs/etchosts"
"github.com/docker/docker/pkg/networkfs/resolvconf" "github.com/docker/docker/pkg/networkfs/resolvconf"
"github.com/docker/docker/pkg/symlink" "github.com/docker/docker/pkg/symlink"
"github.com/docker/docker/runconfig" "github.com/docker/docker/runconfig"
"github.com/docker/docker/utils" "github.com/docker/docker/utils"
"github.com/docker/docker/utils/broadcastwriter"
"github.com/docker/libcontainer/devices" "github.com/docker/libcontainer/devices"
"github.com/docker/libcontainer/label" "github.com/docker/libcontainer/label"
) )

View File

@ -26,6 +26,7 @@ import (
"github.com/docker/docker/engine" "github.com/docker/docker/engine"
"github.com/docker/docker/graph" "github.com/docker/docker/graph"
"github.com/docker/docker/image" "github.com/docker/docker/image"
"github.com/docker/docker/pkg/broadcastwriter"
"github.com/docker/docker/pkg/graphdb" "github.com/docker/docker/pkg/graphdb"
"github.com/docker/docker/pkg/namesgenerator" "github.com/docker/docker/pkg/namesgenerator"
"github.com/docker/docker/pkg/networkfs/resolvconf" "github.com/docker/docker/pkg/networkfs/resolvconf"
@ -35,7 +36,6 @@ import (
"github.com/docker/docker/pkg/truncindex" "github.com/docker/docker/pkg/truncindex"
"github.com/docker/docker/runconfig" "github.com/docker/docker/runconfig"
"github.com/docker/docker/utils" "github.com/docker/docker/utils"
"github.com/docker/docker/utils/broadcastwriter"
"github.com/docker/libcontainer/label" "github.com/docker/libcontainer/label"
) )

View File

@ -12,6 +12,7 @@ import (
"github.com/docker/docker/pkg/tailfile" "github.com/docker/docker/pkg/tailfile"
"github.com/docker/docker/engine" "github.com/docker/docker/engine"
"github.com/docker/docker/pkg/jsonlog"
"github.com/docker/docker/utils" "github.com/docker/docker/utils"
) )
@ -89,7 +90,7 @@ func (daemon *Daemon) ContainerLogs(job *engine.Job) engine.Status {
} }
dec := json.NewDecoder(cLog) dec := json.NewDecoder(cLog)
for { for {
l := &utils.JSONLog{} l := &jsonlog.JSONLog{}
if err := dec.Decode(l); err == io.EOF { if err := dec.Decode(l); err == io.EOF {
break break
@ -115,13 +116,13 @@ func (daemon *Daemon) ContainerLogs(job *engine.Job) engine.Status {
if stdout { if stdout {
stdoutPipe := container.StdoutLogPipe() stdoutPipe := container.StdoutLogPipe()
go func() { go func() {
errors <- utils.WriteLog(stdoutPipe, job.Stdout, format) errors <- jsonlog.WriteLog(stdoutPipe, job.Stdout, format)
}() }()
} }
if stderr { if stderr {
stderrPipe := container.StderrLogPipe() stderrPipe := container.StderrLogPipe()
go func() { go func() {
errors <- utils.WriteLog(stderrPipe, job.Stderr, format) errors <- jsonlog.WriteLog(stderrPipe, job.Stderr, format)
}() }()
} }
err := <-errors err := <-errors

View File

@ -4,10 +4,11 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"io" "io"
"log"
"sync" "sync"
"time" "time"
"github.com/docker/docker/utils" "github.com/docker/docker/pkg/jsonlog"
) )
// BroadcastWriter accumulate multiple io.WriteCloser by stream. // BroadcastWriter accumulate multiple io.WriteCloser by stream.
@ -19,7 +20,7 @@ type BroadcastWriter struct {
// AddWriter adds new io.WriteCloser for stream. // AddWriter adds new io.WriteCloser for stream.
// If stream is "", then all writes proceed as is. Otherwise every line from // If stream is "", then all writes proceed as is. Otherwise every line from
// input will be packed to serialized utils.JSONLog. // input will be packed to serialized jsonlog.JSONLog.
func (w *BroadcastWriter) AddWriter(writer io.WriteCloser, stream string) { func (w *BroadcastWriter) AddWriter(writer io.WriteCloser, stream string) {
w.Lock() w.Lock()
if _, ok := w.streams[stream]; !ok { if _, ok := w.streams[stream]; !ok {
@ -53,9 +54,9 @@ func (w *BroadcastWriter) Write(p []byte) (n int, err error) {
if stream == "" { if stream == "" {
continue continue
} }
b, err := json.Marshal(utils.JSONLog{Log: line, Stream: stream, Created: created}) b, err := json.Marshal(jsonlog.JSONLog{Log: line, Stream: stream, Created: created})
if err != nil { if err != nil {
utils.Errorf("Error making JSON log line: %s", err) log.Printf("Error making JSON log line: %s", err)
continue continue
} }
b = append(b, '\n') b = append(b, '\n')

45
pkg/jsonlog/jsonlog.go Normal file
View File

@ -0,0 +1,45 @@
package jsonlog
import (
"encoding/json"
"fmt"
"io"
"log"
"time"
)
type JSONLog struct {
Log string `json:"log,omitempty"`
Stream string `json:"stream,omitempty"`
Created time.Time `json:"time"`
}
func (jl *JSONLog) Format(format string) (string, error) {
if format == "" {
return jl.Log, nil
}
if format == "json" {
m, err := json.Marshal(jl)
return string(m), err
}
return fmt.Sprintf("[%s] %s", jl.Created.Format(format), jl.Log), nil
}
func WriteLog(src io.Reader, dst io.WriteCloser, format string) error {
dec := json.NewDecoder(src)
for {
l := &JSONLog{}
if err := dec.Decode(l); err == io.EOF {
return nil
} else if err != nil {
log.Printf("Error streaming logs: %s", err)
return err
}
line, err := l.Format(format)
if err != nil {
return err
}
fmt.Fprintf(dst, "%s", line)
}
}

View File

@ -6,7 +6,6 @@ import (
"crypto/sha1" "crypto/sha1"
"crypto/sha256" "crypto/sha256"
"encoding/hex" "encoding/hex"
"encoding/json"
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
@ -19,7 +18,6 @@ import (
"strings" "strings"
"sync" "sync"
"syscall" "syscall"
"time"
"github.com/docker/docker/dockerversion" "github.com/docker/docker/dockerversion"
) )
@ -264,42 +262,6 @@ func (r *bufReader) Close() error {
return closer.Close() return closer.Close()
} }
type JSONLog struct {
Log string `json:"log,omitempty"`
Stream string `json:"stream,omitempty"`
Created time.Time `json:"time"`
}
func (jl *JSONLog) Format(format string) (string, error) {
if format == "" {
return jl.Log, nil
}
if format == "json" {
m, err := json.Marshal(jl)
return string(m), err
}
return fmt.Sprintf("[%s] %s", jl.Created.Format(format), jl.Log), nil
}
func WriteLog(src io.Reader, dst io.WriteCloser, format string) error {
dec := json.NewDecoder(src)
for {
l := &JSONLog{}
if err := dec.Decode(l); err == io.EOF {
return nil
} else if err != nil {
Errorf("Error streaming logs: %s", err)
return err
}
line, err := l.Format(format)
if err != nil {
return err
}
fmt.Fprintf(dst, "%s", line)
}
}
func GetTotalUsedFds() int { func GetTotalUsedFds() int {
if fds, err := ioutil.ReadDir(fmt.Sprintf("/proc/%d/fd", os.Getpid())); err != nil { if fds, err := ioutil.ReadDir(fmt.Sprintf("/proc/%d/fd", os.Getpid())); err != nil {
Errorf("Error opening /proc/%d/fd: %s", os.Getpid(), err) Errorf("Error opening /proc/%d/fd: %s", os.Getpid(), err)