OSDN Git Service

Update to current version of Go library.
[pf3gnuchains/gcc-fork.git] / libgo / go / syslog / syslog.go
1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // Package syslog provides a simple interface to the system log service. It
6 // can send messages to the syslog daemon using UNIX domain sockets, UDP, or
7 // TCP connections.
8 package syslog
9
10 import (
11         "fmt"
12         "log"
13         "net"
14         "os"
15 )
16
17 type Priority int
18
19 const (
20         // From /usr/include/sys/syslog.h.
21         // These are the same on Linux, BSD, and OS X.
22         LOG_EMERG Priority = iota
23         LOG_ALERT
24         LOG_CRIT
25         LOG_ERR
26         LOG_WARNING
27         LOG_NOTICE
28         LOG_INFO
29         LOG_DEBUG
30 )
31
32 // A Writer is a connection to a syslog server.
33 type Writer struct {
34         priority Priority
35         prefix   string
36         conn     serverConn
37 }
38
39 type serverConn interface {
40         writeBytes(p Priority, prefix string, b []byte) (int, os.Error)
41         writeString(p Priority, prefix string, s string) (int, os.Error)
42         close() os.Error
43 }
44
45 type netConn struct {
46         conn net.Conn
47 }
48
49 // New establishes a new connection to the system log daemon.
50 // Each write to the returned writer sends a log message with
51 // the given priority and prefix.
52 func New(priority Priority, prefix string) (w *Writer, err os.Error) {
53         return Dial("", "", priority, prefix)
54 }
55
56 // Dial establishes a connection to a log daemon by connecting
57 // to address raddr on the network net.
58 // Each write to the returned writer sends a log message with
59 // the given priority and prefix.
60 func Dial(network, raddr string, priority Priority, prefix string) (w *Writer, err os.Error) {
61         if prefix == "" {
62                 prefix = os.Args[0]
63         }
64         var conn serverConn
65         if network == "" {
66                 conn, err = unixSyslog()
67         } else {
68                 var c net.Conn
69                 c, err = net.Dial(network, raddr)
70                 conn = netConn{c}
71         }
72         return &Writer{priority, prefix, conn}, err
73 }
74
75 // Write sends a log message to the syslog daemon.
76 func (w *Writer) Write(b []byte) (int, os.Error) {
77         if w.priority > LOG_DEBUG || w.priority < LOG_EMERG {
78                 return 0, os.EINVAL
79         }
80         return w.conn.writeBytes(w.priority, w.prefix, b)
81 }
82
83 func (w *Writer) writeString(p Priority, s string) (int, os.Error) {
84         return w.conn.writeString(p, w.prefix, s)
85 }
86
87 func (w *Writer) Close() os.Error { return w.conn.close() }
88
89 // Emerg logs a message using the LOG_EMERG priority.
90 func (w *Writer) Emerg(m string) (err os.Error) {
91         _, err = w.writeString(LOG_EMERG, m)
92         return err
93 }
94 // Crit logs a message using the LOG_CRIT priority.
95 func (w *Writer) Crit(m string) (err os.Error) {
96         _, err = w.writeString(LOG_CRIT, m)
97         return err
98 }
99 // ERR logs a message using the LOG_ERR priority.
100 func (w *Writer) Err(m string) (err os.Error) {
101         _, err = w.writeString(LOG_ERR, m)
102         return err
103 }
104
105 // Warning logs a message using the LOG_WARNING priority.
106 func (w *Writer) Warning(m string) (err os.Error) {
107         _, err = w.writeString(LOG_WARNING, m)
108         return err
109 }
110
111 // Notice logs a message using the LOG_NOTICE priority.
112 func (w *Writer) Notice(m string) (err os.Error) {
113         _, err = w.writeString(LOG_NOTICE, m)
114         return err
115 }
116 // Info logs a message using the LOG_INFO priority.
117 func (w *Writer) Info(m string) (err os.Error) {
118         _, err = w.writeString(LOG_INFO, m)
119         return err
120 }
121 // Debug logs a message using the LOG_DEBUG priority.
122 func (w *Writer) Debug(m string) (err os.Error) {
123         _, err = w.writeString(LOG_DEBUG, m)
124         return err
125 }
126
127 func (n netConn) writeBytes(p Priority, prefix string, b []byte) (int, os.Error) {
128         return fmt.Fprintf(n.conn, "<%d>%s: %s\n", p, prefix, b)
129 }
130
131 func (n netConn) writeString(p Priority, prefix string, s string) (int, os.Error) {
132         return fmt.Fprintf(n.conn, "<%d>%s: %s\n", p, prefix, s)
133 }
134
135 func (n netConn) close() os.Error {
136         return n.conn.Close()
137 }
138
139 // NewLogger provides an object that implements the full log.Logger interface,
140 // but sends messages to Syslog instead; flag is passed as is to Logger;
141 // priority will be used for all messages sent using this interface.
142 // All messages are logged with priority p.
143 func NewLogger(p Priority, flag int) *log.Logger {
144         s, err := New(p, "")
145         if err != nil {
146                 return nil
147         }
148         return log.New(s, "", flag)
149 }