Commit 8206cce1 authored by Ian Lance Taylor's avatar Ian Lance Taylor

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
parent ec5c4759
......@@ -7,5 +7,6 @@ include ../../Make.inc
TARG=syslog
GOFILES=\
syslog.go\
syslog_unix.go\
include ../../Make.pkg
......@@ -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.
......
// 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")
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment