fix issue in stack trace for package gdebug; improve package gsmtp

This commit is contained in:
John
2020-04-18 10:17:54 +08:00
parent 4f87668780
commit c10149baa0
7 changed files with 93 additions and 46 deletions

View File

@ -42,12 +42,14 @@ func init() {
}
}
// CallerPath returns the function name and the absolute file path along with its line number of the caller.
// CallerPath returns the function name and the absolute file path along with its line
// number of the caller.
func Caller(skip ...int) (function string, path string, line int) {
return CallerWithFilter("", skip...)
}
// CallerPathWithFilter returns the function name and the absolute file path along with its line number of the caller.
// CallerPathWithFilter returns the function name and the absolute file path along with
// its line number of the caller.
//
// The parameter <filter> is used to filter the path of the caller.
func CallerWithFilter(filter string, skip ...int) (function string, path string, line int) {
@ -84,7 +86,10 @@ func CallerWithFilter(filter string, skip ...int) (function string, path string,
return "", "", -1
}
// callerFromIndex returns the caller position and according information exclusive of the debug package.
// callerFromIndex returns the caller position and according information exclusive of the
// debug package.
//
// VERY NOTE THAT, the returned index value should be <index - 1> as the caller's start point.
func callerFromIndex(filters []string) (pc uintptr, file string, line int, index int) {
var filtered, ok bool
for index = 0; index < gMAX_DEPTH; index++ {
@ -102,6 +107,9 @@ func callerFromIndex(filters []string) (pc uintptr, file string, line int, index
if strings.Contains(file, gFILTER_KEY) {
continue
}
if index > 0 {
index--
}
return
}
}

View File

@ -35,25 +35,33 @@ func StackWithFilter(filter string, skip ...int) string {
// StackWithFilters returns a formatted stack trace of the goroutine that calls it.
// It calls runtime.Stack with a large enough buffer to capture the entire trace.
//
// The parameter <filters> is a slice of strings, which are used to filter the path of the caller.
// The parameter <filters> is a slice of strings, which are used to filter the path of the
// caller.
//
// TODO Improve the performance using debug.Stack.
func StackWithFilters(filters []string, skip ...int) string {
number := 0
if len(skip) > 0 {
number = skip[0]
}
name := ""
space := " "
index := 1
buffer := bytes.NewBuffer(nil)
filtered := false
ok := true
pc, file, line, start := callerFromIndex(filters)
var (
name = ""
space = " "
index = 1
buffer = bytes.NewBuffer(nil)
filtered = false
ok = true
pc, file, line, start = callerFromIndex(filters)
)
for i := start + number; i < gMAX_DEPTH; i++ {
if i != start {
pc, file, line, ok = runtime.Caller(i)
}
if ok {
if goRootForFilter != "" && len(file) >= len(goRootForFilter) && file[0:len(goRootForFilter)] == goRootForFilter {
// GOROOT filter.
if goRootForFilter != "" &&
len(file) >= len(goRootForFilter) &&
file[0:len(goRootForFilter)] == goRootForFilter {
continue
}
filtered = false

View File

@ -10,6 +10,7 @@ package gdebug
import (
"runtime"
"runtime/debug"
"testing"
)
@ -49,6 +50,12 @@ func Benchmark_Stack(b *testing.B) {
}
}
func Benchmark_StackOfStdlib(b *testing.B) {
for i := 0; i < b.N; i++ {
debug.Stack()
}
}
func Benchmark_StackWithFilter(b *testing.B) {
for i := 0; i < b.N; i++ {
StackWithFilter("test")