support arbitrary values for `Location`
This commit is contained in:
parent
6af3d03d43
commit
1e2bc8afe8
|
@ -11,7 +11,7 @@ import (
|
||||||
|
|
||||||
func args2config() tail.Config {
|
func args2config() tail.Config {
|
||||||
config := tail.Config{Follow: true}
|
config := tail.Config{Follow: true}
|
||||||
flag.IntVar(&config.Location, "n", 0, "tail from the last Nth location")
|
flag.Int64Var(&config.Location, "n", 0, "tail from the last Nth location")
|
||||||
flag.BoolVar(&config.Follow, "f", false, "wait for additional data to be appended to the file")
|
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")
|
flag.BoolVar(&config.ReOpen, "F", false, "follow, and track file rename/rotation")
|
||||||
flag.BoolVar(&config.Poll, "p", false, "use polling, instead of inotify")
|
flag.BoolVar(&config.Poll, "p", false, "use polling, instead of inotify")
|
||||||
|
|
11
tail.go
11
tail.go
|
@ -26,7 +26,7 @@ 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 int // Tail from last N lines (tail -n)
|
Location int64 // Tail from last N lines (tail -n)
|
||||||
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
|
||||||
|
@ -55,10 +55,6 @@ type Tail struct {
|
||||||
// invoke the `Wait` or `Err` method after finishing reading from the
|
// invoke the `Wait` or `Err` method after finishing reading from the
|
||||||
// `Lines` channel.
|
// `Lines` channel.
|
||||||
func TailFile(filename string, config Config) (*Tail, error) {
|
func TailFile(filename string, config Config) (*Tail, error) {
|
||||||
if !(config.Location == 0 || config.Location == -1) {
|
|
||||||
panic("only 0/-1 values are supported for Location.")
|
|
||||||
}
|
|
||||||
|
|
||||||
if config.ReOpen && !config.Follow {
|
if config.ReOpen && !config.Follow {
|
||||||
panic("cannot set ReOpen without Follow.")
|
panic("cannot set ReOpen without Follow.")
|
||||||
}
|
}
|
||||||
|
@ -143,8 +139,9 @@ 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 >= 0 {
|
||||||
_, err := tail.file.Seek(0, 2) // Seek to the file end
|
// Seek relative to file end
|
||||||
|
_, err := tail.file.Seek(tail.Location, 2)
|
||||||
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
|
||||||
|
|
Loading…
Reference in New Issue