/
tailer_nix.go
64 lines (54 loc) · 1.53 KB
/
tailer_nix.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//go:build !no_logs && !windows
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
package file
import (
"fmt"
"io"
"log"
"path/filepath"
coreconfig "flashcat.cloud/categraf/config"
"flashcat.cloud/categraf/logs/decoder"
)
// setup sets up the file tailer
func (t *Tailer) setup(offset int64, whence int) error {
fullpath, err := filepath.Abs(t.file.Path)
if err != nil {
return err
}
t.fullpath = fullpath
// adds metadata to enable users to filter logs by filename
t.tags = t.buildTailerTags()
if coreconfig.Config.DebugMode {
log.Println("I! Opening", t.file.Path, "for tailer key", t.file.GetScanKey())
}
f, err := openFile(fullpath)
if err != nil {
return err
}
t.osFile = f
ret, _ := f.Seek(offset, whence)
t.readOffset = ret
t.decodedOffset = ret
return nil
}
// read lets the tailer tail the content of a file
// until it is closed or the tailer is stopped.
func (t *Tailer) read() (int, error) {
// keep reading data from file
inBuf := make([]byte, 4096)
n, err := t.osFile.Read(inBuf)
if err != nil && err != io.EOF {
// an unexpected error occurred, stop the tailor
t.file.Source.Status.Error(err)
return 0, fmt.Errorf("E! Unexpected error occurred while reading file: %v", err)
}
if n == 0 {
return 0, nil
}
t.decoder.InputChan <- decoder.NewInput(inBuf[:n])
t.incrementReadOffset(n)
return n, nil
}