Skip to content

Commit

Permalink
add external PR:pkg#198
Browse files Browse the repository at this point in the history
  • Loading branch information
peakle committed Nov 13, 2021
1 parent e4dcbeb commit f06639a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ _testmain.go
*.exe
*.test
*.prof
.idea
36 changes: 25 additions & 11 deletions stack.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package errors

import (
"bytes"
"fmt"
"io"
"path"
Expand Down Expand Up @@ -49,6 +50,8 @@ func (f Frame) name() string {
return fn.Name()
}

func (f Frame) Format(s fmt.State, verb rune) { f.format(s, s, verb) }

// Format formats the frame according to the fmt.Formatter interface.
//
// %s source file
Expand All @@ -61,25 +64,25 @@ func (f Frame) name() string {
// %+s function name and path of source file relative to the compile time
// GOPATH separated by \n\t (<funcname>\n\t<path>)
// %+v equivalent to %+s:%d
func (f Frame) Format(s fmt.State, verb rune) {
func (f Frame) format(w io.Writer, s fmt.State, verb rune) {
switch verb {
case 's':
switch {
case s.Flag('+'):
io.WriteString(s, f.name())
io.WriteString(s, "\n\t")
io.WriteString(s, f.file())
io.WriteString(w, f.name())
io.WriteString(w, "\n\t")
io.WriteString(w, f.file())
default:
io.WriteString(s, path.Base(f.file()))
io.WriteString(w, path.Base(f.file()))
}
case 'd':
io.WriteString(s, strconv.Itoa(f.line()))
io.WriteString(w, strconv.Itoa(f.line()))
case 'n':
io.WriteString(s, funcname(f.name()))
io.WriteString(w, funcname(f.name()))
case 'v':
f.Format(s, 's')
io.WriteString(s, ":")
f.Format(s, 'd')
f.format(w, s, 's')
io.WriteString(w, ":")
f.format(w, s, 'd')
}
}

Expand Down Expand Up @@ -141,11 +144,22 @@ func (st StackTrace) formatSlice(s fmt.State, verb rune) {
// stack represents a stack of program counters.
type stack []uintptr

// stackMinLen is a best-guess at the minimum length of a stack trace. It
// doesn't need to be exact, just give a good enough head start for the buffer
// to avoid the expensive early growth.
const stackMinLen = 96

func (s *stack) Format(st fmt.State, verb rune) {
if verb == 'v' && st.Flag('+') {
var b = &bytes.Buffer{}
b.Grow(len(*s) * stackMinLen)

for i := range *s {
fmt.Fprintf(st, "\n%+v", Frame((*s)[i]))
b.WriteByte('\n')
Frame((*s)[i]).format(b, st, verb)
}

io.Copy(st, b)
}
}

Expand Down

0 comments on commit f06639a

Please sign in to comment.