Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solved the problem for never return the last line if it's not followed by a newline #126

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions tail.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion tail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down