From: Peter Amstutz Date: Tue, 19 Jan 2016 22:07:59 +0000 (-0500) Subject: 7816: CopyReaderToLog renamed to ReadWriteLines. Use Writer instead of Logger X-Git-Tag: 1.1.0~1166^2 X-Git-Url: https://git.arvados.org/arvados.git/commitdiff_plain/8cb032bf7b9a8db6095d027d4088c4841c07d3f7 7816: CopyReaderToLog renamed to ReadWriteLines. Use Writer instead of Logger to avoid unnecessary copy. --- diff --git a/services/crunch-run/crunchrun.go b/services/crunch-run/crunchrun.go index fd91bc1e71..640ac88ca9 100644 --- a/services/crunch-run/crunchrun.go +++ b/services/crunch-run/crunchrun.go @@ -216,8 +216,8 @@ func (runner *ContainerRunner) AttachLogs() (err error) { runner.Stdout = NewThrottledLogger(runner.NewLogWriter("stdout")) runner.Stderr = NewThrottledLogger(runner.NewLogWriter("stderr")) - go CopyReaderToLog(stdoutReader, runner.Stdout.Logger, runner.loggingDone) - go CopyReaderToLog(stderrReader, runner.Stderr.Logger, runner.loggingDone) + go ReadWriteLines(stdoutReader, runner.Stdout, runner.loggingDone) + go ReadWriteLines(stderrReader, runner.Stderr, runner.loggingDone) return nil } diff --git a/services/crunch-run/logging.go b/services/crunch-run/logging.go index 3860484747..9d97384109 100644 --- a/services/crunch-run/logging.go +++ b/services/crunch-run/logging.go @@ -21,7 +21,7 @@ type Timestamper func(t time.Time) string // ThrottledLogger.buf -> ThrottledLogger.flusher -> goWriter -> // ArvLogWriter.Write -> CollectionFileWriter.Write | Api.Create // -// For stdout/stderr CopyReaderToLog additionally runs as a goroutine to pull +// For stdout/stderr ReadWriteLines additionally runs as a goroutine to pull // data from the stdout/stderr Reader and send to the Logger. // ThrottledLogger accepts writes, prepends a timestamp to each line of the @@ -116,9 +116,9 @@ const ( MaxLogLine = 1 << 12 ) -// CopyReaderToLog reads from a Reader and prints to a Logger, with long line -// splitting. -func CopyReaderToLog(in io.Reader, logger *log.Logger, done chan<- bool) { +// ReadWriteLines reads lines from a reader and writes to a Writer, with long +// line splitting. +func ReadWriteLines(in io.Reader, writer io.Writer, done chan<- bool) { reader := bufio.NewReaderSize(in, MaxLogLine) var prefix string for { @@ -126,13 +126,19 @@ func CopyReaderToLog(in io.Reader, logger *log.Logger, done chan<- bool) { if err == io.EOF { break } else if err != nil { - logger.Print("error reading container log:", err) + writer.Write([]byte(fmt.Sprintln("error reading container log:", err))) } var suffix string if isPrefix { - suffix = "[...]" + suffix = "[...]\n" } - logger.Print(prefix, string(line), suffix) + + if prefix == "" && suffix == "" { + writer.Write(line) + } else { + writer.Write([]byte(fmt.Sprint(prefix, string(line), suffix))) + } + // Set up prefix for following line if isPrefix { prefix = "[...]"