Documentation
¶
Overview ¶
Package utils provides common utilities for devloop.
This package contains shared functionality used across the devloop ecosystem: - Logging infrastructure with colored prefix support - Color management for terminal output - Log management and streaming capabilities - Prefix writers for enhanced output formatting
Logging ¶
The package provides structured logging with prefix support:
logger := utils.NewDevloopLogger(true, 10, colorManager)
logger.LogWithPrefix("build", "Starting build process")
// Output: [build ] Starting build process
Color Management ¶
Color management provides consistent terminal coloring:
colorManager := utils.NewColorManager(true)
coloredText := colorManager.FormatPrefix("[build]", rule)
Log Streaming ¶
Log streaming enables real-time log access:
logManager := utils.NewLogManager(logsDir)
err := logManager.StreamLogs("ruleName", "filter", stream)
Prefix Writers ¶
Enhanced output writers with prefix and color support:
writer := utils.NewColoredPrefixWriter(writers, "[prefix] ", colorManager, rule)
writer.Write([]byte("Hello World"))
// Output: [prefix] Hello World (with colors)
Index ¶
- func InitGlobalLogger(prefixLogs bool, prefixMaxLength int, colorManager ColorFormatter)
- func LogDevloop(format string, args ...any)
- func LogGateway(format string, args ...any)
- func LogMCP(format string, args ...any)
- func StripColorCodes(input string) string
- type ColorFormatter
- type ColorManager
- func (cm *ColorManager) DisableColors()
- func (cm *ColorManager) FormatPrefix(prefix string, rule interface{}) string
- func (cm *ColorManager) GetColorForRule(rule ColorRule) *color.Color
- func (cm *ColorManager) GetColoredString(text string, rule ColorRule) string
- func (cm *ColorManager) IsEnabled() bool
- type ColorRule
- type ColorSettings
- type ColoredPrefixWriter
- type DevloopLogger
- type FileLogSource
- type LogBroadcaster
- func (b *LogBroadcaster) BroadcastEvent(event *pb.LogEvent)
- func (b *LogBroadcaster) GetHistory(n int) []string
- func (b *LogBroadcaster) IsFinished() bool
- func (b *LogBroadcaster) SignalFinished()
- func (b *LogBroadcaster) SignalNewRun(appendMode bool)
- func (b *LogBroadcaster) Stop()
- func (b *LogBroadcaster) Subscribe(id, filter string) *Subscriber
- func (b *LogBroadcaster) Unsubscribe(id string)
- type LogManager
- func (lm *LogManager) Close() error
- func (lm *LogManager) GetWriter(ruleName string, appendOnRestart bool) (io.Writer, error)
- func (lm *LogManager) SignalFinished(ruleName string, success bool, errMsg string)
- func (lm *LogManager) StreamLogs(ruleName, filter string, timeoutSeconds int64, lastNLines int32, ...) error
- type LogSource
- type LoggerConfig
- type Nameable
- type RingBuffer
- type SimpleColorRule
- type Subscriber
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func InitGlobalLogger ¶ added in v0.0.30
func InitGlobalLogger(prefixLogs bool, prefixMaxLength int, colorManager ColorFormatter)
InitGlobalLogger initializes the global logger with configuration
func LogDevloop ¶ added in v0.0.30
LogDevloop logs a devloop message using the global logger
func LogGateway ¶ added in v0.0.30
LogGateway logs a gateway message using the global logger
func StripColorCodes ¶ added in v0.0.30
StripColorCodes removes ANSI color codes from a string using regex
Types ¶
type ColorFormatter ¶ added in v0.0.30
ColorFormatter interface for formatting colored prefixes
type ColorManager ¶ added in v0.0.30
type ColorManager struct {
// contains filtered or unexported fields
}
ColorManager handles color assignment and formatting for rule output
func NewColorManager ¶ added in v0.0.30
func NewColorManager(settings ColorSettings) *ColorManager
NewColorManager creates a new color manager with the given settings
func (*ColorManager) DisableColors ¶ added in v0.0.30
func (cm *ColorManager) DisableColors()
DisableColors explicitly disables color output for this ColorManager only
func (*ColorManager) FormatPrefix ¶ added in v0.0.30
func (cm *ColorManager) FormatPrefix(prefix string, rule interface{}) string
FormatPrefix applies color formatting to a prefix string
func (*ColorManager) GetColorForRule ¶ added in v0.0.30
func (cm *ColorManager) GetColorForRule(rule ColorRule) *color.Color
GetColorForRule returns the appropriate color for a given rule
func (*ColorManager) GetColoredString ¶ added in v0.0.30
func (cm *ColorManager) GetColoredString(text string, rule ColorRule) string
GetColoredString returns a colored version of the input string for the given rule
func (*ColorManager) IsEnabled ¶ added in v0.0.30
func (cm *ColorManager) IsEnabled() bool
IsEnabled returns whether color formatting is enabled
type ColorSettings ¶ added in v0.0.30
type ColorSettings interface {
GetColorLogs() bool
GetColorScheme() string
GetCustomColors() map[string]string
}
ColorSettings represents the color configuration needed by ColorManager
type ColoredPrefixWriter ¶ added in v0.0.30
type ColoredPrefixWriter struct {
// contains filtered or unexported fields
}
ColoredPrefixWriter is an enhanced io.Writer that adds colored prefixes to each line of output It separates terminal output (with colors) from file output (without colors)
func NewColoredPrefixWriter ¶ added in v0.0.30
func NewColoredPrefixWriter(writers []io.Writer, prefix string, colorManager *ColorManager, rule Nameable) *ColoredPrefixWriter
NewColoredPrefixWriter creates a new ColoredPrefixWriter
func (*ColoredPrefixWriter) UpdatePrefix ¶ added in v0.0.30
func (cpw *ColoredPrefixWriter) UpdatePrefix(newPrefix string)
UpdatePrefix updates the prefix and regenerates the colored version
func (*ColoredPrefixWriter) Write ¶ added in v0.0.30
func (cpw *ColoredPrefixWriter) Write(p []byte) (n int, err error)
Write implements the io.Writer interface with color support
func (*ColoredPrefixWriter) WriteToFileOnly ¶ added in v0.0.30
func (cpw *ColoredPrefixWriter) WriteToFileOnly(p []byte) (n int, err error)
WriteToFileOnly writes output only to file writers (without colors)
func (*ColoredPrefixWriter) WriteToTerminalOnly ¶ added in v0.0.30
func (cpw *ColoredPrefixWriter) WriteToTerminalOnly(p []byte) (n int, err error)
WriteToTerminalOnly writes output only to terminal writers (with colors)
type DevloopLogger ¶ added in v0.0.30
type DevloopLogger struct {
// contains filtered or unexported fields
}
DevloopLogger provides consistent logging across all components
func NewDevloopLogger ¶ added in v0.0.30
func NewDevloopLogger(prefixLogs bool, prefixMaxLength int, colorManager ColorFormatter) *DevloopLogger
NewDevloopLogger creates a new logger with the given configuration
func (*DevloopLogger) LogWithPrefix ¶ added in v0.0.30
func (dl *DevloopLogger) LogWithPrefix(prefix, format string, args ...any)
LogWithPrefix logs a message with the specified prefix (e.g., "devloop", "gateway", "mcp")
type FileLogSource ¶ added in v0.1.0
type FileLogSource struct {
// contains filtered or unexported fields
}
FileLogSource reads new lines from a log file, detecting truncation.
func NewFileLogSource ¶ added in v0.1.0
func NewFileLogSource(path string) *FileLogSource
NewFileLogSource creates a FileLogSource for the given path. The file is opened lazily on the first ReadLines call.
func (*FileLogSource) Close ¶ added in v0.1.0
func (fs *FileLogSource) Close() error
Close releases the file handle.
func (*FileLogSource) ReadLines ¶ added in v0.1.0
func (fs *FileLogSource) ReadLines(ctx context.Context) ([]string, error)
ReadLines returns new complete lines since the last call. Returns nil, nil if the file doesn't exist yet or has no new content.
func (*FileLogSource) Reset ¶ added in v0.1.0
func (fs *FileLogSource) Reset() error
Reset re-opens the file from the beginning.
type LogBroadcaster ¶ added in v0.1.0
type LogBroadcaster struct {
// contains filtered or unexported fields
}
LogBroadcaster reads new lines from a LogSource once and fans out to all subscribers. One broadcaster per rule eliminates redundant file polling.
func NewLogBroadcaster ¶ added in v0.1.0
func NewLogBroadcaster(ruleName string, source LogSource) *LogBroadcaster
NewLogBroadcaster creates and starts a broadcaster for the given rule.
func (*LogBroadcaster) BroadcastEvent ¶ added in v0.1.0
func (b *LogBroadcaster) BroadcastEvent(event *pb.LogEvent)
BroadcastEvent sends a LogEvent to all subscribers (non-blocking).
func (*LogBroadcaster) GetHistory ¶ added in v0.1.0
func (b *LogBroadcaster) GetHistory(n int) []string
GetHistory returns the last n lines from the ring buffer.
func (*LogBroadcaster) IsFinished ¶ added in v0.1.0
func (b *LogBroadcaster) IsFinished() bool
IsFinished returns whether the rule has finished.
func (*LogBroadcaster) SignalFinished ¶ added in v0.1.0
func (b *LogBroadcaster) SignalFinished()
SignalFinished marks the rule as finished. The poller will do a final drain and then close all subscriber Done channels.
func (*LogBroadcaster) SignalNewRun ¶ added in v0.1.0
func (b *LogBroadcaster) SignalNewRun(appendMode bool)
SignalNewRun prepares the broadcaster for a new rule run. In truncate mode (appendMode=false): clears the ring buffer, resets the source to offset 0, and clears the finished flag. In append mode (appendMode=true): only clears the finished flag so the source continues from its current offset (picking up the separator + new output) and the ring buffer preserves history across runs.
func (*LogBroadcaster) Stop ¶ added in v0.1.0
func (b *LogBroadcaster) Stop()
Stop cancels the poller goroutine, closes all subscriber Done channels, and closes the source.
func (*LogBroadcaster) Subscribe ¶ added in v0.1.0
func (b *LogBroadcaster) Subscribe(id, filter string) *Subscriber
Subscribe adds a subscriber that will receive new log lines. Returns the subscriber. The caller should select on sub.Ch and sub.Done.
func (*LogBroadcaster) Unsubscribe ¶ added in v0.1.0
func (b *LogBroadcaster) Unsubscribe(id string)
Unsubscribe removes a subscriber by ID.
type LogManager ¶ added in v0.0.30
type LogManager struct {
// contains filtered or unexported fields
}
LogManager manages log files and provides streaming capabilities. It uses a single LogBroadcaster per rule to fan out log lines to all subscribers instead of each client polling independently.
func NewLogManager ¶ added in v0.0.30
func NewLogManager(logDir string) (*LogManager, error)
NewLogManager creates a new LogManager instance.
func (*LogManager) Close ¶ added in v0.0.30
func (lm *LogManager) Close() error
Close closes the LogManager and stops all broadcasters.
func (*LogManager) GetWriter ¶ added in v0.0.30
GetWriter returns an io.Writer for a specific rule's log file. When appendOnRestart is true, the log file is opened in append mode and a separator line is written to mark the start of a new run. When false (default), the file is truncated on each run.
func (*LogManager) SignalFinished ¶ added in v0.0.30
func (lm *LogManager) SignalFinished(ruleName string, success bool, errMsg string)
SignalFinished signals that a rule's execution has finished. success indicates whether the run completed successfully. errMsg is an optional error message when success is false.
func (*LogManager) StreamLogs ¶ added in v0.0.30
func (lm *LogManager) StreamLogs(ruleName, filter string, timeoutSeconds int64, lastNLines int32, writer *gocurrent.Writer[*pb.StreamLogsResponse]) error
StreamLogs streams logs for a given rule to the provided gocurrent Writer. lastNLines controls history replay: 0 = no history, negative = all available.
type LogSource ¶ added in v0.1.0
type LogSource interface {
// ReadLines returns new lines since the last call. Returns nil, nil if no new content or file doesn't exist yet.
ReadLines(ctx context.Context) ([]string, error)
// Reset seeks back to the beginning (e.g. after file truncation).
Reset() error
// Close releases the underlying resource.
Close() error
}
LogSource provides an interface for reading new log lines incrementally.
type LoggerConfig ¶ added in v0.0.30
type LoggerConfig struct {
PrefixLogs bool
PrefixMaxLength int
ColorManager ColorFormatter
}
LoggerConfig holds configuration for consistent logging
type Nameable ¶ added in v0.0.30
type Nameable interface {
GetName() string
}
Nameable represents anything that has a name (used for color generation)
type RingBuffer ¶ added in v0.1.0
type RingBuffer struct {
// contains filtered or unexported fields
}
RingBuffer is a thread-safe circular buffer of strings. We use a simple slice-based approach instead of container/ring because:
- container/ring is a linked list without fixed-capacity overwrite semantics
- No built-in "read last N" — would require O(n) linked-list traversal
- Per-element heap allocations vs a single contiguous slice here
- Uses any/interface{} — loses type safety
TODO: If we need a generic version, consider making this RingBuffer[T any].
func NewRingBuffer ¶ added in v0.1.0
func NewRingBuffer(capacity int) *RingBuffer
NewRingBuffer creates a new RingBuffer with the given capacity.
func (*RingBuffer) Clear ¶ added in v0.1.0
func (rb *RingBuffer) Clear()
Clear resets the buffer to empty.
func (*RingBuffer) Len ¶ added in v0.1.0
func (rb *RingBuffer) Len() int
Len returns the number of lines currently in the buffer.
func (*RingBuffer) ReadLast ¶ added in v0.1.0
func (rb *RingBuffer) ReadLast(n int) []string
ReadLast returns the last n lines from the buffer. If n < 0, returns all available lines. If n > available, returns all available lines.
func (*RingBuffer) Write ¶ added in v0.1.0
func (rb *RingBuffer) Write(lines []string)
Write appends lines to the ring buffer, overwriting oldest entries if full.
type SimpleColorRule ¶ added in v0.0.30
type SimpleColorRule struct {
// contains filtered or unexported fields
}
SimpleColorRule is a basic implementation of ColorRule for simple cases
func (*SimpleColorRule) GetColor ¶ added in v0.0.30
func (r *SimpleColorRule) GetColor() string
func (*SimpleColorRule) GetName ¶ added in v0.0.30
func (r *SimpleColorRule) GetName() string
func (*SimpleColorRule) GetPrefix ¶ added in v0.0.30
func (r *SimpleColorRule) GetPrefix() string
type Subscriber ¶ added in v0.1.0
type Subscriber struct {
ID string
Filter string
Ch chan []*pb.LogLine // buffered, batched delivery
Events chan *pb.LogEvent // buffered, event delivery
Done chan struct{} // closed when broadcaster stops or rule finishes
}
Subscriber represents a client subscribed to log broadcasts.