Skip to content

Commit

Permalink
huge code cleaning and migration to go mod
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanaelle committed Apr 19, 2019
1 parent 37ed07a commit 30be0bf
Show file tree
Hide file tree
Showing 48 changed files with 635 additions and 505 deletions.
1 change: 0 additions & 1 deletion .GOPATH/src/github.com/nathanaelle/syslog5424

This file was deleted.

3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ language: go
go_import_path: github.com/nathanaelle/syslog5424
go:
- tip
- 1.9
- 1.8
- 1.12
branches:
except:
- /poc-.*/
Expand Down
1 change: 0 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"go.gopath": "${workspaceRoot}/.GOPATH",
"go.testOnSave": true,
"go.coverOnSave": true,
"go.inferGopath": false,
Expand Down
27 changes: 14 additions & 13 deletions burst_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ type burstTestOk struct {
}

//*
func Test_Burst(t *testing.T) {
func TestBurst(t *testing.T) {

seq := []burstTestOk{
{"u5425", "unix", T_RFC5425},
{"ulf", "unix", T_LFENDED},
{"uzero", "unix", T_ZEROENDED},
{"dlf", "unixgram", T_LFENDED},
{"dzero", "unixgram", T_ZEROENDED},
{"d5425", "unixgram", T_RFC5425},
{"u5425", "unix", TransportRFC5425},
{"ulf", "unix", TransportLFEnded},
{"uzero", "unix", TransportZeroEnded},
{"dlf", "unixgram", TransportLFEnded},
{"dzero", "unixgram", TransportZeroEnded},
{"d5425", "unixgram", TransportRFC5425},
}

for _, s := range seq {
Expand All @@ -41,6 +42,7 @@ func Test_Burst(t *testing.T) {
}

//*/

func burst(sock, n string, t Transport) (err error) {
defer os.Remove(sock)
os.Remove(sock)
Expand Down Expand Up @@ -84,13 +86,13 @@ func clientBurst(wg *sync.WaitGroup, mutex *sync.Mutex, sock, n string, t Transp
}
}()

syslog, err := New(slConn, LOG_DAEMON|LOG_WARNING, "client-app")
syslog, err := New(slConn, LogDAEMON|LogWARNING, "client-app")
if err != nil {
log.Fatalf("client New %q", err)
}
syslog.TestMode()

loggerErrorConf := syslog.Channel(LOG_ERR).Logger("ERR : ")
loggerErrorConf := syslog.Channel(LogERR).Logger("ERR : ")

for i := 0; i < count; i++ {
loggerErrorConf.Print(burstMessage)
Expand Down Expand Up @@ -129,7 +131,7 @@ func serverBurst(wg *sync.WaitGroup, mutex *sync.Mutex, sock, n string, t Transp
}
}

func Benchmark_Burst(b *testing.B) {
func BenchmarkBurst(b *testing.B) {
sock := burstSocket + "-bench"
defer os.Remove(sock)
os.Remove(sock)
Expand All @@ -144,14 +146,13 @@ func Benchmark_Burst(b *testing.B) {

mutex.Lock()
wg.Add(2)
go clientBurst(wg, mutex, sock, "unix", T_RFC5425, b.N+100)
go clientBurst(wg, mutex, sock, "unix", TransportRFC5425, b.N+100)

b.ResetTimer()
serverBurst(wg, mutex, sock, "unix", T_RFC5425, b.N)
serverBurst(wg, mutex, sock, "unix", TransportRFC5425, b.N)

wg.Wait()
mutex.Unlock()

return

}
16 changes: 8 additions & 8 deletions channel.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package syslog5424 // import "github.com/nathanaelle/syslog5424"
package syslog5424 // import "github.com/nathanaelle/syslog5424/v2"

import (
"io"
"log"
"time"

"github.com/nathanaelle/syslog5424/sdata"
"github.com/nathanaelle/syslog5424/v2/sdata"
)

var (
// Now permit to change the local default alias function for time.Now(). only usefull in case of test or debug
Now func() time.Time = time.Now
Now = time.Now
)

type (
Expand Down Expand Up @@ -61,7 +61,7 @@ func (d *trueChannel) AppName(sup string) Channel {
priority: d.priority,
hostname: d.hostname,
pid: d.pid,
appname: valid_app(appname),
appname: validApp(appname),
msgid: d.msgid,
output: d.output,
}}
Expand All @@ -73,14 +73,14 @@ func (d *trueChannel) Msgid(msgid string) Channel {
hostname: d.hostname,
pid: d.pid,
appname: d.appname,
msgid: valid_msgid(msgid),
msgid: validMsgid(msgid),
output: d.output,
}
}

func (c *msgChannel) Logger(prefix string) *log.Logger {
switch c.priority.Severity() {
case LOG_DEBUG:
case LogDEBUG:
return log.New(c, prefix, log.Lshortfile)
default:
return log.New(c, prefix, 0)
Expand All @@ -92,12 +92,12 @@ func (c *msgChannel) IsDevNull() bool {
}

func (c *msgChannel) Write(d []byte) (int, error) {
c.output.Send(forge_message(c.priority, Now(), c.hostname, c.appname, c.pid, c.msgid, string(d)))
c.output.Send(forgeMessage(c.priority, Now(), c.hostname, c.appname, c.pid, c.msgid, string(d)))
return len(d), nil
}

func (c *msgChannel) Log(d string, sd ...sdata.StructuredData) {
msg := forge_message(c.priority, Now(), c.hostname, c.appname, c.pid, c.msgid, string(d))
msg := forgeMessage(c.priority, Now(), c.hostname, c.appname, c.pid, c.msgid, string(d))

if len(sd) > 0 {
msg = msg.StructuredData(sd...)
Expand Down
6 changes: 3 additions & 3 deletions connector-fd.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package syslog5424 // import "github.com/nathanaelle/syslog5424"
package syslog5424 // import "github.com/nathanaelle/syslog5424/v2"

import (
"os"
Expand All @@ -7,7 +7,7 @@ import (
// StdioConnector returns a Connector that only forward to stderr: or stdout:
func StdioConnector(addr string) Connector {
if addr == "" {
return InvalidConnector{ErrorEmptyNetworkAddress}
return InvalidConnector{ErrEmptyNetworkAddress}
}

switch addr {
Expand All @@ -24,5 +24,5 @@ func StdioConnector(addr string) Connector {

// TODO implement file logging here

return InvalidConnector{ErrorInvalidAddress}
return InvalidConnector{ErrInvalidAddress}
}
2 changes: 1 addition & 1 deletion connector-invalid.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package syslog5424 // import "github.com/nathanaelle/syslog5424"
package syslog5424 // import "github.com/nathanaelle/syslog5424/v2"

type (
// InvalidConnector is a connector that always return an error
Expand Down
2 changes: 1 addition & 1 deletion connector-local.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package syslog5424 // import "github.com/nathanaelle/syslog5424"
package syslog5424 // import "github.com/nathanaelle/syslog5424/v2"

import (
"net"
Expand Down
2 changes: 1 addition & 1 deletion connector-local_other.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// +build !darwin,!linux,!freebsd,!netbsd,!openbsd,!dragonfly

package syslog5424 // import "github.com/nathanaelle/syslog5424"
package syslog5424 // import "github.com/nathanaelle/syslog5424/v2"

import (
"net"
Expand Down
18 changes: 13 additions & 5 deletions connector-local_unix.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// +build darwin linux freebsd netbsd openbsd dragonfly

package syslog5424 // import "github.com/nathanaelle/syslog5424"
package syslog5424 // import "github.com/nathanaelle/syslog5424/v2"

import (
"net"
Expand All @@ -12,24 +12,32 @@ func (c *localConn) osGuessConnnector() (*net.UnixConn, error) {

if c.address != "" {
for _, network := range logTypes {
conn, err := net.DialUnix(network, nil, &net.UnixAddr{c.address, network})
uAddr, err := net.ResolveUnixAddr(network, c.address)
if err != nil {
continue
}
conn, err := net.DialUnix(network, nil, uAddr)
if err == nil {
c.network = network
return conn, nil
}
}
return nil, ErrorNoConnecion
return nil, ErrNoConnecion
}

for _, network := range logTypes {
for _, path := range logPaths {
conn, err := net.DialUnix(network, nil, &net.UnixAddr{path, network})
uAddr, err := net.ResolveUnixAddr(network, path)
if err != nil {
continue
}
conn, err := net.DialUnix(network, nil, uAddr)
if err == nil {
c.network = network
c.address = path
return conn, nil
}
}
}
return nil, ErrorNoConnecion
return nil, ErrNoConnecion
}
10 changes: 5 additions & 5 deletions connector-tcp.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package syslog5424 // import "github.com/nathanaelle/syslog5424"
package syslog5424 // import "github.com/nathanaelle/syslog5424/v2"

import (
"context"
Expand All @@ -17,14 +17,14 @@ var resolver = &net.Resolver{
PreferGo: true,
}

// dialer that forward to a local RFC5424 syslog receiver
// TCPConnector is a dialer that forward to a local RFC5424 syslog receiver
func TCPConnector(network, address string) Connector {
if network == "" || address == "" {
return InvalidConnector{ErrorEmptyNetworkAddress}
return InvalidConnector{ErrEmptyNetworkAddress}
}

if network != "tcp" && network != "tcp4" && network != "tcp6" {
return InvalidConnector{ErrorInvalidNetwork}
return InvalidConnector{ErrInvalidNetwork}

}

Expand Down Expand Up @@ -72,7 +72,7 @@ func (c *tcpConn) Connect() (conn WriteCloser, err error) {

var contcp *net.TCPConn
for _, ip := range ips {
addr := &net.TCPAddr{ip.IP, port, ip.Zone}
addr := &net.TCPAddr{IP: ip.IP, Port: port, Zone: ip.Zone}
contcp, err = net.DialTCP(c.network, nil, addr)
if err == nil {
break
Expand Down
15 changes: 10 additions & 5 deletions connector.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package syslog5424 // import "github.com/nathanaelle/syslog5424"
package syslog5424 // import "github.com/nathanaelle/syslog5424/v2"

import (
"io"
Expand All @@ -10,13 +10,15 @@ import (
)

type (
// Connector describe the generic way to create a WriteCloser
Connector interface {
Connect() (WriteCloser, error)
}

// ConnectorFunc is an helper that convert an function to a Connector
ConnectorFunc func() (WriteCloser, error)

// generic interface describing a Connection
// WriteCloser is a generic interface describing a Connection
WriteCloser interface {
io.Writer
io.Closer
Expand All @@ -35,17 +37,19 @@ type (
queue *net.Buffers
}

// Addr see net.Addr
Addr struct {
network string
address string
}
)

// Connect implements Connector.Connect
func (f ConnectorFunc) Connect() (WriteCloser, error) {
return f()
}

// Create a new sender
// NewSender create a new sender
func NewSender(output Connector, transport Transport, ticker <-chan time.Time) (*Sender, <-chan error) {
s := &Sender{
endAsked: make(chan struct{}),
Expand Down Expand Up @@ -126,7 +130,7 @@ func (c *Sender) runQueue() {
}
}

// send a Message to the log_sender goroutine
// Send send a Message to the log_sender goroutine
func (c *Sender) Send(m Message) (err error) {
var msg []byte

Expand All @@ -142,7 +146,7 @@ func (c *Sender) Send(m Message) (err error) {
return
}

// terminate the log_sender goroutine
// End terminate the log_sender goroutine
func (c *Sender) End() {
close(c.endAsked)
<-c.endCompleted
Expand All @@ -152,6 +156,7 @@ func (a *Addr) String() string {
return a.network + "!" + a.address
}

// Network see net.Addr
func (a *Addr) Network() string {
return a.network
}
13 changes: 7 additions & 6 deletions dialer.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package syslog5424 // import "github.com/nathanaelle/syslog5424"
package syslog5424 // import "github.com/nathanaelle/syslog5424/v2"

import (
"errors"
"time"
)

type (
// Dialer contains options for connecting to an address.
Dialer struct {
// delay to flush the queue
FlushDelay time.Duration
Expand Down Expand Up @@ -44,25 +45,25 @@ func (d Dialer) Dial(network, address string, t Transport) (*Sender, <-chan erro
switch network {
case "stdio":
if t == nil {
t = T_LFENDED
t = TransportLFEnded
}
c = StdioConnector(address)

case "local":
if t == nil {
t = T_ZEROENDED
t = TransportZeroEnded
}
c = LocalConnector("", address)

case "unix", "unixgram":
if t == nil {
t = T_ZEROENDED
t = TransportZeroEnded
}
c = LocalConnector(network, address)

case "tcp", "tcp6", "tcp4":
if t == nil {
t = T_LFENDED
t = TransportLFEnded
}
c = TCPConnector(network, address)

Expand All @@ -71,7 +72,7 @@ func (d Dialer) Dial(network, address string, t Transport) (*Sender, <-chan erro
}

if c == nil {
return nil, nil, ErrorNoConnecion
return nil, nil, ErrNoConnecion
}

sndr, chanErr := NewSender(c, t, ticker)
Expand Down
Loading

0 comments on commit 30be0bf

Please sign in to comment.