stack.go 610 B

123456789101112131415161718192021222324252627282930313233343536
  1. package fserr
  2. import (
  3. "fmt"
  4. "github.com/pkg/errors"
  5. "runtime"
  6. )
  7. // stack represents a stack of program counters.
  8. type stack []uintptr
  9. func (s *stack) Format(st fmt.State, verb rune) {
  10. switch verb {
  11. case 'v':
  12. switch {
  13. case st.Flag('+'):
  14. for _, pc := range *s {
  15. fmt.Fprintf(st, "\n%+v", errors.Frame(pc))
  16. }
  17. }
  18. }
  19. }
  20. func (s *stack) StackTrace() errors.StackTrace {
  21. f := make([]errors.Frame, len(*s))
  22. for i := 0; i < len(f); i++ {
  23. f[i] = errors.Frame((*s)[i])
  24. }
  25. return f
  26. }
  27. func callers() *stack {
  28. var pcs [32]uintptr
  29. var st stack = pcs[0:runtime.Callers(3, pcs[:])]
  30. return &st
  31. }