callers_old.go 754 B

12345678910111213141516171819202122232425262728293031323334
  1. //go:build !go1.21
  2. package log
  3. import (
  4. "runtime/debug"
  5. "sync"
  6. )
  7. var (
  8. // The cached full import path for the true full import path for the main package in the current
  9. // binary.
  10. cachedMainPackagePath string
  11. cacheMainPackagePath sync.Once
  12. )
  13. // Returns the full import path for the main package in the current binary, through the Once guard.
  14. func mainPackagePath() string {
  15. cacheMainPackagePath.Do(initMainPackagePath)
  16. return cachedMainPackagePath
  17. }
  18. func initMainPackagePath() {
  19. cachedMainPackagePath = getMainPackagePath()
  20. }
  21. // Reads the build info to get the true full import path for the main package.
  22. func getMainPackagePath() string {
  23. info, ok := debug.ReadBuildInfo()
  24. if ok {
  25. return info.Path
  26. }
  27. return mainPackageFrameImport
  28. }