Skip to content

Commit

Permalink
remove the stack library dependency (#10)
Browse files Browse the repository at this point in the history
Signed-off-by: Mark Sagi-Kazar <mark.sagikazar@gmail.com>
  • Loading branch information
sagikazarmark authored Sep 16, 2021
1 parent ad0641e commit 1befbfc
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 11 deletions.
5 changes: 1 addition & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,4 @@ module github.com/go-kit/log

go 1.17

require (
github.com/go-logfmt/logfmt v0.5.1
github.com/go-stack/stack v1.8.1
)
require github.com/go-logfmt/logfmt v0.5.1
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
github.com/go-logfmt/logfmt v0.5.1 h1:otpy5pqBCBZ1ng9RQ0dPu4PN7ba75Y/aA+UpowDyNVA=
github.com/go-logfmt/logfmt v0.5.1/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
github.com/go-stack/stack v1.8.1 h1:ntEHSVwIt7PNXNpgPmVfMrNhLtgjlmnZha2kOpuRiDw=
github.com/go-stack/stack v1.8.1/go.mod h1:dcoOX6HbPZSZptuspn9bctJ+N/CnF5gGygcUP3XYfe4=
38 changes: 38 additions & 0 deletions internal/stack/stack.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package stack

import "runtime"

// Caller returns a frame from the stack of the current goroutine. The argument
// skip is the number of frames to ascend, with 0 identifying the
// calling function.
func Caller(skip int) runtime.Frame {
var pcs [3]uintptr

n := runtime.Callers(skip+1, pcs[:])
frames := runtime.CallersFrames(pcs[:n])
frame, _ := frames.Next()
frame, _ = frames.Next()

return frame
}

// Trace returns a call stack for the current goroutine with element 0
// identifying the calling function.
func Trace() []runtime.Frame {
var pcs [512]uintptr
n := runtime.Callers(1, pcs[:])

frames := runtime.CallersFrames(pcs[:n])
cs := make([]runtime.Frame, 0, n)

// Skip extra frame retrieved just to make sure the runtime.sigpanic
// special case is handled.
frame, more := frames.Next()

for more {
frame, more = frames.Next()
cs = append(cs, frame)
}

return cs
}
9 changes: 4 additions & 5 deletions log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ package log_test

import (
"bytes"
"fmt"
"sync"
"testing"

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

func TestContext(t *testing.T) {
Expand Down Expand Up @@ -109,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 := fmt.Sprintf("%n", stack.Caller(0))
fn := stack.Caller(0).Function

var output []interface{}

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

stackValuer := log.Valuer(func() interface{} {
for i, c := range stack.Trace() {
if fmt.Sprintf("%n", c) == fn {
for i, f := range stack.Trace() {
if f.Function == fn {
return i
}
}
Expand Down

0 comments on commit 1befbfc

Please sign in to comment.