log.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package mint
  2. import (
  3. "fmt"
  4. "log"
  5. "os"
  6. "strings"
  7. )
  8. // We use this environment variable to control logging. It should be a
  9. // comma-separated list of log tags (see below) or "*" to enable all logging.
  10. const logConfigVar = "MINT_LOG"
  11. // Pre-defined log types
  12. const (
  13. logTypeCrypto = "crypto"
  14. logTypeHandshake = "handshake"
  15. logTypeNegotiation = "negotiation"
  16. logTypeIO = "io"
  17. logTypeFrameReader = "frame"
  18. logTypeVerbose = "verbose"
  19. )
  20. var (
  21. logFunction = log.Printf
  22. logAll = false
  23. logSettings = map[string]bool{}
  24. )
  25. func init() {
  26. parseLogEnv(os.Environ())
  27. }
  28. func parseLogEnv(env []string) {
  29. for _, stmt := range env {
  30. if strings.HasPrefix(stmt, logConfigVar+"=") {
  31. val := stmt[len(logConfigVar)+1:]
  32. if val == "*" {
  33. logAll = true
  34. } else {
  35. for _, t := range strings.Split(val, ",") {
  36. logSettings[t] = true
  37. }
  38. }
  39. }
  40. }
  41. }
  42. func logf(tag string, format string, args ...interface{}) {
  43. if logAll || logSettings[tag] {
  44. fullFormat := fmt.Sprintf("[%s] %s", tag, format)
  45. logFunction(fullFormat, args...)
  46. }
  47. }