Skip to content

Commit 71fa7d7

Browse files
authored
Simplify stack helpers and remove internal/stack package (#11)
1 parent 1befbfc commit 71fa7d7

File tree

2 files changed

+27
-42
lines changed

2 files changed

+27
-42
lines changed

internal/stack/stack.go

Lines changed: 0 additions & 38 deletions
This file was deleted.

log_test.go

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ package log_test
22

33
import (
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

1212
func TestContext(t *testing.T) {
@@ -108,7 +108,7 @@ func TestWithPrefixAndSuffix(t *testing.T) {
108108
// Valuers, regardless of how many times With has been called.
109109
func 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

Comments
 (0)