Skip to content

Commit

Permalink
refactor: 为 Stack 添加 ignoreRuntime 参数
Browse files Browse the repository at this point in the history
由用户决定是否需要显示系统调用的条目。
  • Loading branch information
caixw committed May 10, 2024
1 parent 3b29067 commit 8469cd1
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 16 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down
4 changes: 2 additions & 2 deletions mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions mod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
9 changes: 5 additions & 4 deletions source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand All @@ -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 {
Expand All @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 的行号
Expand Down

0 comments on commit 8469cd1

Please sign in to comment.