Skip to content

Commit

Permalink
Merge pull request mcuadros#31 from cezarsa/master
Browse files Browse the repository at this point in the history
Allow extensibility by not exposing types declared in internal pkgs
  • Loading branch information
mcuadros authored Jul 25, 2016
2 parents d3055b9 + e6dd18c commit 858a415
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 25 deletions.
9 changes: 4 additions & 5 deletions format/automatic.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"errors"
"strconv"

"gopkg.in/mcuadros/go-syslog.v2/internal/syslogparser"
"gopkg.in/mcuadros/go-syslog.v2/internal/syslogparser/rfc3164"
"gopkg.in/mcuadros/go-syslog.v2/internal/syslogparser/rfc5424"
)
Expand Down Expand Up @@ -57,20 +56,20 @@ func detect(data []byte) (detected int, err error) {
return detectedUnknown, nil
}

func (f *Automatic) GetParser(line []byte) syslogparser.LogParser {
func (f *Automatic) GetParser(line []byte) LogParser {
switch format, _ := detect(line); format {
case detectedRFC3164:
return rfc3164.NewParser(line)
return &parserWrapper{rfc3164.NewParser(line)}
case detectedRFC5424:
return rfc5424.NewParser(line)
return &parserWrapper{rfc5424.NewParser(line)}
default:
// If the line was an RFC6587 line, the splitter should already have removed the length,
// so one of the above two will be chosen if the line is correctly formed. However, it
// may have a second length illegally placed at the start, in which case the detector
// will return detectedRFC6587. The line may also simply be malformed after the length in
// which case we will have detectedUnknown. In this case we return the simplest parser so
// the illegally formatted line is properly handled
return rfc3164.NewParser(line)
return &parserWrapper{rfc3164.NewParser(line)}
}
}

Expand Down
19 changes: 18 additions & 1 deletion format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,28 @@ package format

import (
"bufio"
"time"

"gopkg.in/mcuadros/go-syslog.v2/internal/syslogparser"
)

type LogParts map[string]interface{}

type LogParser interface {
Parse() error
Dump() LogParts
Location(*time.Location)
}

type Format interface {
GetParser([]byte) syslogparser.LogParser
GetParser([]byte) LogParser
GetSplitFunc() bufio.SplitFunc
}

type parserWrapper struct {
syslogparser.LogParser
}

func (w *parserWrapper) Dump() LogParts {
return LogParts(w.LogParser.Dump())
}
5 changes: 2 additions & 3 deletions format/rfc3164.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ package format
import (
"bufio"

"gopkg.in/mcuadros/go-syslog.v2/internal/syslogparser"
"gopkg.in/mcuadros/go-syslog.v2/internal/syslogparser/rfc3164"
)

type RFC3164 struct{}

func (f *RFC3164) GetParser(line []byte) syslogparser.LogParser {
return rfc3164.NewParser(line)
func (f *RFC3164) GetParser(line []byte) LogParser {
return &parserWrapper{rfc3164.NewParser(line)}
}

func (f *RFC3164) GetSplitFunc() bufio.SplitFunc {
Expand Down
5 changes: 2 additions & 3 deletions format/rfc5424.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ package format
import (
"bufio"

"gopkg.in/mcuadros/go-syslog.v2/internal/syslogparser"
"gopkg.in/mcuadros/go-syslog.v2/internal/syslogparser/rfc5424"
)

type RFC5424 struct{}

func (f *RFC5424) GetParser(line []byte) syslogparser.LogParser {
return rfc5424.NewParser(line)
func (f *RFC5424) GetParser(line []byte) LogParser {
return &parserWrapper{rfc5424.NewParser(line)}
}

func (f *RFC5424) GetSplitFunc() bufio.SplitFunc {
Expand Down
5 changes: 2 additions & 3 deletions format/rfc6587.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ import (
"bytes"
"strconv"

"gopkg.in/mcuadros/go-syslog.v2/internal/syslogparser"
"gopkg.in/mcuadros/go-syslog.v2/internal/syslogparser/rfc5424"
)

type RFC6587 struct{}

func (f *RFC6587) GetParser(line []byte) syslogparser.LogParser {
return rfc5424.NewParser(line)
func (f *RFC6587) GetParser(line []byte) LogParser {
return &parserWrapper{rfc5424.NewParser(line)}
}

func (f *RFC6587) GetSplitFunc() bufio.SplitFunc {
Expand Down
10 changes: 4 additions & 6 deletions handler.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package syslog

import (
"gopkg.in/mcuadros/go-syslog.v2/internal/syslogparser"
"gopkg.in/mcuadros/go-syslog.v2/format"
)

type LogParts syslogparser.LogParts

//The handler receive every syslog entry at Handle method
type Handler interface {
Handle(LogParts, int64, error)
Handle(format.LogParts, int64, error)
}

type LogPartsChannel chan LogParts
type LogPartsChannel chan format.LogParts

//The ChannelHandler will send all the syslog entries into the given channel
type ChannelHandler struct {
Expand All @@ -32,6 +30,6 @@ func (h *ChannelHandler) SetChannel(channel LogPartsChannel) {
}

//Syslog entry receiver
func (h *ChannelHandler) Handle(logParts LogParts, messageLength int64, err error) {
func (h *ChannelHandler) Handle(logParts format.LogParts, messageLength int64, err error) {
h.channel <- logParts
}
3 changes: 2 additions & 1 deletion handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package syslog

import (
. "gopkg.in/check.v1"
"gopkg.in/mcuadros/go-syslog.v2/format"
)

type HandlerSuite struct{}

var _ = Suite(&HandlerSuite{})

func (s *HandlerSuite) TestHandle(c *C) {
logPart := LogParts{"tag": "foo"}
logPart := format.LogParts{"tag": "foo"}

channel := make(LogPartsChannel, 1)
handler := NewChannelHandler(channel)
Expand Down
2 changes: 1 addition & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ func (s *Server) parser(line []byte, client string, tlsPeer string) {
}
logParts["tls_peer"] = tlsPeer

s.handler.Handle(LogParts(logParts), int64(len(line)), err)
s.handler.Handle(logParts, int64(len(line)), err)
}

//Returns the last error
Expand Down
5 changes: 3 additions & 2 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

. "gopkg.in/check.v1"
"gopkg.in/mcuadros/go-syslog.v2/format"
)

func Test(t *testing.T) { TestingT(t) }
Expand Down Expand Up @@ -50,12 +51,12 @@ func (s *ServerSuite) TestTailFile(c *C) {
}

type HandlerMock struct {
LastLogParts LogParts
LastLogParts format.LogParts
LastMessageLength int64
LastError error
}

func (s *HandlerMock) Handle(logParts LogParts, msgLen int64, err error) {
func (s *HandlerMock) Handle(logParts format.LogParts, msgLen int64, err error) {
s.LastLogParts = logParts
s.LastMessageLength = msgLen
s.LastError = err
Expand Down

0 comments on commit 858a415

Please sign in to comment.