Skip to content

Commit 6022f09

Browse files
committed
added record option info stack.FunctionID
1 parent 09192ee commit 6022f09

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

internal/stack/function_id.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ func (id functionID) String() string {
1212
return string(id)
1313
}
1414

15-
func FunctionID(id string) Caller {
15+
func FunctionID(id string, opts ...recordOption) Caller {
1616
if id != "" {
1717
return functionID(id)
1818
}
1919

20-
return Call(1)
20+
return Call(1, opts...)
2121
}

internal/stack/record.go

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
)
1111

1212
type recordOptions struct {
13+
packageAlias string
1314
packagePath bool
1415
packageName bool
1516
structName bool
@@ -71,30 +72,41 @@ func PackagePath(b bool) recordOption {
7172
}
7273
}
7374

75+
func Package(alias string) recordOption {
76+
return func(opts *recordOptions) {
77+
opts.packageAlias = alias
78+
}
79+
}
80+
7481
var _ Caller = call{}
7582

7683
type call struct {
7784
function uintptr
7885
file string
7986
line int
87+
opts []recordOption
8088
}
8189

82-
func Call(depth int) (c call) {
90+
func Call(depth int, opts ...recordOption) (c call) {
8391
c.function, c.file, c.line, _ = runtime.Caller(depth + 1)
8492

93+
c.opts = opts
94+
8595
return c
8696
}
8797

8898
func (c call) Record(opts ...recordOption) string {
8999
optionsHolder := recordOptions{
90100
packagePath: true,
101+
packageAlias: "",
91102
packageName: true,
92103
structName: true,
93104
functionName: true,
94105
fileName: true,
95106
line: true,
96107
lambdas: true,
97108
}
109+
98110
for _, opt := range opts {
99111
if opt != nil {
100112
opt(&optionsHolder)
@@ -151,14 +163,22 @@ func buildRecordString(
151163
) string {
152164
buffer := xstring.Buffer()
153165
defer buffer.Free()
154-
if optionsHolder.packagePath {
155-
buffer.WriteString(fnDetails.pkgPath)
156-
}
157-
if optionsHolder.packageName {
158-
if buffer.Len() > 0 {
159-
buffer.WriteByte('/')
166+
if optionsHolder.packageAlias != "" {
167+
buffer.WriteString(optionsHolder.packageAlias)
168+
} else {
169+
if optionsHolder.packagePath {
170+
if optionsHolder.packageAlias != "" {
171+
buffer.WriteString(optionsHolder.packageAlias)
172+
} else {
173+
buffer.WriteString(fnDetails.pkgPath)
174+
}
175+
}
176+
if optionsHolder.packageName {
177+
if buffer.Len() > 0 {
178+
buffer.WriteByte('/')
179+
}
180+
buffer.WriteString(fnDetails.pkgName)
160181
}
161-
buffer.WriteString(fnDetails.pkgName)
162182
}
163183
if optionsHolder.structName && len(fnDetails.structName) > 0 {
164184
if buffer.Len() > 0 {
@@ -198,7 +218,7 @@ func buildRecordString(
198218
}
199219

200220
func (c call) String() string {
201-
return c.Record(Lambda(false), FileName(false))
221+
return c.Record(append(c.opts, Lambda(false), FileName(false))...)
202222
}
203223

204224
func Record(depth int, opts ...recordOption) string {

0 commit comments

Comments
 (0)