Skip to content

Commit

Permalink
Update the stack parser to the latest raven-go code.
Browse files Browse the repository at this point in the history
  • Loading branch information
knz committed Jun 11, 2019
1 parent b566f84 commit 52fe544
Showing 1 changed file with 25 additions and 16 deletions.
41 changes: 25 additions & 16 deletions withstack/reportable.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ package withstack
import (
"errors"
"fmt"
"runtime"
"go/build"
"path/filepath"
"strconv"
"strings"

Expand Down Expand Up @@ -82,30 +83,38 @@ func convertPkgStack(st pkgErr.StackTrace) *ReportableStackTrace {
return parsePrintedStack(fmt.Sprintf("%+v", st))
}

// getSourceInfoFromPc extracts the details for a given program counter.
func getSourceInfoFromPc(pc uintptr) (file string, line int, fn *runtime.Func) {
fn = runtime.FuncForPC(pc)
if fn != nil {
file, line = fn.FileLine(pc)
} else {
file = "unknown"
// trimPath is a copy of the same function in package raven-go.
func trimPath(filename string) string {
for _, prefix := range trimPaths {
if trimmed := strings.TrimPrefix(filename, prefix); len(trimmed) < len(filename) {
return trimmed
}
}
return file, line, fn
return filename
}

// trimPath is a copy of the same function in package raven-go.
func trimPath(filename string) string {
const src = "/src/"
prefix := strings.LastIndex(filename, src)
if prefix == -1 {
return filename
var trimPaths []string

// init is a copy of the same function in package raven-go.
func init() {
// Collect all source directories, and make sure they
// end in a trailing "separator"
for _, prefix := range build.Default.SrcDirs() {
if prefix[len(prefix)-1] != filepath.Separator {
prefix += string(filepath.Separator)
}
trimPaths = append(trimPaths, prefix)
}
return filename[prefix+len(src):]
}

// functionName is an adapted copy of the same function in package raven-go.
func functionName(fnName string) (pack string, name string) {
name = fnName
// We get this:
// runtime/debug.*T·ptrmethod
// and want this:
// pack = runtime/debug
// name = *T.ptrmethod
if idx := strings.LastIndex(name, "."); idx != -1 {
pack = name[:idx]
name = name[idx+1:]
Expand Down

0 comments on commit 52fe544

Please sign in to comment.