@@ -2,11 +2,11 @@ package log_test
22
33import (
44 "bytes"
5+ "runtime"
56 "sync"
67 "testing"
78
89 "github.com/go-kit/log"
9- "github.com/go-kit/log/internal/stack"
1010)
1111
1212func TestContext (t * testing.T ) {
@@ -108,7 +108,7 @@ func TestWithPrefixAndSuffix(t *testing.T) {
108108// Valuers, regardless of how many times With has been called.
109109func TestContextStackDepth (t * testing.T ) {
110110 t .Parallel ()
111- fn := stack . Caller ( 0 ). Function
111+ fn := callingFunctions ()[ 0 ]
112112
113113 var output []interface {}
114114
@@ -118,8 +118,8 @@ func TestContextStackDepth(t *testing.T) {
118118 }))
119119
120120 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 {
123123 return i
124124 }
125125 }
@@ -149,6 +149,29 @@ func TestContextStackDepth(t *testing.T) {
149149 }
150150}
151151
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+
152175// Test that With returns a Logger safe for concurrent use. This test
153176// validates that the stored logging context does not get corrupted when
154177// multiple clients concurrently log additional keyvals.
0 commit comments