frame.go 732 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package stacktrace
  2. import (
  3. "path/filepath"
  4. "runtime"
  5. "strings"
  6. )
  7. // NewFrame returns a new stack frame for the provided information
  8. func NewFrame(pc uintptr, file string, line int) Frame {
  9. fn := runtime.FuncForPC(pc)
  10. if fn == nil {
  11. return Frame{}
  12. }
  13. pack, name := parseFunctionName(fn.Name())
  14. return Frame{
  15. Line: line,
  16. File: filepath.Base(file),
  17. Package: pack,
  18. Function: name,
  19. }
  20. }
  21. func parseFunctionName(name string) (string, string) {
  22. i := strings.LastIndex(name, ".")
  23. if i == -1 {
  24. return "", name
  25. }
  26. return name[:i], name[i+1:]
  27. }
  28. // Frame contains all the information for a stack frame within a go program
  29. type Frame struct {
  30. File string
  31. Function string
  32. Package string
  33. Line int
  34. }