format.go

v0.2.0
Doc Versions Source
1
package stacktrace
2
3
import "fmt"
4
5
// String returns a compact representation of the frame: Function (File:Line).
6
func (f Frame) String() string {
7
	return fmt.Sprintf("%s (%s:%d)", f.Function, f.File, f.Line)
8
}
9
10
// Format implements [fmt.Formatter] for Trace.
11
//
12
//   - %v / %s — compact: one line per frame, e.g. "funcName (file.go:42)"
13
//   - %+v     — verbose: two lines per frame (function, then indented location)
14
func (t Trace) Format(s fmt.State, verb rune) {
15
	switch verb {
16
	case 'v':
17
		if s.Flag('+') {
18
			for i, f := range t {
19
				if i > 0 {
20
					fmt.Fprint(s, "\n")
21
				}
22
				fmt.Fprintf(s, "%s\n\t%s:%d", f.Function, f.File, f.Line)
23
			}
24
			return
25
		}
26
		fallthrough
27
	case 's':
28
		for i, f := range t {
29
			if i > 0 {
30
				fmt.Fprint(s, "\n")
31
			}
32
			fmt.Fprintf(s, "%s (%s:%d)", f.Function, f.File, f.Line)
33
		}
34
	}
35
}
36

Source Files