diff --git a/tail.go b/tail.go index c99cdaa2..48b2e4f3 100644 --- a/tail.go +++ b/tail.go @@ -224,8 +224,10 @@ func (tail *Tail) readLine() (string, error) { } func (tail *Tail) tailFileSync() { - defer tail.Done() - defer tail.close() + defer func() { + tail.close() + tail.Done() + }() if !tail.MustExist { // deferred first open. @@ -250,16 +252,12 @@ func (tail *Tail) tailFileSync() { tail.openReader() - var offset int64 - var err error - // Read line by line. for { // do not seek in named pipes if !tail.Pipe { // grab the position in case we need to back up in the event of a half-line - offset, err = tail.Tell() - if err != nil { + if _, err := tail.Tell(); err != nil { tail.Kill(err) return } @@ -295,10 +293,8 @@ func (tail *Tail) tailFileSync() { } if tail.Follow && line != "" { - // this has the potential to never return the last line if - // it's not followed by a newline; seems a fair trade here - err := tail.seekTo(SeekInfo{Offset: offset, Whence: 0}) - if err != nil { + tail.sendLine(line) + if err := tail.seekEnd(); err != nil { tail.Kill(err) return } diff --git a/tail_test.go b/tail_test.go index 38d6b84b..5226c944 100644 --- a/tail_test.go +++ b/tail_test.go @@ -114,7 +114,7 @@ func TestStopAtEOF(t *testing.T) { func TestMaxLineSizeFollow(t *testing.T) { // As last file line does not end with newline, it will not be present in tail's output - maxLineSize(t, true, "hello\nworld\nfin\nhe", []string{"hel", "lo", "wor", "ld", "fin"}) + maxLineSize(t, true, "hello\nworld\nfin\nhe", []string{"hel", "lo", "wor", "ld", "fin", "he"}) } func TestMaxLineSizeNoFollow(t *testing.T) {