Merge pull request #14 from QLeelulu/master

add Tell to return the file's current position, like stdio's ftell().
This commit is contained in:
Sridhar Ratnakumar 2013-08-24 14:01:07 -07:00
commit b87764baaf
2 changed files with 48 additions and 0 deletions

15
tail.go
View File

@ -91,6 +91,21 @@ func TailFile(filename string, config Config) (*Tail, error) {
return t, nil return t, nil
} }
// Return the file's current position, like stdio's ftell().
// But this value is not very accurate.
// it may readed one line in the chan(tail.Lines),
// so it may lost one line.
func (tail *Tail) Tell() (offset int64, err error) {
if tail.file == nil {
return
}
offset, err = tail.file.Seek(0, os.SEEK_CUR)
if err == nil {
offset -= int64(tail.reader.Buffered())
}
return
}
// Stop stops the tailing activity. // Stop stops the tailing activity.
func (tail *Tail) Stop() error { func (tail *Tail) Stop() error {
tail.Kill(nil) tail.Kill(nil)

View File

@ -225,6 +225,39 @@ func TestRateLimiting(_t *testing.T) {
tail.Stop() tail.Stop()
} }
func TestTell(_t *testing.T) {
t := NewTailTest("tell-position", _t)
t.CreateFile("test.txt", "hello\nworld\nagain\nmore\n")
config := Config{
Follow: false,
Location: &SeekInfo{0, os.SEEK_SET}}
tail := t.StartTail("test.txt", config)
// read noe line
<-tail.Lines
offset, err := tail.Tell()
if err != nil {
t.Errorf("Tell return error: %s", err.Error())
}
tail.Done()
// tail.close()
config = Config{
Follow: false,
Location: &SeekInfo{offset, os.SEEK_SET}}
tail = t.StartTail("test.txt", config)
for l := range tail.Lines {
// it may readed one line in the chan(tail.Lines),
// so it may lost one line.
if l.Text != "world" && l.Text != "again" {
t.Fatalf("mismatch; expected world or again, but got %s",
l.Text)
}
break
}
t.RemoveFile("test.txt")
tail.Done()
}
// Test library // Test library
type TailTest struct { type TailTest struct {