From 9cc5d7a691350c6dcab835ee80c0f981bd3c5009 Mon Sep 17 00:00:00 2001 From: John Guo Date: Wed, 27 Jan 2021 00:20:23 +0800 Subject: [PATCH] improve package gdebug/gfile --- debug/gdebug/gdebug_caller.go | 12 +++++----- debug/gdebug/gdebug_stack.go | 4 ++-- internal/intlog/intlog.go | 8 +++---- net/ghttp/ghttp_server_router.go | 4 ++-- net/ghttp/ghttp_server_router_group.go | 2 +- os/gfile/gfile_source.go | 31 +++++++++----------------- 6 files changed, 26 insertions(+), 35 deletions(-) diff --git a/debug/gdebug/gdebug_caller.go b/debug/gdebug/gdebug_caller.go index 3256d2695..a70ffda6a 100644 --- a/debug/gdebug/gdebug_caller.go +++ b/debug/gdebug/gdebug_caller.go @@ -17,8 +17,8 @@ import ( ) const ( - gMAX_DEPTH = 1000 - gFILTER_KEY = "/debug/gdebug/gdebug" + maxCallerDepth = 1000 + stackFilterKey = "/debug/gdebug/gdebug" ) var ( @@ -60,7 +60,7 @@ func CallerWithFilter(filter string, skip ...int) (function string, path string, ok := true pc, file, line, start := callerFromIndex([]string{filter}) if start != -1 { - for i := start + number; i < gMAX_DEPTH; i++ { + for i := start + number; i < maxCallerDepth; i++ { if i != start { pc, file, line, ok = runtime.Caller(i) } @@ -68,7 +68,7 @@ func CallerWithFilter(filter string, skip ...int) (function string, path string, if filter != "" && strings.Contains(file, filter) { continue } - if strings.Contains(file, gFILTER_KEY) { + if strings.Contains(file, stackFilterKey) { continue } function := "" @@ -92,7 +92,7 @@ func CallerWithFilter(filter string, skip ...int) (function string, path string, // VERY NOTE THAT, the returned index value should be 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++ { + for index = 0; index < maxCallerDepth; index++ { if pc, file, line, ok = runtime.Caller(index); ok { filtered = false for _, filter := range filters { @@ -104,7 +104,7 @@ func callerFromIndex(filters []string) (pc uintptr, file string, line int, index if filtered { continue } - if strings.Contains(file, gFILTER_KEY) { + if strings.Contains(file, stackFilterKey) { continue } if index > 0 { diff --git a/debug/gdebug/gdebug_stack.go b/debug/gdebug/gdebug_stack.go index 5e6b572fc..af61b9591 100644 --- a/debug/gdebug/gdebug_stack.go +++ b/debug/gdebug/gdebug_stack.go @@ -53,7 +53,7 @@ func StackWithFilters(filters []string, skip ...int) string { ok = true pc, file, line, start = callerFromIndex(filters) ) - for i := start + number; i < gMAX_DEPTH; i++ { + for i := start + number; i < maxCallerDepth; i++ { if i != start { pc, file, line, ok = runtime.Caller(i) } @@ -79,7 +79,7 @@ func StackWithFilters(filters []string, skip ...int) string { if filtered { continue } - if strings.Contains(file, gFILTER_KEY) { + if strings.Contains(file, stackFilterKey) { continue } if fn := runtime.FuncForPC(pc); fn == nil { diff --git a/internal/intlog/intlog.go b/internal/intlog/intlog.go index c2cc60ad6..4c6ec3031 100644 --- a/internal/intlog/intlog.go +++ b/internal/intlog/intlog.go @@ -16,7 +16,7 @@ import ( ) const ( - gFILTER_KEY = "/internal/intlog" + stackFilterKey = "/internal/intlog" ) var ( @@ -71,7 +71,7 @@ func Error(v ...interface{}) { return } array := append([]interface{}{now(), "[INTE]", file()}, v...) - array = append(array, "\n"+gdebug.StackWithFilter(gFILTER_KEY)) + array = append(array, "\n"+gdebug.StackWithFilter(stackFilterKey)) fmt.Println(array...) } @@ -82,7 +82,7 @@ func Errorf(format string, v ...interface{}) { } fmt.Printf( now()+" [INTE] "+file()+" "+format+"\n%s\n", - append(v, gdebug.StackWithFilter(gFILTER_KEY))..., + append(v, gdebug.StackWithFilter(stackFilterKey))..., ) } @@ -93,6 +93,6 @@ func now() string { // file returns caller file name along with its line number. func file() string { - _, p, l := gdebug.CallerWithFilter(gFILTER_KEY) + _, p, l := gdebug.CallerWithFilter(stackFilterKey) return fmt.Sprintf(`%s:%d`, filepath.Base(p), l) } diff --git a/net/ghttp/ghttp_server_router.go b/net/ghttp/ghttp_server_router.go index 8049d01e4..cc7d883b3 100644 --- a/net/ghttp/ghttp_server_router.go +++ b/net/ghttp/ghttp_server_router.go @@ -20,7 +20,7 @@ import ( ) const ( - gFILTER_KEY = "/net/ghttp/ghttp" + stackFilterKey = "/net/ghttp/ghttp" ) var ( @@ -68,7 +68,7 @@ func (s *Server) parsePattern(pattern string) (domain, method, path string, err func (s *Server) setHandler(pattern string, handler *handlerItem) { handler.itemId = handlerIdGenerator.Add(1) if handler.source == "" { - _, file, line := gdebug.CallerWithFilter(gFILTER_KEY) + _, file, line := gdebug.CallerWithFilter(stackFilterKey) handler.source = fmt.Sprintf(`%s:%d`, file, line) } domain, method, uri, err := s.parsePattern(pattern) diff --git a/net/ghttp/ghttp_server_router_group.go b/net/ghttp/ghttp_server_router_group.go index 90dbca7bc..3b4d01d0b 100644 --- a/net/ghttp/ghttp_server_router_group.go +++ b/net/ghttp/ghttp_server_router_group.go @@ -248,7 +248,7 @@ func (g *RouterGroup) Middleware(handlers ...HandlerFunc) *RouterGroup { // preBindToLocalArray adds the route registering parameters to internal variable array for lazily registering feature. func (g *RouterGroup) preBindToLocalArray(bindType string, pattern string, object interface{}, params ...interface{}) *RouterGroup { - _, file, line := gdebug.CallerWithFilter(gFILTER_KEY) + _, file, line := gdebug.CallerWithFilter(stackFilterKey) preBindItems = append(preBindItems, &preBindItem{ group: g, bindType: bindType, diff --git a/os/gfile/gfile_source.go b/os/gfile/gfile_source.go index 3faa85b11..efe93840b 100644 --- a/os/gfile/gfile_source.go +++ b/os/gfile/gfile_source.go @@ -7,7 +7,7 @@ package gfile import ( - "os" + "github.com/gogf/gf/text/gstr" "runtime" "strings" @@ -36,7 +36,7 @@ func init() { // Note2: When the method is called for the first time, if it is in an asynchronous goroutine, // the method may not get the main package path. func MainPkgPath() string { - // Only for source development environments. + // It is only for source development environments. if goRootForFilter == "" { return "" } @@ -44,20 +44,23 @@ func MainPkgPath() string { if path != "" { return path } - lastFile := "" for i := 1; i < 10000; i++ { - if _, file, _, ok := runtime.Caller(i); ok { + if pc, file, _, ok := runtime.Caller(i); ok { if goRootForFilter != "" && len(file) >= len(goRootForFilter) && file[0:len(goRootForFilter)] == goRootForFilter { continue } - if gregex.IsMatchString(`/github.com/[^/]+/gf/`, file) && - !gregex.IsMatchString(`/github.com/[^/]+/gf/\.example/`, file) { - continue + // Check if it is called in package initialization function, + // in which it here cannot retrieve main package path, + // it so just returns that can make next check. + if fn := runtime.FuncForPC(pc); fn != nil { + array := gstr.Split(fn.Name(), ".") + if array[0] != "main" { + continue + } } if Ext(file) != ".go" { continue } - lastFile = file if gregex.IsMatchString(`package\s+main`, GetContents(file)) { mainPkgPath.Set(Dir(file)) return Dir(file) @@ -66,17 +69,5 @@ func MainPkgPath() string { break } } - if lastFile != "" { - for path = Dir(lastFile); len(path) > 1 && Exists(path) && path[len(path)-1] != os.PathSeparator; { - files, _ := ScanDir(path, "*.go") - for _, v := range files { - if gregex.IsMatchString(`package\s+main`, GetContents(v)) { - mainPkgPath.Set(path) - return path - } - } - path = Dir(path) - } - } return "" }