Slide 39
Slide 39 text
//go:generate bash -c "ragel -Z -G2 -o parser.go parser.rl && gofmt -s -w parser.go && gofmt
-s -w parser.go"
%%{
machine lineparser;
write data;
}%%
func parseLogLine(data []byte) ([][]byte, bool, bool) {
parts := make([][]byte, 8)
start := 0
pIdx := -1
cs, p, pe := 0, 0, len(data)
eof := pe
withMsg := false
withPID := false
%%{
action di { pIdx++; start = p }
action dd { parts[pIdx] = data[start:p] }
action msgok { withMsg = true }
action withpid { withPID = true }
main :=
'<' ( digit )+ >di %dd '>' space* ( any - space )+ >di %dd space+
( ( digit+ space digit+ ':' digit+ ':' digit+ ) >di %dd space+ )?
( any - space )+ >di %dd space+ ( any - space - '[' - ']' )+ >di %dd
('[' ( digit )+ >di %dd >withpid ']')? ':' space+ ( any )+ >di %dd >msgok;
write init;
write exec;
}%%
return parts, withMsg, withPID
}
https://github.com/tsuru/bs/blob/master/log/parser.rl