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