Skip to content

Commit

Permalink
fix: Handle \r\n windows line endings.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaqx0r committed Jul 17, 2024
1 parent ee521cd commit 11e9e03
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
17 changes: 15 additions & 2 deletions internal/tailer/logstream/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,25 @@ func (lr *LineReader) send(ctx context.Context) bool {
if i < 0 {
return false
}
// TODO: Skip a previous \r
end := lr.off + i // excluding delim
skip := 1 // len of delim char

// Most file-based log sources will end with \n on Unixlike systems. On
// Windows they appear to be both \r\n. syslog disallows \r (and \t and
// others) and writes them escaped, per syslog(7). [RFC
// 3164](https://www.ietf.org/rfc/rfc3164.txt) disallows newlines in the
// message: "The MSG part of the syslog packet MUST contain visible
// (printing) characters." Thus if the previous char was a \r then ignore
// it as well.
if end > 0 && lr.buf[end-1] == '\r' {
end -= 1
skip = 2
}

line := string(lr.buf[lr.off:end])
logLines.Add(lr.sourcename, 1)
lr.lines <- logline.New(ctx, lr.sourcename, line)
lr.off = end + 1 // set past delim
lr.off = end + skip // move past delim
return true
}

Expand Down
5 changes: 5 additions & 0 deletions internal/tailer/logstream/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ func TestReadAndSend(t *testing.T) {
input: []byte("line 1\nline 2\n"),
wantOut: []string{"line 1", "line 2"},
},
{
name: "windows line ending",
input: []byte("line 1\r\nline \r2\n"),
wantOut: []string{"line 1", "line \r2"},
},
} {
t.Run(tc.name, func(t *testing.T) {
lines := make(chan *logline.LogLine, 5)
Expand Down

0 comments on commit 11e9e03

Please sign in to comment.