servicelog

package
v1.12.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 10, 2024 License: GPL-3.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrRange = errors.New("out of range")
)

Functions

func LastLines

func LastLines(logBuffer *RingBuffer, n int, indent string, stripPrefix bool) (string, error)

LastLines fetches the last n lines of output and, if stripPrefix is true, strips the timestamp and service name prefix from each line. If there are more than n lines, the result is prefixed with a "(...)" line.

func NewFormatWriter

func NewFormatWriter(dest io.Writer, serviceName string) io.Writer

NewFormatWriter returns a io.Writer that inserts timestamp and service name for every line in the stream. For the input:

first\n
second\n
third\n

The expected output is:

2021-05-13T03:16:51.001Z [test] first\n
2021-05-13T03:16:52.002Z [test] second\n
2021-05-13T03:16:53.003Z [test] third\n

Types

type Entry

type Entry struct {
	Time    time.Time
	Service string
	Message string
}

Entry is a parsed log entry.

func Parse

func Parse(line []byte) (Entry, error)

Parse parses a log entry of the form "2021-05-20T15:39:12.345Z [service] log message".

type Iterator

type Iterator interface {
	// Close removes this iterator from the ring buffer. After calling Close,
	// any future calls to Next will return false.
	Close() error

	// Next returns true if there is more data to read. If a non-nil cancel
	// channel is passed in, Next will wait for more data to become available.
	// Sending on this channel, or closing it, will cause Next to return
	// immediately.
	// If the ring buffer writer produces data faster than the iterator can read
	// it, the iterator will eventually be truncated and restarted. The
	// truncation will be identified in the iterator output with the text
	// specified when the iterator was created.
	Next(cancel <-chan struct{}) bool

	// Notify sets the notification channel. When more data is available, the
	// channel passed in to Notify will have true sent on it. If the channel is
	// not receiving (unbuffered) or full (buffered), the notification will be
	// dropped.
	Notify(ch chan bool)

	// Buffered returns the approximate number of bytes available to read.
	Buffered() int

	io.Reader
	io.WriterTo
}

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

Parser parses and iterates over logs from a Reader until EOF (or another error occurs).

func NewParser

func NewParser(r io.Reader, size int) *Parser

NewParser creates a Parser with the given buffer size.

func (*Parser) Entry

func (p *Parser) Entry() Entry

Entry returns the current log entry (should only be called after Next returns true).

func (*Parser) Err

func (p *Parser) Err() error

Err returns the last error that occurred (EOF is not considered an error).

func (*Parser) Next

func (p *Parser) Next() bool

Next parses the next log from the reader and reports whether another log is available (false is returned on EOF or other read error).

type RingBuffer

type RingBuffer struct {
	// contains filtered or unexported fields
}

RingBuffer is a io.Writer that uses a single byte buffer to store data written to it until Release is called on the range no-longer required. RingBuffer is effectively a linear allocator with sequential frees that must be done in the same order as the allocations.

func NewRingBuffer

func NewRingBuffer(size int) *RingBuffer

NewRingBuffer creates a RingBuffer with the provided size in bytes for the backing buffer.

func (*RingBuffer) Available

func (rb *RingBuffer) Available() int

Available returns the number of bytes available to allocate.

func (*RingBuffer) Buffered

func (rb *RingBuffer) Buffered() int

Buffered returns the number of bytes readable from the buffer.

func (*RingBuffer) Close

func (rb *RingBuffer) Close() error

Close closes the writer to further writes, readers may continue.

func (*RingBuffer) Closed

func (rb *RingBuffer) Closed() bool

Closed returns true if the writing side has closed.

func (*RingBuffer) Copy

func (rb *RingBuffer) Copy(dest []byte, start RingPos) (next RingPos, n int, err error)

Copy copies bytes into dest upto the length of dest, starting at the supplied start position in the RingBuffer. If start is outside of the range that is buffered, ErrRange is returned.

func (*RingBuffer) Discard

func (rb *RingBuffer) Discard(n int) error

Discard disposes of n bytes from the tail of the buffer making them available to be used for subsequent writes.

func (*RingBuffer) HeadIterator

func (rb *RingBuffer) HeadIterator(lines int) Iterator

HeadIterator returns an iterator from the head of the buffer. If lines is greater than zero, the iterator will start that many lines backwards from the head.

func (*RingBuffer) Positions

func (rb *RingBuffer) Positions() (start RingPos, end RingPos)

Positions returns the start and end positions of readable data in the RingBuffer.

func (*RingBuffer) Size

func (rb *RingBuffer) Size() int

Size returns the size in bytes of the internal buffer.

func (*RingBuffer) TailIterator

func (rb *RingBuffer) TailIterator() Iterator

TailIterator returns an iterator from the tail of the buffer.

func (*RingBuffer) Write

func (rb *RingBuffer) Write(p []byte) (written int, err error)

Write writes p to the backing buffer, allocating the number of bytes in p. If p is larger than the size of the buffer then io.ErrShortWrite is returned and the number of bytes written. If the p is larger than the number of bytes available, then the tail is discarded to make room.

func (*RingBuffer) WriteTo

func (rb *RingBuffer) WriteTo(writer io.Writer, start RingPos) (next RingPos, n int64, err error)

WriteTo writes the selected range to a io.Writer.

type RingPos

type RingPos int64
const (
	// TailPosition is a special position that represents the tail at read time.
	TailPosition RingPos = -1
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL