allow seeking from beginning and end
This commit is contained in:
parent
1e2bc8afe8
commit
8d9c6e4ce1
17
tail.go
17
tail.go
|
@ -26,7 +26,9 @@ type Line struct {
|
|||
|
||||
// Config is used to specify how a file must be tailed.
|
||||
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)
|
||||
ReOpen bool // Reopen recreated files (tail -F)
|
||||
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.
|
||||
if tail.Location >= 0 {
|
||||
// Seek relative to file end
|
||||
_, err := tail.file.Seek(tail.Location, 2)
|
||||
if tail.Location != -1 {
|
||||
var err error
|
||||
switch {
|
||||
case tail.Location <= 0:
|
||||
// Seek relative to file end
|
||||
_, 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 {
|
||||
tail.Killf("Seek error on %s: %s", tail.Filename, err)
|
||||
return
|
||||
|
|
Loading…
Reference in New Issue