Skip to content

Commit 822e2fe

Browse files
committed
Improve support for older versions of Go.
1 parent 00f6035 commit 822e2fe

File tree

4 files changed

+22
-16
lines changed

4 files changed

+22
-16
lines changed

Diff for: format_test.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// +build go1.2
2+
13
package stack_test
24

35
import (
@@ -7,13 +9,13 @@ import (
79
)
810

911
func Example_callFormat() {
10-
log("%+s")
11-
log("%v %[1]n()")
12+
logCaller("%+s")
13+
logCaller("%v %[1]n()")
1214
// Output:
1315
// github.com/go-stack/stack/format_test.go
14-
// format_test.go:11 Example_callFormat()
16+
// format_test.go:13 Example_callFormat()
1517
}
1618

17-
func log(format string) {
19+
func logCaller(format string) {
1820
fmt.Printf(format+"\n", stack.Caller(1))
1921
}

Diff for: stack.go

+10-6
Original file line numberDiff line numberDiff line change
@@ -262,18 +262,22 @@ func init() {
262262
}
263263
}
264264

265-
func inGoroot(path string) bool {
265+
func inGoroot(c Call) bool {
266+
file := c.file()
267+
if len(file) == 0 || file[0] == '?' {
268+
return true
269+
}
266270
if runtime.GOOS == "windows" {
267-
path = strings.ToLower(path)
271+
file = strings.ToLower(file)
268272
}
269-
return strings.HasPrefix(path, goroot)
273+
return strings.HasPrefix(file, goroot)
270274
}
271275

272276
// TrimRuntime returns a slice of the CallStack with the topmost entries from
273-
// the go runtime removed. It considers any calls originating from files under
274-
// GOROOT as part of the runtime.
277+
// the go runtime removed. It considers any calls originating from unknown
278+
// files or files under GOROOT as part of the runtime.
275279
func (cs CallStack) TrimRuntime() CallStack {
276-
for len(cs) > 0 && inGoroot(cs[len(cs)-1].file()) {
280+
for len(cs) > 0 && inGoroot(cs[len(cs)-1]) {
277281
cs = cs[:len(cs)-1]
278282
}
279283
return cs

Diff for: stack12.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ import (
77
)
88

99
func getUintptrs() []uintptr {
10+
var s []uintptr
1011
select {
11-
case s := <-pcStackPool:
12-
return s[:cap(s)]
12+
case s = <-pcStackPool:
13+
s = s[:cap(s)]
1314
default:
14-
return make([]uintptr, 1000)
15+
s = make([]uintptr, 1000)
1516
}
17+
return s
1618
}
1719

1820
func putUintptrs(s []uintptr) {

Diff for: stack_test.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ func TestCallFormat(t *testing.T) {
5656
{c, "func", "%v", fmt.Sprint(path.Base(file), ":", line)},
5757
{c, "func", "%+v", fmt.Sprint(relFile, ":", line)},
5858
{c, "func", "%#v", fmt.Sprint(file, ":", line)},
59-
{c, "func", "%v|%[1]n()", fmt.Sprint(path.Base(file), ":", line, "|", "TestCallFormat()")},
6059

6160
{c2, "meth", "%s", path.Base(file2)},
6261
{c2, "meth", "%+s", relFile2},
@@ -67,7 +66,6 @@ func TestCallFormat(t *testing.T) {
6766
{c2, "meth", "%v", fmt.Sprint(path.Base(file2), ":", line2)},
6867
{c2, "meth", "%+v", fmt.Sprint(relFile2, ":", line2)},
6968
{c2, "meth", "%#v", fmt.Sprint(file2, ":", line2)},
70-
{c2, "meth", "%v|%[1]n()", fmt.Sprint(path.Base(file2), ":", line2, "|", "testType.testMethod()")},
7169
}
7270

7371
for _, d := range data {
@@ -213,7 +211,7 @@ func BenchmarkFuncForPC(b *testing.B) {
213211
fn = runtime.FuncForPC(pc)
214212
}
215213
b.StopTimer()
216-
b.Log(fn.FileLine(pc))
214+
fmt.Fprint(ioutil.Discard, fn.Entry())
217215
}
218216

219217
func deepStack(depth int, b *testing.B) stack.CallStack {

0 commit comments

Comments
 (0)