rfc.go 997 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package formatter
  2. import (
  3. "errors"
  4. syslog "gopkg.in/mcuadros/go-syslog.v2"
  5. "gopkg.in/mcuadros/go-syslog.v2/format"
  6. )
  7. // SyslogRFC is the formatter that the server should use
  8. type SyslogRFC struct{ format.Format }
  9. func (rfc *SyslogRFC) Set(v string) error {
  10. newval, err := parseRFCValue(v)
  11. if err != nil {
  12. return err
  13. }
  14. rfc.Format = newval
  15. return nil
  16. }
  17. func (rfc *SyslogRFC) String() string {
  18. switch {
  19. case rfc.Format == syslog.Automatic:
  20. return "auto"
  21. case rfc.Format == syslog.RFC3164:
  22. return "rfc3164"
  23. case rfc.Format == syslog.RFC5424:
  24. return "rfc5424"
  25. }
  26. return ""
  27. }
  28. func parseRFCValue(v string) (format.Format, error) {
  29. switch {
  30. case v == "rfc3164":
  31. return syslog.RFC3164, nil
  32. case v == "rfc5424":
  33. return syslog.RFC5424, nil
  34. case v == "auto":
  35. return syslog.Automatic, nil
  36. default:
  37. return nil, ErrRFCNotSupported
  38. }
  39. }
  40. // ErrRFCNotSupported is raised if the supplied rfc string is
  41. // not recognized.
  42. var ErrRFCNotSupported = errors.New("RFC not known")