@@ -2,48 +2,39 @@ package syslog
22
33import (
44 "bufio"
5- "bytes"
65 "errors"
76 "net"
8- "strconv"
97 "sync"
10-
118 "time"
129
13- "github.com/jeromer/syslogparser"
14- "github.com/jeromer/syslogparser/rfc3164"
15- "github.com/jeromer/syslogparser/rfc5424"
10+ "github.com/mcuadros/go-syslog/format"
1611)
1712
18- type Format int
19-
20- const (
21- RFC3164 Format = 1 + iota // RFC3164: http://www.ietf.org/rfc/rfc3164.txt
22- RFC5424 // RFC5424: http://www.ietf.org/rfc/rfc5424.txt
23- RFC6587 // RFC6587: http://www.ietf.org/rfc/rfc6587.txt
13+ var (
14+ RFC3164 = & format.RFC3164 {} // RFC3164: http://www.ietf.org/rfc/rfc3164.txt
15+ RFC5424 = & format.RFC5424 {} // RFC5424: http://www.ietf.org/rfc/rfc5424.txt
16+ RFC6587 = & format.RFC6587 {} // RFC6587: http://www.ietf.org/rfc/rfc6587.txt
2417)
2518
2619type Server struct {
2720 listeners []* net.TCPListener
2821 connections []net.Conn
2922 wait sync.WaitGroup
3023 doneTcp chan bool
31- format Format
24+ format format. Format
3225 handler Handler
3326 lastError error
3427 readTimeoutMilliseconds int64
3528}
3629
3730//NewServer returns a new Server
3831func NewServer () * Server {
39- server := new (Server )
40-
41- return server
32+ return & Server {}
4233}
4334
4435//Sets the syslog format (RFC3164 or RFC5424 or RFC6587)
45- func (self * Server ) SetFormat (format Format ) {
46- self .format = format
36+ func (self * Server ) SetFormat (f format. Format ) {
37+ self .format = f
4738}
4839
4940//Sets the handler, this handler with receive every syslog entry
@@ -107,7 +98,7 @@ func (self *Server) ListenTCP(addr string) error {
10798
10899//Starts the server, all the go routines goes to live
109100func (self * Server ) Boot () error {
110- if self .format == 0 {
101+ if self .format == nil {
111102 return errors .New ("please set a valid format" )
112103 }
113104
@@ -148,43 +139,10 @@ func (self *Server) goAcceptConnection(listener *net.TCPListener) {
148139 }(listener )
149140}
150141
151- type TimeoutCloser interface {
152- Close () error
153- SetReadDeadline (t time.Time ) error
154- }
155-
156- type ScanCloser struct {
157- * bufio.Scanner
158- closer TimeoutCloser
159- }
160-
161- func rfc6587ScannerSplit (data []byte , atEOF bool ) (advance int , token []byte , err error ) {
162- if atEOF && len (data ) == 0 {
163- return 0 , nil , nil
164- }
165-
166- if i := bytes .IndexByte (data , ' ' ); i > 0 {
167- pLength := data [0 :i ]
168- length , err := strconv .Atoi (string (pLength ))
169- if err != nil {
170- return 0 , nil , err
171- }
172- end := length + i + 1
173- if len (data ) >= end {
174- //Return the frame with the length removed
175- return end , data [i + 1 : end ], nil
176- }
177- }
178-
179- // Request more data
180- return 0 , nil , nil
181- }
182-
183142func (self * Server ) goScanConnection (connection net.Conn , needClose bool ) {
184143 scanner := bufio .NewScanner (connection )
185- switch self .format {
186- case RFC6587 :
187- scanner .Split (rfc6587ScannerSplit )
144+ if sf := self .format .GetSplitFunc (); sf != nil {
145+ scanner .Split (sf )
188146 }
189147
190148 var scanCloser * ScanCloser
@@ -229,15 +187,7 @@ func (self *Server) scan(scanCloser *ScanCloser) {
229187}
230188
231189func (self * Server ) parser (line []byte ) {
232- var parser syslogparser.LogParser
233-
234- switch self .format {
235- case RFC3164 :
236- parser = self .getParserRFC3164 (line )
237- case RFC5424 , RFC6587 :
238- parser = self .getParserRFC5424 (line )
239- }
240-
190+ parser := self .format .GetParser (line )
241191 err := parser .Parse ()
242192 if err != nil {
243193 self .lastError = err
@@ -246,18 +196,6 @@ func (self *Server) parser(line []byte) {
246196 go self .handler .Handle (parser .Dump (), int64 (len (line )), err )
247197}
248198
249- func (self * Server ) getParserRFC3164 (line []byte ) * rfc3164.Parser {
250- parser := rfc3164 .NewParser (line )
251-
252- return parser
253- }
254-
255- func (self * Server ) getParserRFC5424 (line []byte ) * rfc5424.Parser {
256- parser := rfc5424 .NewParser (line )
257-
258- return parser
259- }
260-
261199//Returns the last error
262200func (self * Server ) GetLastError () error {
263201 return self .lastError
@@ -288,3 +226,13 @@ func (self *Server) Kill() error {
288226func (self * Server ) Wait () {
289227 self .wait .Wait ()
290228}
229+
230+ type TimeoutCloser interface {
231+ Close () error
232+ SetReadDeadline (t time.Time ) error
233+ }
234+
235+ type ScanCloser struct {
236+ * bufio.Scanner
237+ closer TimeoutCloser
238+ }
0 commit comments