diff options
Diffstat (limited to 'internal/tlog')
| -rw-r--r-- | internal/tlog/log.go | 21 | ||||
| -rw-r--r-- | internal/tlog/tlog_test.go | 26 | 
2 files changed, 43 insertions, 4 deletions
| diff --git a/internal/tlog/log.go b/internal/tlog/log.go index 9277abd..9ebe535 100644 --- a/internal/tlog/log.go +++ b/internal/tlog/log.go @@ -57,22 +57,35 @@ type toggledLogger struct {  	Logger *log.Logger  } +// trimNewline removes one trailing newline from "msg" +func trimNewline(msg string) string { +	if len(msg) == 0 { +		return msg +	} +	if msg[len(msg)-1] == '\n' { +		return msg[:len(msg)-1] +	} +	return msg +} +  func (l *toggledLogger) Printf(format string, v ...interface{}) {  	if !l.Enabled {  		return  	} -	l.Logger.Printf(l.prefix + fmt.Sprintf(format, v...) + l.postfix) +	msg := trimNewline(fmt.Sprintf(format, v...)) +	l.Logger.Printf(l.prefix + msg + l.postfix)  	if l.Wpanic { -		l.Logger.Panic(wpanicMsg + fmt.Sprintf(format, v...)) +		l.Logger.Panic(wpanicMsg + msg)  	}  }  func (l *toggledLogger) Println(v ...interface{}) {  	if !l.Enabled {  		return  	} -	l.Logger.Println(l.prefix + fmt.Sprint(v...) + l.postfix) +	msg := trimNewline(fmt.Sprint(v...)) +	l.Logger.Println(l.prefix + msg + l.postfix)  	if l.Wpanic { -		l.Logger.Panic(wpanicMsg + fmt.Sprint(v...)) +		l.Logger.Panic(wpanicMsg + msg)  	}  } diff --git a/internal/tlog/tlog_test.go b/internal/tlog/tlog_test.go new file mode 100644 index 0000000..2e1c034 --- /dev/null +++ b/internal/tlog/tlog_test.go @@ -0,0 +1,26 @@ +package tlog + +import ( +	"testing" +) + +// Test that trimNewline() works as expected +func TestTrimNewline(t *testing.T) { +	testTable := []struct { +		in   string +		want string +	}{ +		{"...\n", "..."}, +		{"\n...\n", "\n..."}, +		{"", ""}, +		{"\n", ""}, +		{"\n\n", "\n"}, +		{"   ", "   "}, +	} +	for _, v := range testTable { +		have := trimNewline(v.in) +		if v.want != have { +			t.Errorf("want=%q have=%q", v.want, have) +		} +	} +} | 
