From 8206cce1178717adec1d56abbb0e57ee708f862a Mon Sep 17 00:00:00 2001 From: Ian Lance Taylor Date: Wed, 9 Mar 2011 13:15:46 -0800 Subject: [PATCH] syslog: split Unix domain support from network support. This is to make it easier to support Solaris syslog. On Solaris syslog messages are sent via STREAMS using putmsg to /dev/conslog. The putmsg call uses a a control buffer of type log_cdtl and a data buffer which is the message, and it is in general a big mess. This CL just splits out the Unix domain support so that Solaris can use a different mechanism. I do not propose to implement the Solaris support today. This split will make it possible for gccgo to just call the libc function for now. R=r, rsc CC=golang-dev https://golang.org/cl/4261061 --- src/pkg/syslog/Makefile | 1 + src/pkg/syslog/syslog.go | 54 +++++++++++++++++++---------------- src/pkg/syslog/syslog_unix.go | 31 ++++++++++++++++++++ 3 files changed, 62 insertions(+), 24 deletions(-) create mode 100644 src/pkg/syslog/syslog_unix.go diff --git a/src/pkg/syslog/Makefile b/src/pkg/syslog/Makefile index cf6863c87b2..82baf7253a6 100644 --- a/src/pkg/syslog/Makefile +++ b/src/pkg/syslog/Makefile @@ -7,5 +7,6 @@ include ../../Make.inc TARG=syslog GOFILES=\ syslog.go\ + syslog_unix.go\ include ../../Make.pkg diff --git a/src/pkg/syslog/syslog.go b/src/pkg/syslog/syslog.go index 4924a76d0c8..711d5ddc74e 100644 --- a/src/pkg/syslog/syslog.go +++ b/src/pkg/syslog/syslog.go @@ -34,7 +34,17 @@ const ( type Writer struct { priority Priority prefix string - conn net.Conn + conn serverConn +} + +type serverConn interface { + writeBytes(p Priority, prefix string, b []byte) (int, os.Error) + writeString(p Priority, prefix string, s string) (int, os.Error) + close() os.Error +} + +type netConn struct { + conn net.Conn } // New establishes a new connection to the system log daemon. @@ -52,46 +62,30 @@ func Dial(network, raddr string, priority Priority, prefix string) (w *Writer, e if prefix == "" { prefix = os.Args[0] } - var conn net.Conn + var conn serverConn if network == "" { conn, err = unixSyslog() } else { - conn, err = net.Dial(network, "", raddr) + var c net.Conn + c, err = net.Dial(network, "", raddr) + conn = netConn{c} } return &Writer{priority, prefix, conn}, err } -func unixSyslog() (conn net.Conn, err os.Error) { - logTypes := []string{"unixgram", "unix"} - logPaths := []string{"/dev/log", "/var/run/syslog"} - var raddr string - for _, network := range logTypes { - for _, path := range logPaths { - raddr = path - conn, err := net.Dial(network, "", raddr) - if err != nil { - continue - } else { - return conn, nil - } - } - } - return nil, os.ErrorString("Unix syslog delivery error") -} - // Write sends a log message to the syslog daemon. func (w *Writer) Write(b []byte) (int, os.Error) { if w.priority > LOG_DEBUG || w.priority < LOG_EMERG { return 0, os.EINVAL } - return fmt.Fprintf(w.conn, "<%d>%s: %s\n", w.priority, w.prefix, b) + return w.conn.writeBytes(w.priority, w.prefix, b) } func (w *Writer) writeString(p Priority, s string) (int, os.Error) { - return fmt.Fprintf(w.conn, "<%d>%s: %s\n", p, w.prefix, s) + return w.conn.writeString(p, w.prefix, s) } -func (w *Writer) Close() os.Error { return w.conn.Close() } +func (w *Writer) Close() os.Error { return w.conn.close() } // Emerg logs a message using the LOG_EMERG priority. func (w *Writer) Emerg(m string) (err os.Error) { @@ -131,6 +125,18 @@ func (w *Writer) Debug(m string) (err os.Error) { return err } +func (n netConn) writeBytes(p Priority, prefix string, b []byte) (int, os.Error) { + return fmt.Fprintf(n.conn, "<%d>%s: %s\n", p, prefix, b) +} + +func (n netConn) writeString(p Priority, prefix string, s string) (int, os.Error) { + return fmt.Fprintf(n.conn, "<%d>%s: %s\n", p, prefix, s) +} + +func (n netConn) close() os.Error { + return n.conn.Close() +} + // NewLogger provides an object that implements the full log.Logger interface, // but sends messages to Syslog instead; flag is passed as is to Logger; // priority will be used for all messages sent using this interface. diff --git a/src/pkg/syslog/syslog_unix.go b/src/pkg/syslog/syslog_unix.go new file mode 100644 index 00000000000..b4daf88ee28 --- /dev/null +++ b/src/pkg/syslog/syslog_unix.go @@ -0,0 +1,31 @@ +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package syslog + +import ( + "net" + "os" +) + +// unixSyslog opens a connection to the syslog daemon running on the +// local machine using a Unix domain socket. + +func unixSyslog() (conn serverConn, err os.Error) { + logTypes := []string{"unixgram", "unix"} + logPaths := []string{"/dev/log", "/var/run/syslog"} + var raddr string + for _, network := range logTypes { + for _, path := range logPaths { + raddr = path + conn, err := net.Dial(network, "", raddr) + if err != nil { + continue + } else { + return netConn{conn}, nil + } + } + } + return nil, os.ErrorString("Unix syslog delivery error") +}