parser.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package liquidsoap
  2. import (
  3. "bufio"
  4. "errors"
  5. "fmt"
  6. "io"
  7. "regexp"
  8. "strconv"
  9. )
  10. // Output represents a liquidsoap message in a structured form
  11. type Output struct {
  12. Level int // lower means more important
  13. Msg string
  14. Component string
  15. }
  16. // Parse needs a reader of the log, and will produce parsed output
  17. func Parse(r io.Reader, parsed chan<- Output, onexit chan interface{}) {
  18. scanner := bufio.NewScanner(r)
  19. for scanner.Scan() {
  20. out, err := outParseLine(scanner.Text())
  21. if err == nil {
  22. parsed <- out
  23. }
  24. }
  25. onexit <- struct{}{}
  26. }
  27. var lineRegex = regexp.MustCompile(`^([0-9]{4}/[0-9]{2}/[0-9]{2}) ([0-9:]{8}) \[([^:\[\]]*):([0-9])\] (.*)$`)
  28. func outParseLine(line string) (Output, error) {
  29. res := lineRegex.FindStringSubmatch(line)
  30. if res == nil {
  31. return Output{}, errors.New("Invalid line")
  32. }
  33. if len(res) != 6 {
  34. return Output{}, errors.New("Invalid line: missing parts")
  35. }
  36. level, err := strconv.Atoi(res[4])
  37. if err != nil {
  38. return Output{}, fmt.Errorf("Invalid line: invalid level `%s`", res[4])
  39. }
  40. return Output{Level: level, Msg: res[5], Component: res[3]}, nil
  41. }