|
@@ -8,8 +8,6 @@ import (
|
|
|
|
|
|
// A Writer is a connection to a syslog server.
|
|
// A Writer is a connection to a syslog server.
|
|
type Writer struct {
|
|
type Writer struct {
|
|
- sync.Mutex // guards conn
|
|
|
|
-
|
|
|
|
priority Priority
|
|
priority Priority
|
|
tag string
|
|
tag string
|
|
hostname string
|
|
hostname string
|
|
@@ -19,28 +17,48 @@ type Writer struct {
|
|
framer Framer
|
|
framer Framer
|
|
formatter Formatter
|
|
formatter Formatter
|
|
|
|
|
|
|
|
+ mu sync.RWMutex // guards conn
|
|
conn serverConn
|
|
conn serverConn
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// getConn provides access to the internal conn, protected by a mutex. The
|
|
|
|
+// conn is threadsafe, so it can be used while unlocked, but we want to avoid
|
|
|
|
+// race conditions on grabbing a reference to it.
|
|
|
|
+func (w *Writer) getConn() serverConn {
|
|
|
|
+ w.mu.RLock()
|
|
|
|
+ conn := w.conn
|
|
|
|
+ w.mu.RUnlock()
|
|
|
|
+ return conn
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// setConn updates the internal conn, protected by a mutex.
|
|
|
|
+func (w *Writer) setConn(c serverConn) {
|
|
|
|
+ w.mu.Lock()
|
|
|
|
+ w.conn = c
|
|
|
|
+ w.mu.Unlock()
|
|
|
|
+}
|
|
|
|
+
|
|
// connect makes a connection to the syslog server.
|
|
// connect makes a connection to the syslog server.
|
|
-// It must be called with w.mu held.
|
|
|
|
-func (w *Writer) connect() (err error) {
|
|
|
|
- if w.conn != nil {
|
|
|
|
|
|
+func (w *Writer) connect() (serverConn, error) {
|
|
|
|
+ conn := w.getConn()
|
|
|
|
+ if conn != nil {
|
|
// ignore err from close, it makes sense to continue anyway
|
|
// ignore err from close, it makes sense to continue anyway
|
|
- w.conn.close()
|
|
|
|
- w.conn = nil
|
|
|
|
|
|
+ conn.close()
|
|
|
|
+ w.setConn(nil)
|
|
}
|
|
}
|
|
|
|
|
|
- var conn serverConn
|
|
|
|
var hostname string
|
|
var hostname string
|
|
|
|
+ var err error
|
|
dialer := w.getDialer()
|
|
dialer := w.getDialer()
|
|
conn, hostname, err = dialer.Call()
|
|
conn, hostname, err = dialer.Call()
|
|
if err == nil {
|
|
if err == nil {
|
|
- w.conn = conn
|
|
|
|
|
|
+ w.setConn(conn)
|
|
w.hostname = hostname
|
|
w.hostname = hostname
|
|
- }
|
|
|
|
|
|
|
|
- return
|
|
|
|
|
|
+ return conn, nil
|
|
|
|
+ } else {
|
|
|
|
+ return nil, err
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
// SetFormatter changes the formatter function for subsequent messages.
|
|
// SetFormatter changes the formatter function for subsequent messages.
|
|
@@ -59,14 +77,17 @@ func (w *Writer) Write(b []byte) (int, error) {
|
|
return w.writeAndRetry(w.priority, string(b))
|
|
return w.writeAndRetry(w.priority, string(b))
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// WriteWithPriority sends a log message with a custom priority
|
|
|
|
+func (w *Writer) WriteWithPriority(p Priority, b []byte) (int, error) {
|
|
|
|
+ return w.writeAndRetry(p, string(b))
|
|
|
|
+}
|
|
|
|
+
|
|
// Close closes a connection to the syslog daemon.
|
|
// Close closes a connection to the syslog daemon.
|
|
func (w *Writer) Close() error {
|
|
func (w *Writer) Close() error {
|
|
- w.Lock()
|
|
|
|
- defer w.Unlock()
|
|
|
|
-
|
|
|
|
- if w.conn != nil {
|
|
|
|
- err := w.conn.close()
|
|
|
|
- w.conn = nil
|
|
|
|
|
|
+ conn := w.getConn()
|
|
|
|
+ if conn != nil {
|
|
|
|
+ err := conn.close()
|
|
|
|
+ w.setConn(nil)
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
return nil
|
|
@@ -131,29 +152,29 @@ func (w *Writer) Debug(m string) (err error) {
|
|
func (w *Writer) writeAndRetry(p Priority, s string) (int, error) {
|
|
func (w *Writer) writeAndRetry(p Priority, s string) (int, error) {
|
|
pr := (w.priority & facilityMask) | (p & severityMask)
|
|
pr := (w.priority & facilityMask) | (p & severityMask)
|
|
|
|
|
|
- w.Lock()
|
|
|
|
- defer w.Unlock()
|
|
|
|
-
|
|
|
|
- if w.conn != nil {
|
|
|
|
- if n, err := w.write(pr, s); err == nil {
|
|
|
|
|
|
+ conn := w.getConn()
|
|
|
|
+ if conn != nil {
|
|
|
|
+ if n, err := w.write(conn, pr, s); err == nil {
|
|
return n, err
|
|
return n, err
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- if err := w.connect(); err != nil {
|
|
|
|
|
|
+
|
|
|
|
+ var err error
|
|
|
|
+ if conn, err = w.connect(); err != nil {
|
|
return 0, err
|
|
return 0, err
|
|
}
|
|
}
|
|
- return w.write(pr, s)
|
|
|
|
|
|
+ return w.write(conn, pr, s)
|
|
}
|
|
}
|
|
|
|
|
|
// write generates and writes a syslog formatted string. It formats the
|
|
// write generates and writes a syslog formatted string. It formats the
|
|
// message based on the current Formatter and Framer.
|
|
// message based on the current Formatter and Framer.
|
|
-func (w *Writer) write(p Priority, msg string) (int, error) {
|
|
|
|
|
|
+func (w *Writer) write(conn serverConn, p Priority, msg string) (int, error) {
|
|
// ensure it ends in a \n
|
|
// ensure it ends in a \n
|
|
if !strings.HasSuffix(msg, "\n") {
|
|
if !strings.HasSuffix(msg, "\n") {
|
|
msg += "\n"
|
|
msg += "\n"
|
|
}
|
|
}
|
|
|
|
|
|
- err := w.conn.writeString(w.framer, w.formatter, p, w.hostname, w.tag, msg)
|
|
|
|
|
|
+ err := conn.writeString(w.framer, w.formatter, p, w.hostname, w.tag, msg)
|
|
if err != nil {
|
|
if err != nil {
|
|
return 0, err
|
|
return 0, err
|
|
}
|
|
}
|