pkg/jsonlog: avoid JSONLog allocation in loop

Docker-DCO-1.1-Signed-off-by: Cristian Staretu <cristian.staretu@gmail.com> (github: unclejack)
This commit is contained in:
unclejack 2014-09-18 18:04:18 +03:00
parent be805c6709
commit deaded4ca8

View file

@ -25,11 +25,16 @@ func (jl *JSONLog) Format(format string) (string, error) {
return fmt.Sprintf("[%s] %s", jl.Created.Format(format), jl.Log), nil
}
func (jl *JSONLog) Reset() {
jl.Log = ""
jl.Stream = ""
jl.Created = time.Time{}
}
func WriteLog(src io.Reader, dst io.Writer, format string) error {
dec := json.NewDecoder(src)
l := &JSONLog{}
for {
l := &JSONLog{}
if err := dec.Decode(l); err == io.EOF {
return nil
} else if err != nil {
@ -43,5 +48,6 @@ func WriteLog(src io.Reader, dst io.Writer, format string) error {
if _, err := io.WriteString(dst, line); err != nil {
return err
}
l.Reset()
}
}