File: stackframe.go

package info (click to toggle)
golang-github-joomcode-errorx 1.2.0-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 280 kB
  • sloc: makefile: 2
file content (48 lines) | stat: -rw-r--r-- 768 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package errorx

import (
	"runtime"
)

type frame interface {
	Function() string
	File() string
	Line() int
}

type frameHelper struct {
}

var frameHelperSingleton = &frameHelper{}

type defaultFrame struct {
	frame *runtime.Frame
}

func (f *defaultFrame) Function() string {
	return f.frame.Function
}

func (f *defaultFrame) File() string {
	return f.frame.File
}

func (f *defaultFrame) Line() int {
	return f.frame.Line
}

func (c *frameHelper) GetFrames(pcs []uintptr) []frame {
	frames := runtime.CallersFrames(pcs[:])
	result := make([]frame, 0, len(pcs))

	var rawFrame runtime.Frame
	next := true
	for next {
		rawFrame, next = frames.Next()
		frameCopy := rawFrame
		frame := &defaultFrame{&frameCopy}
		result = append(result, frame)
	}

	return result
}