Skip to content

Commit 67f1519

Browse files
committed
[stacktrace] improve documentation
Change-Id: I6875e670085ed54fd1c78d7d23a90467ee9b0b6c
1 parent 4466ec3 commit 67f1519

2 files changed

Lines changed: 39 additions & 8 deletions

File tree

stacktrace/stack.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,26 @@ import (
77
"strings"
88
)
99

10+
// Stack represents a captured stack trace as a sequence of frames.
11+
//
12+
// Use the Capture function to obtain a Stack, and the String method to render it.
13+
//
14+
// Example:
15+
//
16+
// stack := stacktrace.Capture(0, 10)
17+
// fmt.Println(stack)
1018
type Stack struct {
11-
frames []runtime.Frame `exhaustruct:"optional"`
19+
frames []runtime.Frame
1220
}
1321

22+
// NewStack returns a new Stack with preallocated space for the given initial size.
1423
func NewStack(initialSize int) *Stack {
1524
return &Stack{
1625
frames: make([]runtime.Frame, 0, initialSize),
1726
}
1827
}
1928

20-
// AddCaller adds the caller's frame to the stack.
29+
// AddCaller adds the current caller's frame to the stack.
2130
func (s *Stack) AddCaller() {
2231
pcs := make([]uintptr, 1)
2332
runtime.Callers(3, pcs) //nolint:mnd
@@ -27,18 +36,19 @@ func (s *Stack) AddCaller() {
2736
s.AddFrame(frame)
2837
}
2938

39+
// AddFrame appends the given runtime.Frame to the stack.
3040
func (s *Stack) AddFrame(f runtime.Frame) {
3141
s.frames = append(s.frames, f)
3242
}
3343

34-
// Len returns amount of frames contained in stack.
44+
// Len returns the number of frames contained in the stack.
3545
func (s *Stack) Len() int {
3646
return len(s.frames)
3747
}
3848

3949
// FramesIter returns an iterator over the frames in the stack.
4050
//
41-
// The iterator yields the frames starting from uppermost (the one added last).
51+
// The iterator yields the frames starting from the most recent (the one added last).
4252
func (s *Stack) FramesIter() iter.Seq2[int, runtime.Frame] {
4353
return func(yield func(int, runtime.Frame) bool) {
4454
start := len(s.frames) - 1
@@ -50,6 +60,8 @@ func (s *Stack) FramesIter() iter.Seq2[int, runtime.Frame] {
5060
}
5161
}
5262

63+
// String returns a multi-line string representation of the stack trace.
64+
// Each frame is shown with its function name and file:line.
5365
func (s *Stack) String() string {
5466
b := &strings.Builder{}
5567

@@ -64,13 +76,13 @@ func (s *Stack) String() string {
6476
return b.String()
6577
}
6678

67-
// WriteFrameToBuffer writes a string representation of the given [runtime.Frame]
68-
// to the provided [strings.Builder].
79+
// WriteFrameToBuffer writes a string representation of the given runtime.Frame
80+
// to the provided strings.Builder.
6981
//
7082
// The format is:
7183
//
72-
// FunctionName
73-
// FilePath:LineNumber
84+
// FunctionName
85+
// \tFilePath:LineNumber
7486
func WriteFrameToBuffer(f runtime.Frame, b *strings.Builder) {
7587
b.WriteString(f.Function)
7688
b.WriteRune('\n')

stacktrace/stacktrace.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,34 @@
1+
// Package stacktrace provides utilities for capturing, representing, and
2+
// formatting stack traces.
3+
//
4+
// The package allows you to capture the current call stack, inspect stack
5+
// frames, and render stack traces as human-readable strings.
6+
//
7+
// Example:
8+
//
9+
// stack := stacktrace.Capture(0, 10)
10+
// fmt.Println(stack)
11+
//
12+
// This captures up to 10 frames of the current stack and prints them in a
13+
// readable format.
114
package stacktrace
215

316
import (
417
"math"
518
"runtime"
619
)
720

21+
// DefaultDepth is the default maximum number of stack frames to capture.
822
const DefaultDepth = 64
923

1024
// Capture captures the current stack trace, skipping the specified number of
1125
// frames and limiting the depth of the trace. If depth is math.MaxInt, it
1226
// captures the full trace.
27+
//
28+
// skip controls how many stack frames to skip (0 means start from the caller of Capture).
29+
// depth limits the number of frames captured; use math.MaxInt for no limit.
30+
//
31+
// Returns a *Stack containing the captured frames.
1332
func Capture(skip, depth int) *Stack {
1433
skip++ // we don't want current function ot get to trace
1534

0 commit comments

Comments
 (0)