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
}
dec := json.NewDecoder(cLog)
l := &jsonlog.JSONLog{}
for {
l := &jsonlog.JSONLog{}
if err := dec.Decode(l); err == io.EOF {
break
} 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)
}
if l.Stream == "stdout" && stdout {
fmt.Fprintf(job.Stdout, "%s", logLine)
io.WriteString(job.Stdout, logLine)
}
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
}
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()
}
}