Skip to content

Commit 5fbaaf0

Browse files
author
Jérôme Renard
committed
Add DetectRFC()
1 parent 757a9da commit 5fbaaf0

File tree

3 files changed

+79
-1
lines changed

3 files changed

+79
-1
lines changed

README.md

+20
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,26 @@ You should see
7878
proc_id : -
7979
structured_data : [exampleSDID@32473 iut="3" eventSource="Application" eventID="1011"]
8080

81+
Detecting message format
82+
------------------------
83+
84+
You can use the `WhichRFC()` function. Like this:
85+
86+
b := []byte(`<165>1 2003-10-11T22:14:15.003Z ...`)
87+
rfc, err := syslogparser.DetectRFC(b)
88+
if err != nil {
89+
panic(err)
90+
}
91+
92+
switch rfc {
93+
case RFC_UNKNOWN:
94+
fmt.Println("unknown")
95+
case RFC_3164:
96+
fmt.Println("3164")
97+
case RFC_5424:
98+
fmt.Println("5424")
99+
}
100+
81101
Running tests
82102
-------------
83103

syslogparser.go

+32
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ var (
3030
ErrTimestampUnknownFormat = &ParserError{"Timestamp format unknown"}
3131
)
3232

33+
type RFC uint8
34+
35+
const (
36+
RFC_UNKNOWN = iota
37+
RFC_3164
38+
RFC_5424
39+
)
40+
3341
type LogParser interface {
3442
Parse() error
3543
Dump() LogParts
@@ -204,3 +212,27 @@ func ShowCursorPos(buff []byte, cursor int) {
204212
func (err *ParserError) Error() string {
205213
return err.ErrorString
206214
}
215+
216+
func DetectRFC(buff []byte) (RFC, error) {
217+
max := 10
218+
var v int
219+
var err error
220+
221+
for i := 0; i < max; i++ {
222+
if buff[i] == '>' && i < max {
223+
x := i + 1
224+
v, err = ParseVersion(buff, &x, max)
225+
break
226+
}
227+
}
228+
229+
if err != nil {
230+
return RFC_UNKNOWN, err
231+
}
232+
233+
if v == NO_VERSION {
234+
return RFC_3164, nil
235+
}
236+
237+
return RFC_5424, nil
238+
}

syslogparser_test.go

+27-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package syslogparser
22

33
import (
4-
. "gopkg.in/check.v1"
54
"testing"
5+
6+
. "gopkg.in/check.v1"
67
)
78

89
// Hooks up gocheck into the gotest runner.
@@ -122,6 +123,20 @@ func (s *CommonTestSuite) TestParseHostname_Valid(c *C) {
122123
s.assertHostname(c, hostname, buff, start, len(hostname), nil)
123124
}
124125

126+
func (s *CommonTestSuite) TestDetectRFC_3164(c *C) {
127+
p, err := DetectRFC([]byte("<34>Oct 11 22:14:15 ..."))
128+
129+
c.Assert(err, Equals, nil)
130+
c.Assert(p, Equals, RFC(RFC_3164))
131+
}
132+
133+
func (s *CommonTestSuite) TestDetectRFC_5424(c *C) {
134+
p, err := DetectRFC([]byte("<165>1 2003-10-11T22:14:15.003Z ..."))
135+
136+
c.Assert(err, Equals, nil)
137+
c.Assert(p, Equals, RFC(RFC_5424))
138+
}
139+
125140
func (s *CommonTestSuite) BenchmarkParsePriority(c *C) {
126141
buff := []byte("<190>")
127142
var start int
@@ -150,6 +165,17 @@ func (s *CommonTestSuite) BenchmarkParseVersion(c *C) {
150165
}
151166
}
152167

168+
func (s *CommonTestSuite) BenchmarkDetectRFC(c *C) {
169+
buff := []byte("<165>1 2003-10-11T22:14:15.003Z ...")
170+
171+
for i := 0; i < c.N; i++ {
172+
_, err := DetectRFC(buff)
173+
if err != nil {
174+
panic(err)
175+
}
176+
}
177+
}
178+
153179
func (s *CommonTestSuite) assertPriority(c *C, p Priority, b []byte, cursor int, expC int, e error) {
154180
obtained, err := ParsePriority(b, &cursor, len(b))
155181
c.Assert(obtained, DeepEquals, p)

0 commit comments

Comments
 (0)