update change log, gofmt and remove debug prints
This commit is contained in:
parent
644891ebbc
commit
976fc15b81
|
@ -1,6 +1,8 @@
|
||||||
# May, 2013
|
# May, 2013
|
||||||
|
|
||||||
* Recognize deletions/renames when using polling file watcher (PR #1)
|
* Recognize deletions/renames when using polling file watcher (PR #1)
|
||||||
|
* Detect file truncation
|
||||||
|
* Fix potential race condition when reopening the file (issue 5)
|
||||||
|
|
||||||
# Feb, 2013
|
# Feb, 2013
|
||||||
|
|
||||||
|
|
12
tail.go
12
tail.go
|
@ -13,8 +13,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Line struct {
|
type Line struct {
|
||||||
Text string
|
Text string
|
||||||
Time time.Time
|
Time time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tail configuration
|
// Tail configuration
|
||||||
|
@ -156,7 +156,7 @@ func (tail *Tail) tailFileSync() {
|
||||||
for _, line := range partitionString(string(line), tail.MaxLineSize) {
|
for _, line := range partitionString(string(line), tail.MaxLineSize) {
|
||||||
tail.Lines <- &Line{line, now}
|
tail.Lines <- &Line{line, now}
|
||||||
}
|
}
|
||||||
}else{
|
} else {
|
||||||
tail.Lines <- &Line{string(line), now}
|
tail.Lines <- &Line{string(line), now}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -186,7 +186,6 @@ func (tail *Tail) tailFileSync() {
|
||||||
if !ok {
|
if !ok {
|
||||||
changes = nil // XXX: how to kill changes' goroutine?
|
changes = nil // XXX: how to kill changes' goroutine?
|
||||||
|
|
||||||
log.Println("Changes channel is closed.")
|
|
||||||
// File got deleted/renamed/truncated.
|
// File got deleted/renamed/truncated.
|
||||||
if tail.ReOpen {
|
if tail.ReOpen {
|
||||||
// TODO: no logging in a library?
|
// TODO: no logging in a library?
|
||||||
|
@ -199,7 +198,7 @@ func (tail *Tail) tailFileSync() {
|
||||||
}
|
}
|
||||||
log.Printf("Successfully reopened %s", tail.Filename)
|
log.Printf("Successfully reopened %s", tail.Filename)
|
||||||
tail.reader = bufio.NewReader(tail.file)
|
tail.reader = bufio.NewReader(tail.file)
|
||||||
|
|
||||||
continue
|
continue
|
||||||
} else {
|
} else {
|
||||||
log.Printf("Finishing because file has been moved/deleted: %s", tail.Filename)
|
log.Printf("Finishing because file has been moved/deleted: %s", tail.Filename)
|
||||||
|
@ -208,7 +207,6 @@ func (tail *Tail) tailFileSync() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case <-tail.Dying():
|
case <-tail.Dying():
|
||||||
log.Println("Dying..")
|
|
||||||
tail.close()
|
tail.close()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -231,7 +229,7 @@ func partitionString(s string, chunkSize int) []string {
|
||||||
panic("invalid chunkSize")
|
panic("invalid chunkSize")
|
||||||
}
|
}
|
||||||
length := len(s)
|
length := len(s)
|
||||||
chunks := 1 + length/chunkSize
|
chunks := 1 + length/chunkSize
|
||||||
start := 0
|
start := 0
|
||||||
end := chunkSize
|
end := chunkSize
|
||||||
parts := make([]string, 0, chunks)
|
parts := make([]string, 0, chunks)
|
||||||
|
|
14
tail_test.go
14
tail_test.go
|
@ -85,7 +85,7 @@ func _TestReOpen(_t *testing.T, poll bool) {
|
||||||
var name string
|
var name string
|
||||||
if poll {
|
if poll {
|
||||||
name = "reopen-polling"
|
name = "reopen-polling"
|
||||||
}else {
|
} else {
|
||||||
name = "reopen-inotify"
|
name = "reopen-inotify"
|
||||||
}
|
}
|
||||||
t := NewTailTest(name, _t)
|
t := NewTailTest(name, _t)
|
||||||
|
@ -93,7 +93,7 @@ func _TestReOpen(_t *testing.T, poll bool) {
|
||||||
tail := t.StartTail(
|
tail := t.StartTail(
|
||||||
"test.txt",
|
"test.txt",
|
||||||
Config{Follow: true, ReOpen: true, Poll: poll, Location: -1})
|
Config{Follow: true, ReOpen: true, Poll: poll, Location: -1})
|
||||||
|
|
||||||
go t.VerifyTailOutput(tail, []string{"hello", "world", "more", "data", "endofworld"})
|
go t.VerifyTailOutput(tail, []string{"hello", "world", "more", "data", "endofworld"})
|
||||||
|
|
||||||
// deletion must trigger reopen
|
// deletion must trigger reopen
|
||||||
|
@ -138,12 +138,11 @@ func TestReOpenPolling(_t *testing.T) {
|
||||||
_TestReOpen(_t, true)
|
_TestReOpen(_t, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func _TestReSeek(_t *testing.T, poll bool) {
|
func _TestReSeek(_t *testing.T, poll bool) {
|
||||||
var name string
|
var name string
|
||||||
if poll {
|
if poll {
|
||||||
name = "reseek-polling"
|
name = "reseek-polling"
|
||||||
}else {
|
} else {
|
||||||
name = "reseek-inotify"
|
name = "reseek-inotify"
|
||||||
}
|
}
|
||||||
t := NewTailTest(name, _t)
|
t := NewTailTest(name, _t)
|
||||||
|
@ -151,7 +150,7 @@ func _TestReSeek(_t *testing.T, poll bool) {
|
||||||
tail := t.StartTail(
|
tail := t.StartTail(
|
||||||
"test.txt",
|
"test.txt",
|
||||||
Config{Follow: true, ReOpen: true, Poll: poll, Location: -1})
|
Config{Follow: true, ReOpen: true, Poll: poll, Location: -1})
|
||||||
|
|
||||||
go t.VerifyTailOutput(tail, []string{
|
go t.VerifyTailOutput(tail, []string{
|
||||||
"a really long string goes here", "hello", "world", "h311o", "w0r1d", "endofworld"})
|
"a really long string goes here", "hello", "world", "h311o", "w0r1d", "endofworld"})
|
||||||
|
|
||||||
|
@ -176,7 +175,7 @@ func _TestReSeek(_t *testing.T, poll bool) {
|
||||||
<-time.After(100 * time.Millisecond)
|
<-time.After(100 * time.Millisecond)
|
||||||
t.RemoveFile("test.txt")
|
t.RemoveFile("test.txt")
|
||||||
|
|
||||||
println("Stopping...")
|
println("Stopping (RESEEK)...")
|
||||||
tail.Stop()
|
tail.Stop()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -191,7 +190,6 @@ func TestReSeekPolling(_t *testing.T) {
|
||||||
_TestReSeek(_t, true)
|
_TestReSeek(_t, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Test library
|
// Test library
|
||||||
|
|
||||||
type TailTest struct {
|
type TailTest struct {
|
||||||
|
@ -275,7 +273,7 @@ func (t TailTest) VerifyTailOutput(tail *Tail, lines []string) {
|
||||||
err := tail.Wait()
|
err := tail.Wait()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal("tail ended early with error: %v", err)
|
t.Fatal("tail ended early with error: %v", err)
|
||||||
}else{
|
} else {
|
||||||
t.Fatalf("tail ended early; expecting more: %v", lines[idx:])
|
t.Fatalf("tail ended early; expecting more: %v", lines[idx:])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
17
watch.go
17
watch.go
|
@ -6,9 +6,8 @@ import (
|
||||||
"github.com/howeyc/fsnotify"
|
"github.com/howeyc/fsnotify"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"time"
|
|
||||||
"sync"
|
"sync"
|
||||||
"fmt"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// FileWatcher monitors file-level events.
|
// FileWatcher monitors file-level events.
|
||||||
|
@ -34,7 +33,6 @@ func NewInotifyFileWatcher(filename string) *InotifyFileWatcher {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fw *InotifyFileWatcher) BlockUntilExists() error {
|
func (fw *InotifyFileWatcher) BlockUntilExists() error {
|
||||||
fmt.Println("BUE(inotify): creating watcher")
|
|
||||||
w, err := fsnotify.NewWatcher()
|
w, err := fsnotify.NewWatcher()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -50,7 +48,6 @@ func (fw *InotifyFileWatcher) BlockUntilExists() error {
|
||||||
}
|
}
|
||||||
defer w.RemoveWatch(filepath.Dir(fw.Filename))
|
defer w.RemoveWatch(filepath.Dir(fw.Filename))
|
||||||
|
|
||||||
fmt.Println("BUE(inotify): does file exist now?")
|
|
||||||
// Do a real check now as the file might have been created before
|
// Do a real check now as the file might have been created before
|
||||||
// calling `WatchFlags` above.
|
// calling `WatchFlags` above.
|
||||||
if _, err = os.Stat(fw.Filename); !os.IsNotExist(err) {
|
if _, err = os.Stat(fw.Filename); !os.IsNotExist(err) {
|
||||||
|
@ -58,10 +55,8 @@ func (fw *InotifyFileWatcher) BlockUntilExists() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("BUE(inotify): checking events (last: %v)\n", err)
|
|
||||||
for {
|
for {
|
||||||
evt := <-w.Event
|
evt := <-w.Event
|
||||||
fmt.Printf("BUE(inotify): got event: %v\n", evt)
|
|
||||||
if evt.Name == fw.Filename {
|
if evt.Name == fw.Filename {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
@ -93,7 +88,6 @@ func (fw *InotifyFileWatcher) ChangeEvents(fi os.FileInfo) chan bool {
|
||||||
prevSize := fw.Size
|
prevSize := fw.Size
|
||||||
|
|
||||||
evt := <-w.Event
|
evt := <-w.Event
|
||||||
fmt.Printf("inotify change evt: %v\n", evt)
|
|
||||||
switch {
|
switch {
|
||||||
case evt.IsDelete():
|
case evt.IsDelete():
|
||||||
fallthrough
|
fallthrough
|
||||||
|
@ -109,7 +103,6 @@ func (fw *InotifyFileWatcher) ChangeEvents(fi os.FileInfo) chan bool {
|
||||||
}
|
}
|
||||||
fw.Size = fi.Size()
|
fw.Size = fi.Size()
|
||||||
|
|
||||||
fmt.Printf("WATCH(inotify): prevSize=%d; fs.Size=%d\n", prevSize, fw.Size)
|
|
||||||
if prevSize > 0 && prevSize > fw.Size {
|
if prevSize > 0 && prevSize > fw.Size {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -143,11 +136,10 @@ func (fw *PollingFileWatcher) BlockUntilExists() error {
|
||||||
for {
|
for {
|
||||||
if _, err := os.Stat(fw.Filename); err == nil {
|
if _, err := os.Stat(fw.Filename); err == nil {
|
||||||
return nil
|
return nil
|
||||||
}else if !os.IsNotExist(err) {
|
} else if !os.IsNotExist(err) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
time.Sleep(POLL_DURATION)
|
time.Sleep(POLL_DURATION)
|
||||||
println("blocking..")
|
|
||||||
}
|
}
|
||||||
panic("unreachable")
|
panic("unreachable")
|
||||||
}
|
}
|
||||||
|
@ -169,7 +161,7 @@ func (fw *PollingFileWatcher) ChangeEvents(origFi os.FileInfo) chan bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
fw.Size = origFi.Size()
|
fw.Size = origFi.Size()
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
prevSize := fw.Size
|
prevSize := fw.Size
|
||||||
for {
|
for {
|
||||||
|
@ -198,7 +190,6 @@ func (fw *PollingFileWatcher) ChangeEvents(origFi os.FileInfo) chan bool {
|
||||||
|
|
||||||
// Was the file truncated?
|
// Was the file truncated?
|
||||||
fw.Size = fi.Size()
|
fw.Size = fi.Size()
|
||||||
fmt.Printf("WATCH(poll): prevSize=%d; fs.Size=%d\n", prevSize, fw.Size)
|
|
||||||
if prevSize > 0 && prevSize > fw.Size {
|
if prevSize > 0 && prevSize > fw.Size {
|
||||||
once.Do(stopAndClose)
|
once.Do(stopAndClose)
|
||||||
continue
|
continue
|
||||||
|
@ -212,8 +203,6 @@ func (fw *PollingFileWatcher) ChangeEvents(origFi os.FileInfo) chan bool {
|
||||||
case ch <- true:
|
case ch <- true:
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
}else{
|
|
||||||
fmt.Printf("polling; not modified: %v == %v\n", modTime, prevModTime)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
Loading…
Reference in New Issue