caller.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package hooks
  15. import (
  16. "fmt"
  17. "runtime"
  18. "strings"
  19. "sync"
  20. "github.com/sirupsen/logrus"
  21. )
  22. const callerDelim = "log"
  23. type CallerHook struct {
  24. UpHookPakcageName string
  25. }
  26. func isInvokeStep(fun, upfun string) bool {
  27. if strings.Contains(fun, callerDelim) && !strings.Contains(upfun, callerDelim) {
  28. return true
  29. }
  30. return false
  31. }
  32. func (hook *CallerHook) GetCallFields() string {
  33. var depth int
  34. for i := 10; i <= 10; i++ {
  35. pc, _, _, ok := runtime.Caller(i)
  36. pcUp, _, _, ok := runtime.Caller(i + 1)
  37. if !ok {
  38. return "???"
  39. }
  40. funcName := runtime.FuncForPC(pc).Name()
  41. upFuncName := runtime.FuncForPC(pcUp).Name()
  42. // fmt.Printf("===debug: %d->%q: fname: %q, upName: %q\n", i, i+1, funcName, upFuncName)
  43. if isInvokeStep(funcName, upFuncName) {
  44. depth = i + 1
  45. break
  46. }
  47. }
  48. pc, file, line, ok := runtime.Caller(depth)
  49. if !ok {
  50. return "???"
  51. }
  52. funcName := runtime.FuncForPC(pc).Name()
  53. funcName = funcName[strings.LastIndex(funcName, "/")+1:]
  54. file = file[strings.LastIndex(file, "/")+1:]
  55. return fmt.Sprintf("%v(%v:%v)", funcName, file, line)
  56. }
  57. var (
  58. // Used for caller information initialisation
  59. callerInitOnce sync.Once
  60. // logrusPackage string
  61. knownLogrusFrames int = 9
  62. minimumCallerDepth int = knownLogrusFrames
  63. maximumCallerDepth int = 25
  64. )
  65. // getPackageName reduces a fully qualified function name to the package name
  66. // There really ought to be to be a better way...
  67. func GetPackageName(f string) string {
  68. for {
  69. lastPeriod := strings.LastIndex(f, ".")
  70. lastSlash := strings.LastIndex(f, "/")
  71. if lastPeriod > lastSlash {
  72. f = f[:lastPeriod]
  73. } else {
  74. break
  75. }
  76. }
  77. return f
  78. }
  79. func (hook *CallerHook) getCaller() string {
  80. pcs := make([]uintptr, maximumCallerDepth)
  81. depth := runtime.Callers(minimumCallerDepth, pcs)
  82. frames := runtime.CallersFrames(pcs[:depth])
  83. var meetUpLogPacket bool
  84. for f, again := frames.Next(); again; f, again = frames.Next() {
  85. pkg := GetPackageName(f.Function)
  86. if !meetUpLogPacket {
  87. if pkg == hook.UpHookPakcageName {
  88. meetUpLogPacket = true
  89. }
  90. } else {
  91. funcName := f.Function[strings.LastIndex(f.Function, "/")+1:]
  92. file := f.File[strings.LastIndex(f.File, "/")+1:]
  93. return fmt.Sprintf("%v(%v:%v)", funcName, file, f.Line)
  94. }
  95. }
  96. return "???"
  97. }
  98. func (hook *CallerHook) Fire(entry *logrus.Entry) error {
  99. entry.Data["caller"] = hook.getCaller()
  100. return nil
  101. }
  102. func (hook *CallerHook) Levels() []logrus.Level {
  103. return logrus.AllLevels
  104. }