From eb9b6bb15666ab0044e2244d75288f0d010431ec Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Mon, 18 Dec 2017 15:41:30 -0500 Subject: sd_daemon.Logger: improve docs --- sd_daemon/log.go | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) (limited to 'sd_daemon') diff --git a/sd_daemon/log.go b/sd_daemon/log.go index 138a07a..dc77907 100644 --- a/sd_daemon/log.go +++ b/sd_daemon/log.go @@ -53,10 +53,7 @@ func NewLogger(w io.Writer) *Logger { // Log is a Logger whose interface is very similar to syslog.Writer, // but writes to os.Stderr under the assumption that stderr is -// forwarded to syslog (or journald). -// -// You are encouraged to use stderr unless you have a good reason to -// talk to syslog or journald directly. +// forwarded to syslogd (or journald). var Log = NewLogger(os.Stderr) // Cheap version of @@ -114,7 +111,7 @@ func (l *Logger) WriteString(pri syslog.Priority, msg string) (n int, err error) return l.out.Write(l.buf) } -// WriteString writes a message with the specified priority to the +// WriteBytes writes a message with the specified priority to the // log. func (l *Logger) WriteBytes(pri syslog.Priority, msg []byte) (n int, err error) { // Copy/pasted from WriteString and -- cgit v1.2.3-54-g00ecf From b0c40f4c505fcf29d3ef8080c5e0d75d49000712 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Mon, 18 Dec 2017 15:43:59 -0500 Subject: sd_daemon: BREAKING CHANGE: rename WriteBytes->LogBytes, WriteString->LogString This is to make room for an io.stringWriter implementation. --- sd_daemon/log.go | 12 ++++++------ sd_daemon/log_test.go | 8 ++++---- sd_daemon/log_util.go.gen | 2 +- 3 files changed, 11 insertions(+), 11 deletions(-) (limited to 'sd_daemon') diff --git a/sd_daemon/log.go b/sd_daemon/log.go index dc77907..1bdf65f 100644 --- a/sd_daemon/log.go +++ b/sd_daemon/log.go @@ -74,9 +74,9 @@ func appendPrefix(buf []byte, n syslog.Priority) []byte { return append(buf, b[i:]...) } -// WriteString writes a message with the specified priority to the +// LogString writes a message with the specified priority to the // log. -func (l *Logger) WriteString(pri syslog.Priority, msg string) (n int, err error) { +func (l *Logger) LogString(pri syslog.Priority, msg string) (n int, err error) { l.mu.Lock() defer l.mu.Unlock() @@ -111,10 +111,10 @@ func (l *Logger) WriteString(pri syslog.Priority, msg string) (n int, err error) return l.out.Write(l.buf) } -// WriteBytes writes a message with the specified priority to the +// LogBytes writes a message with the specified priority to the // log. -func (l *Logger) WriteBytes(pri syslog.Priority, msg []byte) (n int, err error) { - // Copy/pasted from WriteString and +func (l *Logger) LogBytes(pri syslog.Priority, msg []byte) (n int, err error) { + // Copy/pasted from LogString and // * `strings.` -> `bytes.` // * `"\n"` -> `[]byte{'\n'}` l.mu.Lock() @@ -151,7 +151,7 @@ type loggerWriter struct { } func (lw loggerWriter) Write(p []byte) (n int, err error) { - return lw.log.WriteBytes(lw.pri, p) + return lw.log.LogBytes(lw.pri, p) } // Writer returns an io.Writer that writes messages with the specified diff --git a/sd_daemon/log_test.go b/sd_daemon/log_test.go index 2f835a8..fe2f6f6 100644 --- a/sd_daemon/log_test.go +++ b/sd_daemon/log_test.go @@ -54,17 +54,17 @@ func TestLog(t *testing.T) { for in, out := range testcases { written = nil - n, err := log.WriteString(in.pri, in.msg) + n, err := log.LogString(in.pri, in.msg) if n != len(out) || string(written) != out || err != nil { - t.Errorf("WriteString(%#v, %#v)\n -> expected:{%#v, %#v, %#v}\n -> got:{%#v, %#v, %#v}\n", + t.Errorf("LogString(%#v, %#v)\n -> expected:{%#v, %#v, %#v}\n -> got:{%#v, %#v, %#v}\n", in.pri, in.msg, len(out), nil, out, n, err, string(written)) } written = nil - n, err = log.WriteBytes(in.pri, []byte(in.msg)) + n, err = log.LogBytes(in.pri, []byte(in.msg)) if n != len(out) || string(written) != out || err != nil { - t.Errorf("WriteBytes(%#v, %#v)\n -> expected:{%#v, %#v, %#v}\n -> got:{%#v, %#v, %#v}\n", + t.Errorf("LogBytes(%#v, %#v)\n -> expected:{%#v, %#v, %#v}\n -> got:{%#v, %#v, %#v}\n", in.pri, in.msg, len(out), nil, out, n, err, string(written)) diff --git a/sd_daemon/log_util.go.gen b/sd_daemon/log_util.go.gen index 3ca63dc..9bfc28c 100755 --- a/sd_daemon/log_util.go.gen +++ b/sd_daemon/log_util.go.gen @@ -28,7 +28,7 @@ EOF cat < Date: Mon, 18 Dec 2017 22:40:43 -0500 Subject: sd_daemon/log: implement io.Writer and io.stringWriter where applicable --- sd_daemon/log.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'sd_daemon') diff --git a/sd_daemon/log.go b/sd_daemon/log.go index 1bdf65f..c8d8419 100644 --- a/sd_daemon/log.go +++ b/sd_daemon/log.go @@ -145,6 +145,20 @@ func (l *Logger) LogBytes(pri syslog.Priority, msg []byte) (n int, err error) { return l.out.Write(l.buf) } +// Write writes a message with priority syslog.LOG_INFO to the log; +// implementing io.Writer. +func (l *Logger) Write(msg []byte) (n int, err error) { + n, err = l.LogBytes(syslog.LOG_INFO, msg) + return n, err +} + +// WriteString writes a message with priority syslog.LOG_INFO to the +// log; implementing io.WriteString's interface. +func (l *Logger) WriteString(msg string) (n int, err error) { + n, err = l.LogString(syslog.LOG_INFO, msg) + return n, err +} + type loggerWriter struct { log *Logger pri syslog.Priority @@ -154,6 +168,10 @@ func (lw loggerWriter) Write(p []byte) (n int, err error) { return lw.log.LogBytes(lw.pri, p) } +func (lw loggerWriter) WriteString(p string) (n int, err error) { + return lw.log.LogString(lw.pri, p) +} + // Writer returns an io.Writer that writes messages with the specified // priority to the log. func (l *Logger) Writer(pri syslog.Priority) io.Writer { -- cgit v1.2.3-54-g00ecf