debug.go 996 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package opts
  2. import (
  3. "os"
  4. "strings"
  5. )
  6. // The SQLCDEBUG variable controls debugging variables within the runtime. It
  7. // is a comma-separated list of name=val pairs setting these named variables:
  8. //
  9. // dumpast: setting dumpast=1 will print the AST of every SQL statement
  10. // dumpcatalog: setting dumpcatalog=1 will print the parsed database schema
  11. // trace: setting trace=<path> will output a trace
  12. type Debug struct {
  13. DumpAST bool
  14. DumpCatalog bool
  15. Trace string
  16. }
  17. func DebugFromEnv() Debug {
  18. d := Debug{}
  19. val := os.Getenv("SQLCDEBUG")
  20. if val == "" {
  21. return d
  22. }
  23. for _, pair := range strings.Split(val, ",") {
  24. pair = strings.TrimSpace(pair)
  25. switch {
  26. case pair == "dumpast=1":
  27. d.DumpAST = true
  28. case pair == "dumpcatalog=1":
  29. d.DumpCatalog = true
  30. case strings.HasPrefix(pair, "trace="):
  31. traceName := strings.TrimPrefix(pair, "trace=")
  32. if traceName == "1" {
  33. d.Trace = "trace.out"
  34. } else {
  35. d.Trace = traceName
  36. }
  37. }
  38. }
  39. return d
  40. }