Skip to content

Commit

Permalink
Simplify stack helpers and remove internal/stack package (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisHines committed Sep 17, 2021
1 parent 1befbfc commit 71fa7d7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 42 deletions.
38 changes: 0 additions & 38 deletions internal/stack/stack.go

This file was deleted.

31 changes: 27 additions & 4 deletions log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package log_test

import (
"bytes"
"runtime"
"sync"
"testing"

"github.com/go-kit/log"
"github.com/go-kit/log/internal/stack"
)

func TestContext(t *testing.T) {
Expand Down Expand Up @@ -108,7 +108,7 @@ func TestWithPrefixAndSuffix(t *testing.T) {
// Valuers, regardless of how many times With has been called.
func TestContextStackDepth(t *testing.T) {
t.Parallel()
fn := stack.Caller(0).Function
fn := callingFunctions()[0]

var output []interface{}

Expand All @@ -118,8 +118,8 @@ func TestContextStackDepth(t *testing.T) {
}))

stackValuer := log.Valuer(func() interface{} {
for i, f := range stack.Trace() {
if f.Function == fn {
for i, f := range callingFunctions() {
if f == fn {
return i
}
}
Expand Down Expand Up @@ -149,6 +149,29 @@ func TestContextStackDepth(t *testing.T) {
}
}

// callingFunctions returns the names of the functions on the call stack for the
// current goroutine with element 0 identifying the calling function.
func callingFunctions() []string {
pcs := make([]uintptr, 10)
n := runtime.Callers(2, pcs)
if n == 0 {
return nil
}

frames := runtime.CallersFrames(pcs[:n])
funcs := make([]string, 0, n)

for {
frame, more := frames.Next()
funcs = append(funcs, frame.Function)
if !more {
break
}
}

return funcs
}

// Test that With returns a Logger safe for concurrent use. This test
// validates that the stored logging context does not get corrupted when
// multiple clients concurrently log additional keyvals.
Expand Down

0 comments on commit 71fa7d7

Please sign in to comment.