log: logging an empty string should still print a line
Print("") was printing a header but no line.
Fixes #9665.
Change-Id: Iac783187786065e1389ad6e8d7ef02c579ed7bd8
Reviewed-on: https://go-review.googlesource.com/8665
Reviewed-by: Ian Lance Taylor <iant@golang.org>
diff --git a/src/log/log.go b/src/log/log.go
index 17646a1..9b68008 100644
--- a/src/log/log.go
+++ b/src/log/log.go
@@ -156,7 +156,7 @@
l.buf = l.buf[:0]
l.formatHeader(&l.buf, now, file, line)
l.buf = append(l.buf, s...)
- if len(s) > 0 && s[len(s)-1] != '\n' {
+ if len(s) == 0 || s[len(s)-1] != '\n' {
l.buf = append(l.buf, '\n')
}
_, err := l.out.Write(l.buf)
diff --git a/src/log/log_test.go b/src/log/log_test.go
index 14e0b29..d7d2490 100644
--- a/src/log/log_test.go
+++ b/src/log/log_test.go
@@ -10,6 +10,7 @@
"bytes"
"os"
"regexp"
+ "strings"
"testing"
)
@@ -17,7 +18,7 @@
Rdate = `[0-9][0-9][0-9][0-9]/[0-9][0-9]/[0-9][0-9]`
Rtime = `[0-9][0-9]:[0-9][0-9]:[0-9][0-9]`
Rmicroseconds = `\.[0-9][0-9][0-9][0-9][0-9][0-9]`
- Rline = `(54|56):` // must update if the calls to l.Printf / l.Print below move
+ Rline = `(55|57):` // must update if the calls to l.Printf / l.Print below move
Rlongfile = `.*/[A-Za-z0-9_\-]+\.go:` + Rline
Rshortfile = `[A-Za-z0-9_\-]+\.go:` + Rline
)
@@ -118,6 +119,20 @@
}
}
+func TestEmptyPrintCreatesLine(t *testing.T) {
+ var b bytes.Buffer
+ l := New(&b, "Header:", LstdFlags)
+ l.Print()
+ l.Println("non-empty")
+ output := b.String()
+ if n := strings.Count(output, "Header"); n != 2 {
+ t.Errorf("expected 2 headers, got %d", n)
+ }
+ if n := strings.Count(output, "\n"); n != 2 {
+ t.Errorf("expected 2 lines, got %d", n)
+ }
+}
+
func BenchmarkItoa(b *testing.B) {
dst := make([]byte, 0, 64)
for i := 0; i < b.N; i++ {