debug.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Package debug provides general debug feature management for Gio, including
  2. // the ability to toggle debug features using the GIODEBUG environment variable.
  3. package debug
  4. import (
  5. "fmt"
  6. "os"
  7. "strings"
  8. "sync"
  9. "sync/atomic"
  10. )
  11. const (
  12. debugVariable = "GIODEBUG"
  13. textSubsystem = "text"
  14. silentFeature = "silent"
  15. )
  16. // Text controls whether the text subsystem has debug logging enabled.
  17. var Text atomic.Bool
  18. var parseOnce sync.Once
  19. // Parse processes the current value of GIODEBUG. If it is unset, it does nothing.
  20. // Otherwise it process its value, printing usage info the stderr if the value is
  21. // not understood. Parse will be automatically invoked when the first application
  22. // window is created, allowing applications to manipulate GIODEBUG programmatically
  23. // before it is parsed.
  24. func Parse() {
  25. parseOnce.Do(func() {
  26. val, ok := os.LookupEnv(debugVariable)
  27. if !ok {
  28. return
  29. }
  30. print := false
  31. silent := false
  32. for _, part := range strings.Split(val, ",") {
  33. switch part {
  34. case textSubsystem:
  35. Text.Store(true)
  36. case silentFeature:
  37. silent = true
  38. default:
  39. print = true
  40. }
  41. }
  42. if print && !silent {
  43. fmt.Fprintf(os.Stderr,
  44. `Usage of %s:
  45. A comma-delimited list of debug subsystems to enable. Currently recognized systems:
  46. - %s: text debug info including system font resolution
  47. - %s: silence this usage message even if GIODEBUG contains invalid content
  48. `, debugVariable, textSubsystem, silentFeature)
  49. }
  50. })
  51. }