@@ -2,11 +2,11 @@ package log_test
2
2
3
3
import (
4
4
"bytes"
5
+ "runtime"
5
6
"sync"
6
7
"testing"
7
8
8
9
"github.com/go-kit/log"
9
- "github.com/go-kit/log/internal/stack"
10
10
)
11
11
12
12
func TestContext (t * testing.T ) {
@@ -108,7 +108,7 @@ func TestWithPrefixAndSuffix(t *testing.T) {
108
108
// Valuers, regardless of how many times With has been called.
109
109
func TestContextStackDepth (t * testing.T ) {
110
110
t .Parallel ()
111
- fn := stack . Caller ( 0 ). Function
111
+ fn := callingFunctions ()[ 0 ]
112
112
113
113
var output []interface {}
114
114
@@ -118,8 +118,8 @@ func TestContextStackDepth(t *testing.T) {
118
118
}))
119
119
120
120
stackValuer := log .Valuer (func () interface {} {
121
- for i , f := range stack . Trace () {
122
- if f . Function == fn {
121
+ for i , f := range callingFunctions () {
122
+ if f == fn {
123
123
return i
124
124
}
125
125
}
@@ -149,6 +149,29 @@ func TestContextStackDepth(t *testing.T) {
149
149
}
150
150
}
151
151
152
+ // callingFunctions returns the names of the functions on the call stack for the
153
+ // current goroutine with element 0 identifying the calling function.
154
+ func callingFunctions () []string {
155
+ pcs := make ([]uintptr , 10 )
156
+ n := runtime .Callers (2 , pcs )
157
+ if n == 0 {
158
+ return nil
159
+ }
160
+
161
+ frames := runtime .CallersFrames (pcs [:n ])
162
+ funcs := make ([]string , 0 , n )
163
+
164
+ for {
165
+ frame , more := frames .Next ()
166
+ funcs = append (funcs , frame .Function )
167
+ if ! more {
168
+ break
169
+ }
170
+ }
171
+
172
+ return funcs
173
+ }
174
+
152
175
// Test that With returns a Logger safe for concurrent use. This test
153
176
// validates that the stored logging context does not get corrupted when
154
177
// multiple clients concurrently log additional keyvals.
0 commit comments