logstream

package
v1.13.1 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2026 License: GPL-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package logstream provides a thread-safe log broadcasting system with a ring buffer for log history and SSE-based streaming to subscribers.

Index

Constants

View Source
const (
	// DefaultBufferSize is the default number of log lines to keep in the ring buffer.
	DefaultBufferSize = 1000
	// DefaultSubscriberBuffer is the buffer size for each subscriber's channel.
	DefaultSubscriberBuffer = 100
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Hub

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

Hub manages log broadcasting to subscribers with a ring buffer for history.

func NewHub

func NewHub(size int) *Hub

NewHub creates a new Hub with the specified buffer size. If size <= 0, DefaultBufferSize is used.

func (*Hub) Count

func (h *Hub) Count() int

Count returns the number of lines currently in the buffer.

func (*Hub) History

func (h *Hub) History(n int) []string

History returns the last n lines from the ring buffer. If n <= 0 or n > count, returns all available lines.

func (*Hub) Subscribe

func (h *Hub) Subscribe(ctx context.Context) *Subscriber

Subscribe creates a new subscriber that receives log lines. The returned Subscriber should be unsubscribed when done.

func (*Hub) SubscriberCount

func (h *Hub) SubscriberCount() int

SubscriberCount returns the current number of subscribers.

func (*Hub) Unsubscribe

func (h *Hub) Unsubscribe(sub *Subscriber)

Unsubscribe removes a subscriber and closes its channel.

func (*Hub) Write

func (h *Hub) Write(line string)

Write appends a log line to the ring buffer and broadcasts to subscribers. Lines that exceed subscriber buffer capacity are dropped (slow consumer protection).

type Subscriber

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

Subscriber represents a log stream subscriber with a buffered channel.

func (*Subscriber) Channel

func (s *Subscriber) Channel() <-chan string

Channel returns the subscriber's log line channel.

func (*Subscriber) Done

func (s *Subscriber) Done() <-chan struct{}

Done returns the subscriber's context done channel.

type SwitchableWriter

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

SwitchableWriter is an io.Writer that allows atomic swapping of the underlying writer. It also captures complete log lines and broadcasts them to a Hub.

func NewSwitchableWriter

func NewSwitchableWriter(initial io.Writer, hub *Hub) *SwitchableWriter

NewSwitchableWriter creates a new SwitchableWriter with the given initial writer and hub.

func (*SwitchableWriter) GetHub

func (sw *SwitchableWriter) GetHub() *Hub

GetHub returns the Hub associated with this writer.

func (*SwitchableWriter) Swap

func (sw *SwitchableWriter) Swap(newWriter io.Writer, newCloser io.Closer) io.Closer

Swap atomically replaces the underlying writer and returns the old closer (if any). The caller is responsible for closing the returned closer after the swap.

func (*SwitchableWriter) Write

func (sw *SwitchableWriter) Write(p []byte) (n int, err error)

Write writes data to the underlying writer and captures complete lines for the hub. Note: We don't wrap the error here since SwitchableWriter implements io.Writer and callers expect standard Write semantics.

Jump to

Keyboard shortcuts

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