Skip to content

Commit 0ac1242

Browse files
authored
Merge pull request #108 from jwilder/jw-tail
Fix 100% cpu util when tailing file
2 parents 8ed4b77 + 4c86ed7 commit 0ac1242

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

tail.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,31 @@ func tailFile(ctx context.Context, file string, poll bool, dest *os.File) {
1818
Logger: tail.DiscardingLogger,
1919
})
2020
if err != nil {
21-
log.Fatalf("unable to tail %s: %s", "foo", err)
21+
log.Fatalf("unable to tail %s: %s", file, err)
2222
}
2323

24+
defer func() {
25+
t.Stop()
26+
t.Cleanup()
27+
}()
28+
2429
// main loop
2530
for {
2631
select {
2732
// if the channel is done, then exit the loop
2833
case <-ctx.Done():
29-
t.Stop()
30-
t.Cleanup()
3134
return
3235
// get the next log line and echo it out
3336
case line := <-t.Lines:
34-
if line != nil {
35-
fmt.Fprintln(dest, line.Text)
37+
if line == nil {
38+
if t.Err() != nil {
39+
log.Fatalf("unable to tail %s: %s", file, t.Err())
40+
}
41+
return
42+
} else if line.Err != nil {
43+
log.Fatalf("unable to tail %s: %s", file, t.Err())
3644
}
45+
fmt.Fprintln(dest, line.Text)
3746
}
3847
}
3948
}

0 commit comments

Comments
 (0)