github.com/oarkflow/log@v1.0.78/gelf/utils.go (about)

     1  package gelf
     2  
     3  import (
     4  	"runtime"
     5  	"strings"
     6  )
     7  
     8  // getCaller returns the filename and the line info of a function
     9  // further down in the call stack.  Passing 0 in as callDepth would
    10  // return info on the function calling getCallerIgnoringLog, 1 the
    11  // parent function, and so on.  Any suffixes passed to getCaller are
    12  // path fragments like "/pkg/log/log.go", and functions in the call
    13  // stack from that file are ignored.
    14  func getCaller(callDepth int, suffixesToIgnore ...string) (file string, line int) {
    15  	// bump by 1 to ignore the getCaller (this) stackframe
    16  	callDepth++
    17  outer:
    18  	for {
    19  		var ok bool
    20  		_, file, line, ok = runtime.Caller(callDepth)
    21  		if !ok {
    22  			file = "???"
    23  			line = 0
    24  			break
    25  		}
    26  
    27  		for _, s := range suffixesToIgnore {
    28  			if strings.HasSuffix(file, s) {
    29  				callDepth++
    30  				continue outer
    31  			}
    32  		}
    33  		break
    34  	}
    35  	return
    36  }
    37  
    38  func getCallerIgnoringLogMulti(callDepth int) (string, int) {
    39  	// the +1 is to ignore this (getCallerIgnoringLogMulti) frame
    40  	return getCaller(callDepth+1, "/pkg/log/log.go", "/pkg/io/multi.go")
    41  }