stacktrace

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package stacktrace provides utilities for capturing, representing, and formatting stack traces.

The package allows you to capture the current call stack, inspect stack frames, and render stack traces as human-readable strings.

Example:

stack := stacktrace.Capture(0, 10)
fmt.Println(stack)

This captures up to 10 frames of the current stack and prints them in a readable format.

Index

Constants

View Source
const DefaultDepth = 64

DefaultDepth is the default maximum number of stack frames to capture.

Variables

This section is empty.

Functions

func WriteFrameToBuffer

func WriteFrameToBuffer(f runtime.Frame, b *strings.Builder)

WriteFrameToBuffer writes a string representation of the given runtime.Frame to the provided strings.Builder.

The format is:

FunctionName
\tFilePath:LineNumber

Types

type Stack

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

Stack represents a captured stack trace as a sequence of frames.

Use the Capture function to obtain a Stack, and the String method to render it.

Example:

stack := stacktrace.Capture(0, 10)
fmt.Println(stack)

func Capture

func Capture(skip, depth int) *Stack

Capture captures the current stack trace, skipping the specified number of frames and limiting the depth of the trace. If depth is math.MaxInt, it captures the full trace.

skip controls how many stack frames to skip (0 means start from the caller of Capture). depth limits the number of frames captured; use math.MaxInt for no limit.

Returns a *Stack containing the captured frames.

func NewStack

func NewStack(initialSize int) *Stack

NewStack returns a new Stack with preallocated space for the given initial size.

func (*Stack) AddCaller

func (s *Stack) AddCaller()

AddCaller adds the current caller's frame to the stack.

func (*Stack) AddFrame

func (s *Stack) AddFrame(f runtime.Frame)

AddFrame appends the given runtime.Frame to the stack.

func (*Stack) FramesIter

func (s *Stack) FramesIter() iter.Seq2[int, runtime.Frame]

FramesIter returns an iterator over the frames in the stack.

The iterator yields the frames starting from the most recent (the one added last).

func (*Stack) Len

func (s *Stack) Len() int

Len returns the number of frames contained in the stack.

func (*Stack) String

func (s *Stack) String() string

String returns a multi-line string representation of the stack trace. Each frame is shown with its function name and file:line.

Jump to

Keyboard shortcuts

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