/
helper.go
50 lines (43 loc) · 1.08 KB
/
helper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package queryBuilder
import (
"github.com/sirupsen/logrus"
"runtime"
"strings"
)
// Frame is short format of runtime.Frame
type Frame struct {
Function string
File string
Line int
}
// GetFrames function for retrieve calling trace,
// can be used if you want write to logs calling trace
func (qb *QB) GetFrames() []Frame {
maxLenght := make([]uintptr, 99)
// skip firs 2 callers which is "runtime.Callers" and common.GetFrames
n := runtime.Callers(2, maxLenght)
var res []Frame
if n > 0 {
frames := runtime.CallersFrames(maxLenght[:n])
for more, frameIndex := true, 0; more; frameIndex++ {
var frameCandidate runtime.Frame
frameCandidate, more = frames.Next()
// skip tracing external dependencies
if !strings.Contains(frameCandidate.Function, qb.projectName) {
break
}
res = append(res, Frame{
Function: frameCandidate.Function,
File: frameCandidate.File,
Line: frameCandidate.Line,
})
}
}
return res
}
func initLogTrace(trace logrus.Fields) logrus.Fields {
if trace == nil {
return make(logrus.Fields)
}
return trace
}