-
Notifications
You must be signed in to change notification settings - Fork 0
/
tailer.go
49 lines (42 loc) · 1.33 KB
/
tailer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package reader
import (
"context"
"io"
"log"
"os"
"time"
"github.com/mkmik/tail"
v1 "google.golang.org/grpc/binarylog/grpc_binarylog_v1"
)
// ReadFileInto reads log entries from filename and writes to channel res, optionally "follwing" the
// file for new appended data.
func ReadFileInto(ctx context.Context, filename string, res chan *v1.GrpcLogEntry, follow bool) error {
r, err := openFile(ctx, filename, follow)
if err != nil {
return err
}
return ReadInto(ctx, r, res)
}
func openFile(ctx context.Context, filename string, follow bool) (io.Reader, error) {
if follow {
t := tail.Follow(ctx, tail.LoggerFunc(log.Printf), filename,
tail.Whence(io.SeekStart),
tail.PollTimeout(time.Minute*10))
return t, nil
} else {
return os.Open(filename)
}
}
// ReadFile returns a channel of log entries and a channel containing the possible error.
// The entries channel is closed when an error is returned or when the input is fully consumed if follow is false.
// If follow is true the channel will only be closed when the context is canceled.
func ReadFile(ctx context.Context, filename string, follow bool) (chan *v1.GrpcLogEntry, chan error) {
res := make(chan *v1.GrpcLogEntry)
errCh := make(chan error, 1)
go func() {
errCh <- ReadFileInto(ctx, filename, res, follow)
close(res)
close(errCh)
}()
return res, errCh
}