Skip to content

Commit

Permalink
libct/logs.ForwardLogs: use bufio.Scanner
Browse files Browse the repository at this point in the history
Error handling is slightly cleaner this way.

Signed-off-by: Kir Kolyshkin <[email protected]>
  • Loading branch information
kolyshkin committed Jul 14, 2020
1 parent 5d01054 commit 13d3cef
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions libcontainer/logs/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,14 @@ type Config struct {
}

func ForwardLogs(logPipe io.Reader) {
lineReader := bufio.NewReader(logPipe)
for {
line, err := lineReader.ReadBytes('\n')
if len(line) > 0 {
processEntry(line)
}
if err == io.EOF {
logrus.Debugf("log pipe has been closed: %+v", err)
return
}
if err != nil {
logrus.Errorf("log pipe read error: %+v", err)
}
s := bufio.NewScanner(logPipe)
for s.Scan() {
processEntry(s.Bytes())
}
if err := s.Err(); err != nil {
logrus.Errorf("log pipe read error: %+v", err)
} else {
logrus.Debugf("log pipe closed")
}
}

Expand All @@ -48,6 +43,10 @@ func processEntry(text []byte) {
Msg string `json:"msg"`
}

if len(text) == 0 {
return
}

var jl jsonLog
if err := json.Unmarshal(text, &jl); err != nil {
logrus.Errorf("failed to decode %q to json: %+v", text, err)
Expand Down

0 comments on commit 13d3cef

Please sign in to comment.