From 29d5ca4976ef491f04ed88a286656b2a94453c06 Mon Sep 17 00:00:00 2001 From: Tom Clegg Date: Thu, 30 Jun 2016 11:05:37 -0400 Subject: [PATCH] 8016: Fix some error checking in ThrottledLogger. --- services/crunch-run/logging.go | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/services/crunch-run/logging.go b/services/crunch-run/logging.go index 9b19bf0754..db9d101b58 100644 --- a/services/crunch-run/logging.go +++ b/services/crunch-run/logging.go @@ -46,8 +46,9 @@ func RFC3339Timestamp(t time.Time) string { return t.Format(RFC3339NanoFixed) } -// Write to the internal buffer. Prepend a timestamp to each line of the input -// data. +// Write prepends a timestamp to each line of the input data and +// appends to the internal buffer. Each line is also logged to +// tl.Immediate, if tl.Immediate is not nil. func (tl *ThrottledLogger) Write(p []byte) (n int, err error) { tl.Mutex.Lock() if tl.buf == nil { @@ -57,13 +58,20 @@ func (tl *ThrottledLogger) Write(p []byte) (n int, err error) { now := tl.Timestamper(time.Now().UTC()) sc := bufio.NewScanner(bytes.NewBuffer(p)) - for sc.Scan() { - _, err = fmt.Fprintf(tl.buf, "%s %s\n", now, sc.Text()) + for err == nil && sc.Scan() { + out := fmt.Sprintf("%s %s\n", now, sc.Bytes()) if tl.Immediate != nil { - tl.Immediate.Printf("%s %s\n", now, sc.Text()) + tl.Immediate.Print(out[:len(out)-1]) } + _, err = io.WriteString(tl.buf, out) } - return len(p), err + if err == nil { + err = sc.Err() + if err == nil { + n = len(p) + } + } + return } // Periodically check the current buffer; if not empty, send it on the -- 2.30.2