direttoforo/liquidsoap/parser.go
2017-09-07 18:30:45 +02:00

46 baris
1,1 KiB
Go

package liquidsoap
import (
"bufio"
"errors"
"fmt"
"io"
"regexp"
"strconv"
)
// Output represents a liquidsoap message in a structured form
type Output struct {
Level int // lower means more important
Msg string
Component string
}
// Parse needs a reader of the log, and will produce parsed output
func Parse(r io.Reader, parsed chan<- Output, onexit chan interface{}) {
scanner := bufio.NewScanner(r)
for scanner.Scan() {
out, err := outParseLine(scanner.Text())
if err == nil {
parsed <- out
}
}
onexit <- struct{}{}
}
var lineRegex = regexp.MustCompile(`^([0-9]{4}/[0-9]{2}/[0-9]{2}) ([0-9:]{8}) \[([^:\[\]]*):([0-9])\] (.*)$`)
func outParseLine(line string) (Output, error) {
res := lineRegex.FindStringSubmatch(line)
if res == nil {
return Output{}, errors.New("Invalid line")
}
if len(res) != 6 {
return Output{}, errors.New("Invalid line: missing parts")
}
level, err := strconv.Atoi(res[4])
if err != nil {
return Output{}, fmt.Errorf("Invalid line: invalid level `%s`", res[4])
}
return Output{Level: level, Msg: res[5], Component: res[3]}, nil
}