From 87d772a7d708fec12f48cd8adc0dedff6e1025da Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Mon, 26 Aug 2024 10:15:20 +0200 Subject: Adding upstream version 1.47.0. Signed-off-by: Daniel Baumann --- src/go/collectors/go.d.plugin/pkg/logs/lastline.go | 65 ---------------------- 1 file changed, 65 deletions(-) delete mode 100644 src/go/collectors/go.d.plugin/pkg/logs/lastline.go (limited to 'src/go/collectors/go.d.plugin/pkg/logs/lastline.go') diff --git a/src/go/collectors/go.d.plugin/pkg/logs/lastline.go b/src/go/collectors/go.d.plugin/pkg/logs/lastline.go deleted file mode 100644 index 911dbf497..000000000 --- a/src/go/collectors/go.d.plugin/pkg/logs/lastline.go +++ /dev/null @@ -1,65 +0,0 @@ -// SPDX-License-Identifier: GPL-3.0-or-later - -package logs - -import ( - "errors" - "os" - - "github.com/clbanning/rfile/v2" -) - -const DefaultMaxLineWidth = 4 * 1024 // assume disk block size is 4K - -var ErrTooLongLine = errors.New("too long line") - -// ReadLastLine returns the last line of the file and any read error encountered. -// It expects last line width <= maxLineWidth. -// If maxLineWidth <= 0, it defaults to DefaultMaxLineWidth. -func ReadLastLine(filename string, maxLineWidth int64) ([]byte, error) { - if maxLineWidth <= 0 { - maxLineWidth = DefaultMaxLineWidth - } - f, err := os.Open(filename) - if err != nil { - return nil, err - } - defer func() { _ = f.Close() }() - - stat, _ := f.Stat() - endPos := stat.Size() - if endPos == 0 { - return []byte{}, nil - } - startPos := endPos - maxLineWidth - if startPos < 0 { - startPos = 0 - } - buf := make([]byte, endPos-startPos) - n, err := f.ReadAt(buf, startPos) - if err != nil { - return nil, err - } - lnPos := 0 - foundLn := false - for i := n - 2; i >= 0; i-- { - ch := buf[i] - if ch == '\n' { - foundLn = true - lnPos = i - break - } - } - if foundLn { - return buf[lnPos+1 : n], nil - } - if startPos == 0 { - return buf[0:n], nil - } - - return nil, ErrTooLongLine -} - -func ReadLastLines(filename string, n uint) ([]string, error) { - return rfile.Tail(filename, int(n)) -} -- cgit v1.2.3