|
@@ -3,8 +3,10 @@ package srslog
|
|
import (
|
|
import (
|
|
"crypto/tls"
|
|
"crypto/tls"
|
|
"crypto/x509"
|
|
"crypto/x509"
|
|
|
|
+ "errors"
|
|
"io/ioutil"
|
|
"io/ioutil"
|
|
"log"
|
|
"log"
|
|
|
|
+ "net"
|
|
"os"
|
|
"os"
|
|
)
|
|
)
|
|
|
|
|
|
@@ -15,6 +17,10 @@ type serverConn interface {
|
|
close() error
|
|
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
|
|
// New establishes a new connection to the system log daemon. Each
|
|
// write to the returned Writer sends a log message with the given
|
|
// write to the returned Writer sends a log message with the given
|
|
// priority and prefix.
|
|
// priority and prefix.
|
|
@@ -31,6 +37,22 @@ func Dial(network, raddr string, priority Priority, tag string) (*Writer, error)
|
|
return DialWithTLSConfig(network, raddr, priority, tag, nil)
|
|
return DialWithTLSConfig(network, raddr, priority, tag, nil)
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// ErrNilDialFunc is returned from DialWithCustomDialer when a nil DialFunc is passed,
|
|
|
|
+// avoiding a nil pointer deference panic.
|
|
|
|
+var ErrNilDialFunc = errors.New("srslog: nil DialFunc passed to DialWithCustomDialer")
|
|
|
|
+
|
|
|
|
+// DialWithCustomDialer establishes a connection by calling customDial.
|
|
|
|
+// Each write to the returned Writer sends a log message with the given facility, severity and tag.
|
|
|
|
+// Network must be "custom" in order for this package to use customDial.
|
|
|
|
+// While network and raddr will be passed to customDial, it is allowed for customDial to ignore them.
|
|
|
|
+// If customDial is nil, this function returns ErrNilDialFunc.
|
|
|
|
+func DialWithCustomDialer(network, raddr string, priority Priority, tag string, customDial DialFunc) (*Writer, error) {
|
|
|
|
+ if customDial == nil {
|
|
|
|
+ return nil, ErrNilDialFunc
|
|
|
|
+ }
|
|
|
|
+ return dialAllParameters(network, raddr, priority, tag, nil, customDial)
|
|
|
|
+}
|
|
|
|
+
|
|
// DialWithTLSCertPath establishes a secure connection to a log daemon by connecting to
|
|
// DialWithTLSCertPath establishes a secure connection to a log daemon by connecting to
|
|
// address raddr on the specified network. It uses certPath to load TLS certificates and configure
|
|
// address raddr on the specified network. It uses certPath to load TLS certificates and configure
|
|
// the secure connection.
|
|
// the secure connection.
|
|
@@ -59,6 +81,11 @@ func DialWithTLSCert(network, raddr string, priority Priority, tag string, serve
|
|
// DialWithTLSConfig establishes a secure connection to a log daemon by connecting to
|
|
// DialWithTLSConfig establishes a secure connection to a log daemon by connecting to
|
|
// address raddr on the specified network. It uses tlsConfig to configure the secure connection.
|
|
// address raddr on the specified network. It uses tlsConfig to configure the secure connection.
|
|
func DialWithTLSConfig(network, raddr string, priority Priority, tag string, tlsConfig *tls.Config) (*Writer, error) {
|
|
func DialWithTLSConfig(network, raddr string, priority Priority, tag string, tlsConfig *tls.Config) (*Writer, error) {
|
|
|
|
+ return dialAllParameters(network, raddr, priority, tag, tlsConfig, nil)
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// implementation of the various functions above
|
|
|
|
+func dialAllParameters(network, raddr string, priority Priority, tag string, tlsConfig *tls.Config, customDial DialFunc) (*Writer, error) {
|
|
if err := validatePriority(priority); err != nil {
|
|
if err := validatePriority(priority); err != nil {
|
|
return nil, err
|
|
return nil, err
|
|
}
|
|
}
|
|
@@ -69,12 +96,13 @@ func DialWithTLSConfig(network, raddr string, priority Priority, tag string, tls
|
|
hostname, _ := os.Hostname()
|
|
hostname, _ := os.Hostname()
|
|
|
|
|
|
w := &Writer{
|
|
w := &Writer{
|
|
- priority: priority,
|
|
|
|
- tag: tag,
|
|
|
|
- hostname: hostname,
|
|
|
|
- network: network,
|
|
|
|
- raddr: raddr,
|
|
|
|
- tlsConfig: tlsConfig,
|
|
|
|
|
|
+ priority: priority,
|
|
|
|
+ tag: tag,
|
|
|
|
+ hostname: hostname,
|
|
|
|
+ network: network,
|
|
|
|
+ raddr: raddr,
|
|
|
|
+ tlsConfig: tlsConfig,
|
|
|
|
+ customDial: customDial,
|
|
}
|
|
}
|
|
|
|
|
|
_, err := w.connect()
|
|
_, err := w.connect()
|