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

N option: Start from Nth last line #46

Open
wants to merge 1 commit 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
7 changes: 5 additions & 2 deletions cmd/gotail/gotail.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@ package main
import (
"flag"
"fmt"
"io"
"os"
"github.com/nxadm/tail"
"io"
"os"
)

func args2config() (tail.Config, int64) {
config := tail.Config{Follow: true}
N := uint64(0)
n := int64(0)
maxlinesize := int(0)
flag.Int64Var(&n, "n", 0, "tail from the last Nth location")
flag.Uint64Var(&N, "N", 0, "tail from the last Nth line")
flag.IntVar(&maxlinesize, "max", 0, "max line size")
flag.BoolVar(&config.Follow, "f", false, "wait for additional data to be appended to the file")
flag.BoolVar(&config.ReOpen, "F", false, "follow, and track file rename/rotation")
Expand All @@ -25,6 +27,7 @@ func args2config() (tail.Config, int64) {
if config.ReOpen {
config.Follow = true
}
config.N = N
config.MaxLineSize = maxlinesize
return config, n
}
Expand Down
49 changes: 49 additions & 0 deletions tail.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ type logger interface {
// Config is used to specify how a file must be tailed.
type Config struct {
// File-specifc
N uint64 // Start from n last lines (tail -n)
Location *SeekInfo // Tail from this location. If nil, start at the beginning of the file
ReOpen bool // Reopen recreated files (tail -F)
MustExist bool // Fail early if the file does not exist
Expand Down Expand Up @@ -282,6 +283,54 @@ func (tail *Tail) tailFileSync() {
}
}

if tail.Location == nil && tail.N > 0 {
var lines uint64 = 0
var cursor int64 = 0
stat, err := tail.file.Stat()
if err == nil {
filesize := stat.Size()
for {
if filesize == 0 {
break
}
cursor--
if cursor == -1 {
_, err = tail.file.Seek(-1, io.SeekEnd)
} else {
_, err = tail.file.Seek(-2, io.SeekCurrent)
}
if err != nil {
tail.Killf("Seek error on %s: %s", tail.Filename, err)
return
}

char := make([]byte, 1)
_, err = tail.file.Read(char)
if err != nil {
tail.Killf("Seek error on %s: %s", tail.Filename, err)
return
}

if cursor != -1 && (char[0] == 10 || char[0] == 13) { // line found
lines++

if lines == tail.N {
cursor++
break
}
}

if cursor == -filesize {
break
}
}

if cursor < 0 {
tail.Location = &SeekInfo{Offset: cursor, Whence: io.SeekEnd}
}
}
}

// Seek to requested location on first open of the file.
if tail.Location != nil {
_, err := tail.file.Seek(tail.Location.Offset, tail.Location.Whence)
Expand Down
72 changes: 72 additions & 0 deletions tail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,78 @@ func TestLocationMiddle(t *testing.T) {
tailTest.Cleanup(tail, true)
}

func TestNLastLinesEmptyFile(t *testing.T) {
// Test reading from middle.
tailTest, cleanup := NewTailTest("n-last-lines-empty-file", t)
defer cleanup()
tailTest.CreateFile("test.txt", "")
tail := tailTest.StartTail("test.txt", Config{Follow: true, N: 5})
go tailTest.VerifyTailOutput(tail, []string{"1 line"}, false)

<-time.After(100 * time.Millisecond)
tailTest.AppendFile("test.txt", "1 line\n")

// Delete after a reasonable delay, to give tail sufficient time
// to read all lines.
<-time.After(100 * time.Millisecond)
tailTest.RemoveFile("test.txt")
tailTest.Cleanup(tail, true)
}

func TestNLastLinesOneEmpty(t *testing.T) {
// Test reading from middle.
tailTest, cleanup := NewTailTest("n-last-lines-one-empty", t)
defer cleanup()
tailTest.CreateFile("test.txt", "\n")
tail := tailTest.StartTail("test.txt", Config{Follow: true, N: 5})
go tailTest.VerifyTailOutput(tail, []string{"", "1 line"}, false)

<-time.After(100 * time.Millisecond)
tailTest.AppendFile("test.txt", "1 line\n")

// Delete after a reasonable delay, to give tail sufficient time
// to read all lines.
<-time.After(100 * time.Millisecond)
tailTest.RemoveFile("test.txt")
tailTest.Cleanup(tail, true)
}

func TestNLastLines(t *testing.T) {
// Test reading from middle.
tailTest, cleanup := NewTailTest("n-last-lines", t)
defer cleanup()
tailTest.CreateFile("test.txt", "1 line\n2 line\n3 line\n")
tail := tailTest.StartTail("test.txt", Config{Follow: true, N: 5})
go tailTest.VerifyTailOutput(tail, []string{"1 line", "2 line", "3 line", "4 line"}, false)

<-time.After(100 * time.Millisecond)
tailTest.AppendFile("test.txt", "4 line\n")

// Delete after a reasonable delay, to give tail sufficient time
// to read all lines.
<-time.After(100 * time.Millisecond)
tailTest.RemoveFile("test.txt")
tailTest.Cleanup(tail, true)
}

func TestN5LastLines(t *testing.T) {
// Test reading from middle.
tailTest, cleanup := NewTailTest("n5-last-lines", t)
defer cleanup()
tailTest.CreateFile("test.txt", "1 line\n2 line\n3 line\n4 line\n5 line\n6 line\n")
tail := tailTest.StartTail("test.txt", Config{Follow: true, N: 5})
go tailTest.VerifyTailOutput(tail, []string{"2 line", "3 line", "4 line", "5 line", "6 line", "7 line"}, false)

<-time.After(100 * time.Millisecond)
tailTest.AppendFile("test.txt", "7 line\n")

// Delete after a reasonable delay, to give tail sufficient time
// to read all lines.
<-time.After(100 * time.Millisecond)
tailTest.RemoveFile("test.txt")
tailTest.Cleanup(tail, true)
}

// The use of polling file watcher could affect file rotation
// (detected via renames), so test these explicitly.

Expand Down