Skip to content

Commit

Permalink
utils.Debugf -> log.Printf, move jsonlog to own package.
Browse files Browse the repository at this point in the history
Docker-DCO-1.1-Signed-off-by: Erik Hollensbe <[email protected]> (github: erikh)
  • Loading branch information
Erik Hollensbe committed Aug 6, 2014
1 parent 5ab09f2 commit 5cdb9c8
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 42 deletions.
9 changes: 5 additions & 4 deletions pkg/broadcastwriter/broadcastwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
"bytes"
"encoding/json"
"io"
"log"
"sync"
"time"

"github.com/docker/docker/utils"
"github.com/docker/docker/pkg/jsonlog"
)

// BroadcastWriter accumulate multiple io.WriteCloser by stream.
Expand All @@ -19,7 +20,7 @@ type BroadcastWriter struct {

// AddWriter adds new io.WriteCloser for stream.
// 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) {
w.Lock()
if _, ok := w.streams[stream]; !ok {
Expand Down Expand Up @@ -53,9 +54,9 @@ func (w *BroadcastWriter) Write(p []byte) (n int, err error) {
if stream == "" {
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 {
utils.Errorf("Error making JSON log line: %s", err)
log.Printf("Error making JSON log line: %s", err)
continue
}
b = append(b, '\n')
Expand Down
45 changes: 45 additions & 0 deletions pkg/jsonlog/jsonlog.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
38 changes: 0 additions & 38 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"crypto/sha1"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"io/ioutil"
Expand All @@ -19,7 +18,6 @@ import (
"strings"
"sync"
"syscall"
"time"

"github.com/docker/docker/dockerversion"
)
Expand Down Expand Up @@ -264,42 +262,6 @@ func (r *bufReader) Close() error {
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 {
if fds, err := ioutil.ReadDir(fmt.Sprintf("/proc/%d/fd", os.Getpid())); err != nil {
Errorf("Error opening /proc/%d/fd: %s", os.Getpid(), err)
Expand Down

0 comments on commit 5cdb9c8

Please sign in to comment.