Skip to content

Commit

Permalink
Merge pull request #18 from chmorgan/develop
Browse files Browse the repository at this point in the history
add %k and %+k to stack formatter
  • Loading branch information
ChrisHines authored Jul 24, 2017
2 parents 3bd81bd + 6820643 commit 44dafbc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
18 changes: 18 additions & 0 deletions stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,15 @@ var ErrNoFunc = errors.New("no call stack information")
// %s source file
// %d line number
// %n function name
// %k last segment of the package path
// %v equivalent to %s:%d
//
// It accepts the '+' and '#' flags for most of the verbs as follows.
//
// %+s path of source file relative to the compile time GOPATH
// %#s full path of source file
// %+n import path qualified function name
// %+k full package path
// %+v equivalent to %+s:%d
// %#v equivalent to %#s:%d
func (c Call) Format(s fmt.State, verb rune) {
Expand Down Expand Up @@ -111,6 +113,22 @@ func (c Call) Format(s fmt.State, verb rune) {
buf := [6]byte{}
s.Write(strconv.AppendInt(buf[:0], int64(line), 10))

case 'k':
name := c.fn.Name()
const pathSep = "/"
start, end := 0, len(name)
if i := strings.LastIndex(name, pathSep); i != -1 {
start = i + len(pathSep)
}
const pkgSep = "."
if i := strings.Index(name[start:], pkgSep); i != -1 {
end = start + i
}
if s.Flag('+') {
start = 0
}
io.WriteString(s, name[start:end])

case 'n':
name := c.fn.Name()
if !s.Flag('+') {
Expand Down
4 changes: 4 additions & 0 deletions stack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ func TestCallFormat(t *testing.T) {
{c, "func", "%d", fmt.Sprint(line)},
{c, "func", "%n", "TestCallFormat"},
{c, "func", "%+n", runtime.FuncForPC(pc - 1).Name()},
{c, "func", "%k", "stack_test"},
{c, "func", "%+k", "github.com/go-stack/stack_test"},
{c, "func", "%v", fmt.Sprint(path.Base(file), ":", line)},
{c, "func", "%+v", fmt.Sprint(relFile, ":", line)},
{c, "func", "%#v", fmt.Sprint(file, ":", line)},
Expand All @@ -65,6 +67,8 @@ func TestCallFormat(t *testing.T) {
{c2, "meth", "%d", fmt.Sprint(line2)},
{c2, "meth", "%n", "testType.testMethod"},
{c2, "meth", "%+n", runtime.FuncForPC(pc2).Name()},
{c2, "meth", "%k", "stack_test"},
{c2, "meth", "%+k", "github.com/go-stack/stack_test"},
{c2, "meth", "%v", fmt.Sprint(path.Base(file2), ":", line2)},
{c2, "meth", "%+v", fmt.Sprint(relFile2, ":", line2)},
{c2, "meth", "%#v", fmt.Sprint(file2, ":", line2)},
Expand Down

0 comments on commit 44dafbc

Please sign in to comment.