Skip to content

Commit

Permalink
Add flag to set initial reading position
Browse files Browse the repository at this point in the history
  • Loading branch information
masa23 committed Jun 8, 2018
1 parent 110f070 commit f0c2d2b
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions tail.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ import (

// Tail is tail file struct
type Tail struct {
file string
fileFd *os.File
posFile string
posFd *os.File
Stat Stat
data chan []byte
file string
fileFd *os.File
posFile string
posFd *os.File
Stat Stat
data chan []byte
InitialReadPositionEnd bool // If true, there is no pos file Start reading from the end of the file
}

// Stat tail stats infomation struct
Expand All @@ -30,6 +31,7 @@ type Stat struct {
// Open file and position files.
func Open(file string, posfile string) (*Tail, error) {
var err error
var isCreatePosFile bool
t := Tail{file: file, posFile: posfile}

// open position file
Expand All @@ -41,6 +43,7 @@ func Open(file string, posfile string) (*Tail, error) {
if err != nil {
return &t, err
}
isCreatePosFile = true
}
posdata, err := ioutil.ReadAll(t.posFd)
if err != nil {
Expand Down Expand Up @@ -80,7 +83,11 @@ func Open(file string, posfile string) (*Tail, error) {
}

// tail seek posititon.
t.fileFd.Seek(t.Stat.Offset, os.SEEK_SET)
if isCreatePosFile {
t.fileFd.Seek(0, os.SEEK_END)
} else {
t.fileFd.Seek(t.Stat.Offset, os.SEEK_SET)
}

return &t, nil
}
Expand Down

0 comments on commit f0c2d2b

Please sign in to comment.