From 607301785358bec2b60c129b58380e993e2d661c Mon Sep 17 00:00:00 2001 From: Segev Finer Date: Thu, 23 Jul 2020 01:14:49 +0300 Subject: [PATCH] Change takeStacktrace skip parameter to a value --- field.go | 4 ++-- field_test.go | 2 +- stacktrace.go | 10 +++------- stacktrace_test.go | 4 ++-- 4 files changed, 8 insertions(+), 12 deletions(-) diff --git a/field.go b/field.go index be04f66c0..f6bb8a498 100644 --- a/field.go +++ b/field.go @@ -368,7 +368,7 @@ func Stack(key string) Field { // from expanding the zapcore.Field union struct to include a byte slice. Since // taking a stacktrace is already so expensive (~10us), the extra allocation // is okay. - return String(key, takeStacktrace(nil)) + return String(key, takeStacktrace(0)) } // StackSkip constructs a field that stores a stacktrace of the current @@ -381,7 +381,7 @@ func StackSkip(key string, skip int) Field { // from expanding the zapcore.Field union struct to include a byte slice. Since // taking a stacktrace is already so expensive (~10us), the extra allocation // is okay. - return String(key, takeStacktrace(&skip)) + return String(key, takeStacktrace(skip)) } // Duration constructs a field with the given key and value. The encoder diff --git a/field_test.go b/field_test.go index e45a1a95d..67a03a90d 100644 --- a/field_test.go +++ b/field_test.go @@ -259,6 +259,6 @@ func TestStackField(t *testing.T) { f := Stack("stacktrace") assert.Equal(t, "stacktrace", f.Key, "Unexpected field key.") assert.Equal(t, zapcore.StringType, f.Type, "Unexpected field type.") - assert.Equal(t, takeStacktrace(nil), f.String, "Unexpected stack trace") + assert.Equal(t, takeStacktrace(0), f.String, "Unexpected stack trace") assertCanBeReused(t, f) } diff --git a/stacktrace.go b/stacktrace.go index 801f9519b..a578e046c 100644 --- a/stacktrace.go +++ b/stacktrace.go @@ -43,7 +43,7 @@ var ( _zapStacktraceVendorContains = addPrefix("/vendor/", _zapStacktracePrefixes...) ) -func takeStacktrace(skip *int) string { +func takeStacktrace(skip int) string { buffer := bufferpool.Get() defer buffer.Free() programCounters := _stacktracePool.Get().(*programCounters) @@ -69,10 +69,6 @@ func takeStacktrace(skip *int) string { // Note: On the last iteration, frames.Next() returns false, with a valid // frame, but we ignore this frame. The last frame is a a runtime frame which // adds noise, since it's only either runtime.main or runtime.goexit. - toSkip := 0 - if skip != nil { - toSkip = *skip - } for frame, more := frames.Next(); more; frame, more = frames.Next() { if skipZapFrames && isZapFrame(frame.Function) { continue @@ -80,8 +76,8 @@ func takeStacktrace(skip *int) string { skipZapFrames = false } - if toSkip > 0 { - toSkip-- + if skip > 0 { + skip-- continue } diff --git a/stacktrace_test.go b/stacktrace_test.go index 7050bdc53..396e1edfd 100644 --- a/stacktrace_test.go +++ b/stacktrace_test.go @@ -29,7 +29,7 @@ import ( ) func TestTakeStacktrace(t *testing.T) { - trace := takeStacktrace(nil) + trace := takeStacktrace(0) lines := strings.Split(trace, "\n") require.True(t, len(lines) > 0, "Expected stacktrace to have at least one frame.") assert.Contains( @@ -70,6 +70,6 @@ func TestIsZapFrame(t *testing.T) { func BenchmarkTakeStacktrace(b *testing.B) { for i := 0; i < b.N; i++ { - takeStacktrace(nil) + takeStacktrace(0) } }