diff --git a/format/format.go b/format/format.go new file mode 100644 index 0000000..8d7d3b1 --- /dev/null +++ b/format/format.go @@ -0,0 +1,12 @@ +package format + +import ( + "bufio" + + "github.com/jeromer/syslogparser" +) + +type Format interface { + GetParser([]byte) syslogparser.LogParser + GetSplitFunc() bufio.SplitFunc +} diff --git a/format/format_test.go b/format/format_test.go new file mode 100644 index 0000000..b6c4129 --- /dev/null +++ b/format/format_test.go @@ -0,0 +1,13 @@ +package format + +import ( + "testing" + + . "launchpad.net/gocheck" +) + +func Test(t *testing.T) { TestingT(t) } + +type FormatSuite struct{} + +var _ = Suite(&FormatSuite{}) diff --git a/format/rfc3164.go b/format/rfc3164.go new file mode 100644 index 0000000..d132bee --- /dev/null +++ b/format/rfc3164.go @@ -0,0 +1,18 @@ +package format + +import ( + "bufio" + + "github.com/jeromer/syslogparser" + "github.com/jeromer/syslogparser/rfc3164" +) + +type RFC3164 struct{} + +func (f *RFC3164) GetParser(line []byte) syslogparser.LogParser { + return rfc3164.NewParser(line) +} + +func (f *RFC3164) GetSplitFunc() bufio.SplitFunc { + return nil +} diff --git a/format/rfc3164_test.go b/format/rfc3164_test.go new file mode 100644 index 0000000..7c322db --- /dev/null +++ b/format/rfc3164_test.go @@ -0,0 +1,10 @@ +package format + +import ( + . "launchpad.net/gocheck" +) + +func (s *FormatSuite) TestRFC3164_SingleSplit(c *C) { + f := RFC3164{} + c.Assert(f.GetSplitFunc(), IsNil) +} diff --git a/format/rfc5424.go b/format/rfc5424.go new file mode 100644 index 0000000..0468e88 --- /dev/null +++ b/format/rfc5424.go @@ -0,0 +1,18 @@ +package format + +import ( + "bufio" + + "github.com/jeromer/syslogparser" + "github.com/jeromer/syslogparser/rfc5424" +) + +type RFC5424 struct{} + +func (f *RFC5424) GetParser(line []byte) syslogparser.LogParser { + return rfc5424.NewParser(line) +} + +func (f *RFC5424) GetSplitFunc() bufio.SplitFunc { + return nil +} diff --git a/format/rfc5424_test.go b/format/rfc5424_test.go new file mode 100644 index 0000000..3509ac8 --- /dev/null +++ b/format/rfc5424_test.go @@ -0,0 +1,10 @@ +package format + +import ( + . "launchpad.net/gocheck" +) + +func (s *FormatSuite) TestRFC5424_SingleSplit(c *C) { + f := RFC5424{} + c.Assert(f.GetSplitFunc(), IsNil) +} diff --git a/format/rfc6587.go b/format/rfc6587.go new file mode 100644 index 0000000..24e9700 --- /dev/null +++ b/format/rfc6587.go @@ -0,0 +1,42 @@ +package format + +import ( + "bufio" + "bytes" + "strconv" + + "github.com/jeromer/syslogparser" + "github.com/jeromer/syslogparser/rfc5424" +) + +type RFC6587 struct{} + +func (f *RFC6587) GetParser(line []byte) syslogparser.LogParser { + return rfc5424.NewParser(line) +} + +func (f *RFC6587) GetSplitFunc() bufio.SplitFunc { + return rfc6587ScannerSplit +} + +func rfc6587ScannerSplit(data []byte, atEOF bool) (advance int, token []byte, err error) { + if atEOF && len(data) == 0 { + return 0, nil, nil + } + + if i := bytes.IndexByte(data, ' '); i > 0 { + pLength := data[0:i] + length, err := strconv.Atoi(string(pLength)) + if err != nil { + return 0, nil, err + } + end := length + i + 1 + if len(data) >= end { + //Return the frame with the length removed + return end, data[i+1 : end], nil + } + } + + // Request more data + return 0, nil, nil +} diff --git a/format/rfc6587_test.go b/format/rfc6587_test.go index e91e4b4..02052cf 100644 --- a/format/rfc6587_test.go +++ b/format/rfc6587_test.go @@ -5,25 +5,23 @@ import ( "bytes" "fmt" "strings" - "testing" + + . "launchpad.net/gocheck" ) -func TestRFC6587_GetSplitFuncSingleSplit(t *testing.T) { +func (s *FormatSuite) TestRFC6587_GetSplitFuncSingleSplit(c *C) { f := RFC6587{} - find := "I am test." - buf := strings.NewReader("10 " + find) + buf := strings.NewReader("10 I am test.") scanner := bufio.NewScanner(buf) scanner.Split(f.GetSplitFunc()) - if r := scanner.Scan(); !r { - t.Error("Expected Scan() to return true, but didn't") - } - if found := scanner.Text(); found != find { - t.Errorf("Expected the right ('%s') token, but got: '%s'\n", find, found) - } + + r := scanner.Scan() + c.Assert(r, NotNil) + c.Assert(scanner.Text(), Equals, "I am test.") } -func TestRFC6587_GetSplitFuncMultiSplit(t *testing.T) { +func (s *FormatSuite) TestRFC6587_GetSplitFuncMultiSplit(c *C) { f := RFC6587{} find := []string{ @@ -40,15 +38,14 @@ func TestRFC6587_GetSplitFuncMultiSplit(t *testing.T) { i := 0 for scanner.Scan() { + c.Assert(scanner.Text(), Equals, find[i]) i++ } - if i != len(find) { - t.Errorf("Expected to find %d items, but found: %d\n", len(find), i) - } + c.Assert(i, Equals, len(find)) } -func TestRFC6587_GetSplitBadSplit(t *testing.T) { +func (s *FormatSuite) TestRFC6587_GetSplitBadSplit(c *C) { f := RFC6587{} find := "I am test.2 ab" @@ -56,19 +53,13 @@ func TestRFC6587_GetSplitBadSplit(t *testing.T) { scanner := bufio.NewScanner(buf) scanner.Split(f.GetSplitFunc()) - if r := scanner.Scan(); !r { - t.Error("Expected Scan() to return true, but didn't") - } - if found := scanner.Text(); found != find[0:9] { - t.Errorf("Expected to find %s, but found %s.", find[0:9], found) - } - if r := scanner.Scan(); r { - t.Error("Expected Scan() to return false, but didn't") - } - if err := scanner.Err(); err == nil { - t.Error("Expected an error, but didn't get one") - } else { - t.Log("Error was: ", err) - } + r := scanner.Scan() + c.Assert(r, NotNil) + c.Assert(scanner.Text(), Equals, find[0:9]) + + r = scanner.Scan() + c.Assert(r, NotNil) + err := scanner.Err() + c.Assert(err, ErrorMatches, "strconv.ParseInt: parsing \".2\": invalid syntax") }