~john-koepi/ubuntu/trusty/golang/default

« back to all changes in this revision

Viewing changes to src/pkg/syslog/syslog.go

  • Committer: Bazaar Package Importer
  • Author(s): Ondřej Surý
  • Date: 2011-04-20 17:36:48 UTC
  • Revision ID: james.westby@ubuntu.com-20110420173648-ifergoxyrm832trd
Tags: upstream-2011.03.07.1
Import upstream version 2011.03.07.1

Show diffs side-by-side

added added

removed removed

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