From fbad5f60eb6590d5b4e096216cb51219e23cb16f Mon Sep 17 00:00:00 2001 From: jflyfox Date: Tue, 22 Jun 2021 15:09:08 +0800 Subject: [PATCH] improve caller path filtering for package gdebug --- debug/gdebug/gdebug_caller.go | 23 +++++++++++++---------- debug/gdebug/gdebug_stack.go | 9 ++++++--- internal/utils/utils_debug.go | 4 ++-- util/gmode/gmode.go | 1 + 4 files changed, 22 insertions(+), 15 deletions(-) diff --git a/debug/gdebug/gdebug_caller.go b/debug/gdebug/gdebug_caller.go index 485a61228..fa9bb2115 100644 --- a/debug/gdebug/gdebug_caller.go +++ b/debug/gdebug/gdebug_caller.go @@ -8,6 +8,7 @@ package gdebug import ( "fmt" + "github.com/gogf/gf/internal/utils" "os" "os/exec" "path/filepath" @@ -53,11 +54,13 @@ func Caller(skip ...int) (function string, path string, line int) { // // The parameter is used to filter the path of the caller. func CallerWithFilter(filter string, skip ...int) (function string, path string, line int) { - number := 0 + var ( + number = 0 + ok = true + ) if len(skip) > 0 { number = skip[0] } - ok := true pc, file, line, start := callerFromIndex([]string{filter}) if start != -1 { for i := start + number; i < maxCallerDepth; i++ { @@ -65,12 +68,6 @@ func CallerWithFilter(filter string, skip ...int) (function string, path string, pc, file, line, ok = runtime.Caller(i) } if ok { - if filter != "" && strings.Contains(file, filter) { - continue - } - if strings.Contains(file, stackFilterKey) { - continue - } function := "" if fn := runtime.FuncForPC(pc); fn == nil { function = "unknown" @@ -104,8 +101,14 @@ func callerFromIndex(filters []string) (pc uintptr, file string, line int, index if filtered { continue } - if strings.Contains(file, stackFilterKey) { - continue + if !utils.IsDebugEnabled() { + if strings.Contains(file, utils.StackFilterKeyForGoFrame) { + continue + } + } else { + if strings.Contains(file, stackFilterKey) { + continue + } } if index > 0 { index-- diff --git a/debug/gdebug/gdebug_stack.go b/debug/gdebug/gdebug_stack.go index 09ac951cd..a9c0a1e9c 100644 --- a/debug/gdebug/gdebug_stack.go +++ b/debug/gdebug/gdebug_stack.go @@ -80,14 +80,17 @@ func StackWithFilters(filters []string, skip ...int) string { if filtered { continue } - if strings.Contains(file, stackFilterKey) { - continue - } + if !utils.IsDebugEnabled() { if strings.Contains(file, utils.StackFilterKeyForGoFrame) { continue } + } else { + if strings.Contains(file, stackFilterKey) { + continue + } } + if fn := runtime.FuncForPC(pc); fn == nil { name = "unknown" } else { diff --git a/internal/utils/utils_debug.go b/internal/utils/utils_debug.go index c03d30dd4..201b78662 100644 --- a/internal/utils/utils_debug.go +++ b/internal/utils/utils_debug.go @@ -11,8 +11,8 @@ import ( ) const ( - debugKey = "gf.debug" // Debug key for checking if in debug mode. - StackFilterKeyForGoFrame = "/github.com/gogf/gf/" // Stack filtering key for all GoFrame module paths. + debugKey = "gf.debug" // Debug key for checking if in debug mode. + StackFilterKeyForGoFrame = "github.com/gogf/gf/" // Stack filtering key for all GoFrame module paths. ) var ( diff --git a/util/gmode/gmode.go b/util/gmode/gmode.go index 9b43dacaf..14df6f5a2 100644 --- a/util/gmode/gmode.go +++ b/util/gmode/gmode.go @@ -25,6 +25,7 @@ const ( ) var ( + // Note that `currentMode` is not concurrent safe. currentMode = NOT_SET )