From 8469cd1365764c8d1167c0b13dd7b0b2cc50433f Mon Sep 17 00:00:00 2001 From: caixw Date: Fri, 10 May 2024 08:15:01 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=B8=BA=20Stack=20=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=20ignoreRuntime=20=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 由用户决定是否需要显示系统调用的条目。 --- go.mod | 2 +- go.sum | 4 ++-- mod.go | 4 ++-- mod_test.go | 12 ++++++------ source.go | 9 +++++---- source_test.go | 2 +- 6 files changed, 17 insertions(+), 16 deletions(-) diff --git a/go.mod b/go.mod index fee9d51..d504d63 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/issue9/source go 1.21 require ( - github.com/issue9/assert/v4 v4.1.1 + github.com/issue9/assert/v4 v4.3.0 github.com/issue9/errwrap v0.3.2 golang.org/x/mod v0.17.0 ) diff --git a/go.sum b/go.sum index 3e923b5..992237a 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -github.com/issue9/assert/v4 v4.1.1 h1:OhPE8SB8n/qZCNGLQa+6MQtr/B3oON0JAVj68k8jJlc= -github.com/issue9/assert/v4 v4.1.1/go.mod h1:v7qDRXi7AsaZZNh8eAK2rkLJg5/clztqQGA1DRv9Lv4= +github.com/issue9/assert/v4 v4.3.0 h1:W3XDKmttsfzihYGxJ9rJoL2ViJgWERB9IxfHcxjv65U= +github.com/issue9/assert/v4 v4.3.0/go.mod h1:v7qDRXi7AsaZZNh8eAK2rkLJg5/clztqQGA1DRv9Lv4= github.com/issue9/errwrap v0.3.2 h1:7KEme9Pfe75M+sIMcPCn/DV90wjnOcRbO4DXVAHj3Fw= github.com/issue9/errwrap v0.3.2/go.mod h1:KcCLuUGiffjooLCUjL89r1cyO8/HT/VRcQrneO53N3A= golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA= diff --git a/mod.go b/mod.go index cf9b2e5..deeee50 100644 --- a/mod.go +++ b/mod.go @@ -78,10 +78,10 @@ LOOP: } } -// PackagePath 文件或目录 p 所在 Go 文件的导出路径 +// PkgPath 文件或目录 p 所在 Go 文件的导出路径 // // 会向上查找 go.mod,根据 go.mod 中的 module 结合当前目录组成当前目录的导出路径。 -func PackagePath(p string) (string, error) { +func PkgPath(p string) (string, error) { abs, err := filepath.Abs(p) if err != nil { return "", err diff --git a/mod_test.go b/mod_test.go index dab5b6b..f1ef1f5 100644 --- a/mod_test.go +++ b/mod_test.go @@ -36,21 +36,21 @@ func TestModDir(t *testing.T) { a.NotError(err).NotEmpty(d) } -func TestPackagePath(t *testing.T) { +func TestPkgPath(t *testing.T) { a := assert.New(t, false) - p, err := PackagePath("./") + p, err := PkgPath("./") a.NotError(err).Equal(p, "github.com/issue9/source") - p, err = PackagePath("./testdata") + p, err = PkgPath("./testdata") a.NotError(err).Equal(p, "github.com/issue9/source/testdata") - p, err = PackagePath("./testdata/go.mod/sub/") + p, err = PkgPath("./testdata/go.mod/sub/") a.NotError(err).Equal(p, "github.com/issue9/source/mod/sub") - p, err = PackagePath("./testdata/go.mod/sub/sub.go") + p, err = PkgPath("./testdata/go.mod/sub/sub.go") a.NotError(err).Equal(p, "github.com/issue9/source/mod/sub") - p, err = PackagePath("./testdata/go.mod/go.mod") + p, err = PkgPath("./testdata/go.mod/go.mod") a.NotError(err).Equal(p, "github.com/issue9/source/mod") } diff --git a/source.go b/source.go index 94cf92e..8b709a9 100644 --- a/source.go +++ b/source.go @@ -77,9 +77,9 @@ func CurrentFunction() string { // - 2 表示 Stack 的调用者,以此类推; // // msg 表示需要输出的额外信息; -func Stack(skip int, msg ...any) string { +func Stack(skip int, ignoreRuntime bool, msg ...any) string { buf := &bytes.Buffer{} - DumpStack(buf, skip+1, msg...) + DumpStack(buf, skip+1, ignoreRuntime, msg...) return buf.String() } @@ -90,8 +90,9 @@ func Stack(skip int, msg ...any) string { // - 1 表示 Stack 自身; // - 2 表示 Stack 的调用者,以此类推; // +// ignoreRuntime 表示是否不显示 runtime 下的系统调用信息; // msg 表示需要输出的额外信息; -func DumpStack(w io.Writer, skip int, msg ...any) { +func DumpStack(w io.Writer, skip int, ignoreRuntime bool, msg ...any) { pc := make([]uintptr, 10) n := runtime.Callers(skip, pc) if n == 0 { @@ -109,7 +110,7 @@ func DumpStack(w io.Writer, skip int, msg ...any) { break } - if strings.Contains(frame.File, "runtime/") { // 忽略 runtime 下的系统调用 + if ignoreRuntime && strings.Contains(frame.File, "runtime/") { continue } diff --git a/source_test.go b/source_test.go index 8abd14e..b7a187c 100644 --- a/source_test.go +++ b/source_test.go @@ -65,7 +65,7 @@ func TestCurrentLocation(t *testing.T) { func TestStack(t *testing.T) { a := assert.New(t, false) - str := Stack(1, "message", 12) + str := Stack(1, true, "message", 12) t.Log(str) a.True(strings.HasPrefix(str, "message 12")) a.True(strings.Contains(str, "source_test.go:68"), str) // 依赖调用 Stack 的行号