summaryrefslogtreecommitdiff
path: root/internal/io/line/line.go
blob: 69cc6671fc6b394058460b906a32230e30ddd543 (plain)
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
package line

import (
	"bytes"
	"fmt"
	"sync"
)

// lineBuffer is there to optimize memory allocations. DTail otherwise allocates
// a lot of memory while reading logs.
var lineBuffer = sync.Pool{
	New: func() interface{} {
		return &Line{}
	},
}

// Line represents a read log line.
type Line struct {
	// The content of the log line.
	Content *bytes.Buffer
	// Until now, how many log lines were processed?
	Count uint64
	// Sometimes we produce too many log lines so that the client
	// is too slow to process all of them. The server will drop log
	// lines if that happens but it will signal to the client how
	// many log lines in % could be transmitted to the client.
	TransmittedPerc int
	// Contains the unique identifier of the source log file.
	// It could be the name of the log or it could be one of the parent
	// directories in case multiple log files with the same basename are
	// followed.
	SourceID string
	// Session generation this line belongs to. Zero means unscoped/legacy output.
	Generation uint64
}

// New creaters a new line object. This is a DTail internal helper structure for reading files.
func New(content *bytes.Buffer, count uint64, transmittedPerc int, sourceID string) *Line {
	l := lineBuffer.Get().(*Line)
	l.Content = content
	l.Count = count
	l.TransmittedPerc = transmittedPerc
	l.SourceID = sourceID
	l.Generation = 0
	return l
}

// Return a human readable representation of the followed line.
func (l Line) String() string {
	return fmt.Sprintf("Line(Content:%s,TransmittedPerc:%v,Count:%v,SourceID:%s)",
		l.Content.String(),
		l.TransmittedPerc,
		l.Count,
		l.SourceID)
}

// Recycle the line. Once done, don't reuse this instance!!!
func (l *Line) Recycle() {
	// No explicit reset required, as NewLine overrides all elements
	// already takes care of it.
	//l.Reset()
	lineBuffer.Put(l)
}