klog_file_windows.go 717 B

12345678910111213141516171819202122232425262728293031323334
  1. //go:build windows
  2. // +build windows
  3. package klog
  4. import (
  5. "os"
  6. "strings"
  7. )
  8. func getUserName() string {
  9. userNameOnce.Do(func() {
  10. // On Windows, the Go 'user' package requires netapi32.dll.
  11. // This affects Windows Nano Server:
  12. // https://github.com/golang/go/issues/21867
  13. // Fallback to using environment variables.
  14. u := os.Getenv("USERNAME")
  15. if len(u) == 0 {
  16. return
  17. }
  18. // Sanitize the USERNAME since it may contain filepath separators.
  19. u = strings.Replace(u, `\`, "_", -1)
  20. // user.Current().Username normally produces something like 'USERDOMAIN\USERNAME'
  21. d := os.Getenv("USERDOMAIN")
  22. if len(d) != 0 {
  23. userName = d + "_" + u
  24. } else {
  25. userName = u
  26. }
  27. })
  28. return userName
  29. }