Skip to content

Commit 817915b

Browse files
committed
Merge branch 'release/v1.6.0'
2 parents 54be5f3 + 44dafbc commit 817915b

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

stack.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,15 @@ var ErrNoFunc = errors.New("no call stack information")
7171
// %s source file
7272
// %d line number
7373
// %n function name
74+
// %k last segment of the package path
7475
// %v equivalent to %s:%d
7576
//
7677
// It accepts the '+' and '#' flags for most of the verbs as follows.
7778
//
7879
// %+s path of source file relative to the compile time GOPATH
7980
// %#s full path of source file
8081
// %+n import path qualified function name
82+
// %+k full package path
8183
// %+v equivalent to %+s:%d
8284
// %#v equivalent to %#s:%d
8385
func (c Call) Format(s fmt.State, verb rune) {
@@ -111,6 +113,22 @@ func (c Call) Format(s fmt.State, verb rune) {
111113
buf := [6]byte{}
112114
s.Write(strconv.AppendInt(buf[:0], int64(line), 10))
113115

116+
case 'k':
117+
name := c.fn.Name()
118+
const pathSep = "/"
119+
start, end := 0, len(name)
120+
if i := strings.LastIndex(name, pathSep); i != -1 {
121+
start = i + len(pathSep)
122+
}
123+
const pkgSep = "."
124+
if i := strings.Index(name[start:], pkgSep); i != -1 {
125+
end = start + i
126+
}
127+
if s.Flag('+') {
128+
start = 0
129+
}
130+
io.WriteString(s, name[start:end])
131+
114132
case 'n':
115133
name := c.fn.Name()
116134
if !s.Flag('+') {

stack_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ func TestCallFormat(t *testing.T) {
5555
{c, "func", "%d", fmt.Sprint(line)},
5656
{c, "func", "%n", "TestCallFormat"},
5757
{c, "func", "%+n", runtime.FuncForPC(pc - 1).Name()},
58+
{c, "func", "%k", "stack_test"},
59+
{c, "func", "%+k", "github.com/go-stack/stack_test"},
5860
{c, "func", "%v", fmt.Sprint(path.Base(file), ":", line)},
5961
{c, "func", "%+v", fmt.Sprint(relFile, ":", line)},
6062
{c, "func", "%#v", fmt.Sprint(file, ":", line)},
@@ -65,6 +67,8 @@ func TestCallFormat(t *testing.T) {
6567
{c2, "meth", "%d", fmt.Sprint(line2)},
6668
{c2, "meth", "%n", "testType.testMethod"},
6769
{c2, "meth", "%+n", runtime.FuncForPC(pc2).Name()},
70+
{c2, "meth", "%k", "stack_test"},
71+
{c2, "meth", "%+k", "github.com/go-stack/stack_test"},
6872
{c2, "meth", "%v", fmt.Sprint(path.Base(file2), ":", line2)},
6973
{c2, "meth", "%+v", fmt.Sprint(relFile2, ":", line2)},
7074
{c2, "meth", "%#v", fmt.Sprint(file2, ":", line2)},

0 commit comments

Comments
 (0)