mirror of
https://gitee.com/johng/gf
synced 2026-06-06 02:25:47 +08:00
fix issue in stack trace for package gdebug; improve package gsmtp
This commit is contained in:
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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")
|
||||
|
||||
Reference in New Issue
Block a user