MCPcopy
hub / github.com/sirupsen/logrus / getCaller

Function getCaller

entry.go:182–216  ·  view source on GitHub ↗

getCaller retrieves the name of the first non-logrus calling function

()

Source from the content-addressed store, hash-verified

180
181// getCaller retrieves the name of the first non-logrus calling function
182func getCaller() *runtime.Frame {
183 // cache this package's fully-qualified name
184 callerInitOnce.Do(func() {
185 pcs := make([]uintptr, maximumCallerDepth)
186 _ = runtime.Callers(0, pcs)
187
188 // dynamic get the package name and the minimum caller depth
189 for i := 0; i < maximumCallerDepth; i++ {
190 funcName := runtime.FuncForPC(pcs[i]).Name()
191 if strings.Contains(funcName, "getCaller") {
192 logrusPackage = getPackageName(funcName)
193 break
194 }
195 }
196
197 minimumCallerDepth = knownLogrusFrames
198 })
199
200 // Restrict the lookback frames to avoid runaway lookups
201 pcs := make([]uintptr, maximumCallerDepth)
202 depth := runtime.Callers(minimumCallerDepth, pcs)
203 frames := runtime.CallersFrames(pcs[:depth])
204
205 for f, again := frames.Next(); again; f, again = frames.Next() {
206 pkg := getPackageName(f.Function)
207
208 // If the caller isn't part of this package, we're done
209 if pkg != logrusPackage {
210 return &f
211 }
212 }
213
214 // if we got here, we failed to find the caller's context
215 return nil
216}
217
218func (entry Entry) HasCaller() (has bool) {
219 return entry.Logger != nil &&

Callers 1

logMethod · 0.85

Calls 1

getPackageNameFunction · 0.85

Tested by

no test coverage detected