Merge pull request #8109 from unclejack/avoid_jsonlog_alloc

daemon/logs: avoid JSONLog struct alloc in loop
This commit is contained in:
Tibor Vass 2014-09-22 12:53:31 -04:00
Родитель c8f5c686a5 d2c104c3a0
Коммит ac75835931
2 изменённых файлов: 12 добавлений и 6 удалений

Просмотреть файл

@ -88,9 +88,8 @@ func (daemon *Daemon) ContainerLogs(job *engine.Job) engine.Status {
cLog = tmp cLog = tmp
} }
dec := json.NewDecoder(cLog) dec := json.NewDecoder(cLog)
l := &jsonlog.JSONLog{}
for { for {
l := &jsonlog.JSONLog{}
if err := dec.Decode(l); err == io.EOF { if err := dec.Decode(l); err == io.EOF {
break break
} else if err != nil { } else if err != nil {
@ -102,11 +101,12 @@ func (daemon *Daemon) ContainerLogs(job *engine.Job) engine.Status {
logLine = fmt.Sprintf("%s %s", l.Created.Format(format), logLine) logLine = fmt.Sprintf("%s %s", l.Created.Format(format), logLine)
} }
if l.Stream == "stdout" && stdout { if l.Stream == "stdout" && stdout {
fmt.Fprintf(job.Stdout, "%s", logLine) io.WriteString(job.Stdout, logLine)
} }
if l.Stream == "stderr" && stderr { if l.Stream == "stderr" && stderr {
fmt.Fprintf(job.Stderr, "%s", logLine) io.WriteString(job.Stderr, logLine)
} }
l.Reset()
} }
} }
} }

Просмотреть файл

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