aboutsummaryrefslogtreecommitdiff
path: root/libgo/go/syslog/syslog.go
diff options
context:
space:
mode:
authorIan Lance Taylor <ian@gcc.gnu.org>2010-12-03 04:34:57 +0000
committerIan Lance Taylor <ian@gcc.gnu.org>2010-12-03 04:34:57 +0000
commit7a9389330e91acc3ed05deac2d198af25d13cf3c (patch)
tree38fe54a4f38ede5d949c915d66191f24a6fe5153 /libgo/go/syslog/syslog.go
parent1aa6700378e5188a853c018256113ce6e1fb5c05 (diff)
downloadgcc-7a9389330e91acc3ed05deac2d198af25d13cf3c.zip
gcc-7a9389330e91acc3ed05deac2d198af25d13cf3c.tar.gz
gcc-7a9389330e91acc3ed05deac2d198af25d13cf3c.tar.bz2
Add Go frontend, libgo library, and Go testsuite.
gcc/: * gcc.c (default_compilers): Add entry for ".go". * common.opt: Add -static-libgo as a driver option. * doc/install.texi (Configuration): Mention libgo as an option for --enable-shared. Mention go as an option for --enable-languages. * doc/invoke.texi (Overall Options): Mention .go as a file name suffix. Mention go as a -x option. * doc/frontends.texi (G++ and GCC): Mention Go as a supported language. * doc/sourcebuild.texi (Top Level): Mention libgo. * doc/standards.texi (Standards): Add section on Go language. Move references for other languages into their own section. * doc/contrib.texi (Contributors): Mention that I contributed the Go frontend. gcc/testsuite/: * lib/go.exp: New file. * lib/go-dg.exp: New file. * lib/go-torture.exp: New file. * lib/target-supports.exp (check_compile): Match // Go. From-SVN: r167407
Diffstat (limited to 'libgo/go/syslog/syslog.go')
-rw-r--r--libgo/go/syslog/syslog.go144
1 files changed, 144 insertions, 0 deletions
diff --git a/libgo/go/syslog/syslog.go b/libgo/go/syslog/syslog.go
new file mode 100644
index 0000000..4924a76
--- /dev/null
+++ b/libgo/go/syslog/syslog.go
@@ -0,0 +1,144 @@
+// 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.
+
+// The syslog package provides a simple interface to
+// the system log service. It can send messages to the
+// syslog daemon using UNIX domain sockets, UDP, or
+// TCP connections.
+package syslog
+
+import (
+ "fmt"
+ "log"
+ "net"
+ "os"
+)
+
+type Priority int
+
+const (
+ // From /usr/include/sys/syslog.h.
+ // These are the same on Linux, BSD, and OS X.
+ LOG_EMERG Priority = iota
+ LOG_ALERT
+ LOG_CRIT
+ LOG_ERR
+ LOG_WARNING
+ LOG_NOTICE
+ LOG_INFO
+ LOG_DEBUG
+)
+
+// A Writer is a connection to a syslog server.
+type Writer struct {
+ priority Priority
+ prefix string
+ conn net.Conn
+}
+
+// New establishes a new connection to the system log daemon.
+// Each write to the returned writer sends a log message with
+// the given priority and prefix.
+func New(priority Priority, prefix string) (w *Writer, err os.Error) {
+ return Dial("", "", priority, prefix)
+}
+
+// Dial establishes a connection to a log daemon by connecting
+// to address raddr on the network net.
+// Each write to the returned writer sends a log message with
+// the given priority and prefix.
+func Dial(network, raddr string, priority Priority, prefix string) (w *Writer, err os.Error) {
+ if prefix == "" {
+ prefix = os.Args[0]
+ }
+ var conn net.Conn
+ if network == "" {
+ conn, err = unixSyslog()
+ } else {
+ conn, err = net.Dial(network, "", raddr)
+ }
+ 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)
+}
+
+func (w *Writer) writeString(p Priority, s string) (int, os.Error) {
+ return fmt.Fprintf(w.conn, "<%d>%s: %s\n", p, w.prefix, s)
+}
+
+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) {
+ _, err = w.writeString(LOG_EMERG, m)
+ return err
+}
+// Crit logs a message using the LOG_CRIT priority.
+func (w *Writer) Crit(m string) (err os.Error) {
+ _, err = w.writeString(LOG_CRIT, m)
+ return err
+}
+// ERR logs a message using the LOG_ERR priority.
+func (w *Writer) Err(m string) (err os.Error) {
+ _, err = w.writeString(LOG_ERR, m)
+ return err
+}
+
+// Warning logs a message using the LOG_WARNING priority.
+func (w *Writer) Warning(m string) (err os.Error) {
+ _, err = w.writeString(LOG_WARNING, m)
+ return err
+}
+
+// Notice logs a message using the LOG_NOTICE priority.
+func (w *Writer) Notice(m string) (err os.Error) {
+ _, err = w.writeString(LOG_NOTICE, m)
+ return err
+}
+// Info logs a message using the LOG_INFO priority.
+func (w *Writer) Info(m string) (err os.Error) {
+ _, err = w.writeString(LOG_INFO, m)
+ return err
+}
+// Debug logs a message using the LOG_DEBUG priority.
+func (w *Writer) Debug(m string) (err os.Error) {
+ _, err = w.writeString(LOG_DEBUG, m)
+ return err
+}
+
+// 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.
+// All messages are logged with priority p.
+func NewLogger(p Priority, flag int) *log.Logger {
+ s, err := New(p, "")
+ if err != nil {
+ return nil
+ }
+ return log.New(s, "", flag)
+}