lib/logger: Strip control characters from log output (fixes #5428) (#5434)

This commit is contained in:
Jakob Borg
2019-01-05 11:31:02 +01:00
committed by GitHub
parent ad30192dca
commit 5503175854
2 changed files with 62 additions and 1 deletions

View File

@@ -70,7 +70,7 @@ type logger struct {
var DefaultLogger = New()
func New() Logger {
return newLogger(os.Stdout)
return newLogger(controlStripper{os.Stdout})
}
func newLogger(w io.Writer) Logger {
@@ -376,3 +376,23 @@ func (r *recorder) append(l LogLevel, msg string) {
r.lines = append(r.lines, Line{time.Now(), "...", l})
}
}
// controlStripper is a Writer that replaces control characters
// with spaces.
type controlStripper struct {
io.Writer
}
func (s controlStripper) Write(data []byte) (int, error) {
for i, b := range data {
if b == '\n' || b == '\r' {
// Newlines are OK
continue
}
if b < 32 {
// Characters below 32 are control characters
data[i] = ' '
}
}
return s.Writer.Write(data)
}