debug

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	LevelBasic    = 1 // -v: Basic info (file counts, phase transitions)
	LevelDetailed = 2 // -vv: Detailed (AST node counts, processing steps)
	LevelTrace    = 3 // -vvv: Full data dumps of IR structures
)

Verbosity levels

Variables

This section is empty.

Functions

func Lazy

func Lazy(ctx context.Context, level int, fn func(Debugger))

Lazy executes a logging function only if the debug level is enabled This avoids expensive string formatting when debugging is disabled Usage: debug.Lazy(ctx, LevelTrace, func(d Debugger) { d.Logf(LevelTrace, "data: %+v", expensiveData()) })

func NewContext

func NewContext(ctx context.Context, debugger Debugger) context.Context

NewContext returns a new context that carries the provided Debugger

func WithTiming

func WithTiming[T any](ctx context.Context, level int, operation string, fn func() T) T

WithTiming is a helper that executes a function and logs its duration Usage: result := debug.WithTiming(ctx, LevelDetailed, "parsing", func() T { ... })

Types

type Debugger

type Debugger interface {
	// Logf conditionally prints a formatted message if the verbosity is high enough
	Logf(level int, format string, args ...any)

	// Dump conditionally prints a detailed view of a data structure
	// Typically used with LevelTrace
	Dump(level int, label string, data any)

	// IsEnabledFor checks if a given verbosity level is active
	// This is a performance optimization to avoid expensive argument preparation
	IsEnabledFor(level int) bool

	// WithSubsystem returns a new Debugger instance scoped to a specific part of the pipeline
	// e.g., "parser", "formatter", "stripper"
	WithSubsystem(name string) Debugger

	// Timing starts a timer and returns a function to stop it and log the duration
	// Usage: defer dbg.Timing(LevelDetailed, "parsing")()
	Timing(level int, operation string) func()

	// SetFormat sets the output format (text or json)
	SetFormat(format string)
}

Debugger defines the interface for our debugging system

func FromContext

func FromContext(ctx context.Context) Debugger

FromContext retrieves the Debugger from the context If no Debugger is found, it returns a no-op debugger that does nothing This makes usage safe and eliminates nil checks everywhere

func New

func New(w io.Writer, verbosity int) Debugger

New creates a new debugger with a given verbosity level

func Silent

func Silent() Debugger

Silent returns a no-op debugger that does nothing

Jump to

Keyboard shortcuts

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