github.com/utopiagio/gio@v0.0.8/internal/debug/debug.go (about)

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