dsn.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package log
  2. import (
  3. "bytes"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "go-common/library/conf/dsn"
  8. "github.com/pkg/errors"
  9. )
  10. type verboseModule map[string]int32
  11. type logFilter []string
  12. func (f *logFilter) String() string {
  13. return fmt.Sprint(*f)
  14. }
  15. // Set sets the value of the named command-line flag.
  16. // format: -log.filter key1,key2
  17. func (f *logFilter) Set(value string) error {
  18. for _, i := range strings.Split(value, ",") {
  19. *f = append(*f, strings.TrimSpace(i))
  20. }
  21. return nil
  22. }
  23. func (m verboseModule) String() string {
  24. // FIXME strings.Builder
  25. var buf bytes.Buffer
  26. for k, v := range m {
  27. buf.WriteString(k)
  28. buf.WriteString(strconv.FormatInt(int64(v), 10))
  29. buf.WriteString(",")
  30. }
  31. return buf.String()
  32. }
  33. // Set sets the value of the named command-line flag.
  34. // format: -log.module file=1,file2=2
  35. func (m verboseModule) Set(value string) error {
  36. for _, i := range strings.Split(value, ",") {
  37. kv := strings.Split(i, "=")
  38. if len(kv) == 2 {
  39. if v, err := strconv.ParseInt(kv[1], 10, 64); err == nil {
  40. m[strings.TrimSpace(kv[0])] = int32(v)
  41. }
  42. }
  43. }
  44. return nil
  45. }
  46. // parseDSN parse log agent dsn.
  47. // unixgram:///var/run/lancer/collector.sock?timeout=100ms&chan=1024
  48. func parseDSN(rawdsn string) *AgentConfig {
  49. ac := new(AgentConfig)
  50. d, err := dsn.Parse(rawdsn)
  51. if err != nil {
  52. panic(errors.WithMessage(err, fmt.Sprintf("log: invalid dsn: %s", rawdsn)))
  53. }
  54. if _, err = d.Bind(ac); err != nil {
  55. panic(errors.WithMessage(err, fmt.Sprintf("log: invalid dsn: %s", rawdsn)))
  56. }
  57. return ac
  58. }