Skip to content
This repository has been archived by the owner on Mar 18, 2019. It is now read-only.

Commit

Permalink
Add DialFunc type in place of repeating dial function signature
Browse files Browse the repository at this point in the history
  • Loading branch information
iaburton committed Jul 9, 2018
1 parent 8f8d82c commit 2a1fc6b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
18 changes: 11 additions & 7 deletions srslog.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ type serverConn interface {
close() error
}

// DialFunc is the function signature to be used for a custom dialer callback
// with DialWithCustomDialer
type DialFunc func(string, string) (net.Conn, error)

// New establishes a new connection to the system log daemon. Each
// write to the returned Writer sends a log message with the given
// priority and prefix.
Expand All @@ -32,12 +36,12 @@ func Dial(network, raddr string, priority Priority, tag string) (*Writer, error)
return DialWithTLSConfig(network, raddr, priority, tag, nil)
}

// DialWithCustomDialer establishes a connection by calling cdialer.
// DialWithCustomDialer establishes a connection by calling customDial.
// Each write to the returned Writer sends a log message with the given facility, severity and tag.
// While network and raddr will be passed to cdialer, it is allowed for cdialer to ignore them.
// If cdialer is nil, this function behaves like Dial.
func DialWithCustomDialer(network, raddr string, priority Priority, tag string, cdialer func(string, string) (net.Conn, error)) (*Writer, error) {
return dialAllParameters(network, raddr, priority, tag, nil, cdialer)
// While network and raddr will be passed to customDial, it is allowed for customDial to ignore them.
// If customDial is nil, this function behaves like Dial.
func DialWithCustomDialer(network, raddr string, priority Priority, tag string, customDial DialFunc) (*Writer, error) {
return dialAllParameters(network, raddr, priority, tag, nil, customDial)
}

// DialWithTLSCertPath establishes a secure connection to a log daemon by connecting to
Expand Down Expand Up @@ -72,7 +76,7 @@ func DialWithTLSConfig(network, raddr string, priority Priority, tag string, tls
}

// implementation of the various functions above
func dialAllParameters(network, raddr string, priority Priority, tag string, tlsConfig *tls.Config, cdialer func(string, string) (net.Conn, error)) (*Writer, error) {
func dialAllParameters(network, raddr string, priority Priority, tag string, tlsConfig *tls.Config, customDial DialFunc) (*Writer, error) {
if err := validatePriority(priority); err != nil {
return nil, err
}
Expand All @@ -89,7 +93,7 @@ func dialAllParameters(network, raddr string, priority Priority, tag string, tls
network: network,
raddr: raddr,
tlsConfig: tlsConfig,
customDial: cdialer,
customDial: customDial,
}

_, err := w.connect()
Expand Down
3 changes: 1 addition & 2 deletions writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package srslog

import (
"crypto/tls"
"net"
"strings"
"sync"
)
Expand All @@ -19,7 +18,7 @@ type Writer struct {
formatter Formatter

//non-nil if custom dialer set, used in getDialer
customDial func(string, string) (net.Conn, error)
customDial DialFunc

mu sync.RWMutex // guards conn
conn serverConn
Expand Down

0 comments on commit 2a1fc6b

Please sign in to comment.