utils.go 1.0 KB

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