]> Cypherpunks repositories - gostls13.git/commitdiff
log: logging an empty string should still print a line
authorRob Pike <r@golang.org>
Wed, 8 Apr 2015 21:55:45 +0000 (14:55 -0700)
committerRob Pike <r@golang.org>
Thu, 9 Apr 2015 17:02:27 +0000 (17:02 +0000)
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>
src/log/log.go
src/log/log_test.go

index 17646a12fa8872877e5c282ace6c1d01c981fc42..9b6800891c7d5f8afb1c672bf28426283541dab7 100644 (file)
@@ -156,7 +156,7 @@ func (l *Logger) Output(calldepth int, s string) error {
        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)
index 14e0b2926340564a1ed3964d96cfa65ef9807fa6..d7d24900629800890c379daad68c4e9cf59b5d88 100644 (file)
@@ -10,6 +10,7 @@ import (
        "bytes"
        "os"
        "regexp"
+       "strings"
        "testing"
 )
 
@@ -17,7 +18,7 @@ const (
        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 TestFlagAndPrefixSetting(t *testing.T) {
        }
 }
 
+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++ {