From fdb82882980dff79acdf46b8303edf1e4e619dad Mon Sep 17 00:00:00 2001 From: Rodrigo Chiossi Date: Wed, 17 Oct 2018 19:02:56 +0000 Subject: [PATCH] builder: Handle write error on Stopwatch buffer Use the logger interface to write stopwatch output. With this change, any error that occurs while trying to write the output is handled internally by the log module. This patch also fix a bug where the line break was printed to STDOUT instead of the stopwatch buffer. Signed-off-by: Rodrigo Chiossi --- builder/stopwatch.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/builder/stopwatch.go b/builder/stopwatch.go index c04ea97..a4835a9 100644 --- a/builder/stopwatch.go +++ b/builder/stopwatch.go @@ -1,8 +1,8 @@ package builder import ( - "fmt" "io" + "log" "time" ) @@ -21,11 +21,12 @@ type stopWatchEntry struct { } func (sw *stopWatch) Start(name string) { + logger := log.New(sw.w, "", log.Ldate|log.Ltime) if sw.w != nil { if len(sw.entries) > 0 { - fmt.Println() + logger.Println(sw.w, "") } - fmt.Fprintf(sw.w, "=> %s\n", name) + logger.Printf("=> %s\n", name) } sw.entries = append(sw.entries, stopWatchEntry{name: name}) sw.t = time.Now() @@ -46,6 +47,7 @@ func (sw *stopWatch) Stop() { } func (sw *stopWatch) WriteSummary(w io.Writer) { + logger := log.New(w, "", log.Ldate|log.Ltime) if len(sw.entries) == 0 { return } @@ -56,10 +58,10 @@ func (sw *stopWatch) WriteSummary(w io.Writer) { } } var sum time.Duration - fmt.Fprintf(w, "\nTIMINGS\n") + logger.Printf("\nTIMINGS\n") for _, e := range sw.entries { - fmt.Fprintf(w, " %-*s %s\n", max, e.name, e.d.Truncate(time.Millisecond)) + logger.Printf(" %-*s %s\n", max, e.name, e.d.Truncate(time.Millisecond)) sum += e.d } - fmt.Fprintf(w, "TOTAL: %s\n", sum.Truncate(time.Millisecond)) + logger.Printf("TOTAL: %s\n", sum.Truncate(time.Millisecond)) }