allow seeking from beginning and end

This commit is contained in:
Sridhar Ratnakumar 2013-08-09 14:42:51 -07:00
parent 1e2bc8afe8
commit 8d9c6e4ce1
1 changed files with 13 additions and 4 deletions

15
tail.go
View File

@ -26,7 +26,9 @@ type Line struct {
// Config is used to specify how a file must be tailed. // Config is used to specify how a file must be tailed.
type Config struct { type Config struct {
Location int64 // Tail from last N lines (tail -n) Location int64 // For positive location, tail after first N
// lines; for negative, tail from last N lines;
// for zero, tail from end. If -1, do not seek.
Follow bool // Continue looking for new lines (tail -f) Follow bool // Continue looking for new lines (tail -f)
ReOpen bool // Reopen recreated files (tail -F) ReOpen bool // Reopen recreated files (tail -F)
MustExist bool // Fail early if the file does not exist MustExist bool // Fail early if the file does not exist
@ -139,9 +141,16 @@ func (tail *Tail) tailFileSync() {
} }
// Seek to requested location on first open of the file. // Seek to requested location on first open of the file.
if tail.Location >= 0 { if tail.Location != -1 {
var err error
switch {
case tail.Location <= 0:
// Seek relative to file end // Seek relative to file end
_, err := tail.file.Seek(tail.Location, 2) _, err = tail.file.Seek(tail.Location, os.SEEK_END)
default:
// Seek from file beginning
_, err = tail.file.Seek(tail.Location, os.SEEK_SET)
}
if err != nil { if err != nil {
tail.Killf("Seek error on %s: %s", tail.Filename, err) tail.Killf("Seek error on %s: %s", tail.Filename, err)
return return