OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [log/] [syslog/] [syslog.go] - Blame information for rev 747

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// 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
        "errors"
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     serverConn
38
}
39
 
40
type serverConn interface {
41
        writeBytes(p Priority, prefix string, b []byte) (int, error)
42
        writeString(p Priority, prefix string, s string) (int, error)
43
        close() error
44
}
45
 
46
type netConn struct {
47
        conn net.Conn
48
}
49
 
50
// New establishes a new connection to the system log daemon.
51
// Each write to the returned writer sends a log message with
52
// the given priority and prefix.
53
func New(priority Priority, prefix string) (w *Writer, err error) {
54
        return Dial("", "", priority, prefix)
55
}
56
 
57
// Dial establishes a connection to a log daemon by connecting
58
// to address raddr on the network net.
59
// Each write to the returned writer sends a log message with
60
// the given priority and prefix.
61
func Dial(network, raddr string, priority Priority, prefix string) (w *Writer, err error) {
62
        if prefix == "" {
63
                prefix = os.Args[0]
64
        }
65
        var conn serverConn
66
        if network == "" {
67
                conn, err = unixSyslog()
68
        } else {
69
                var c net.Conn
70
                c, err = net.Dial(network, raddr)
71
                conn = netConn{c}
72
        }
73
        return &Writer{priority, prefix, conn}, err
74
}
75
 
76
// Write sends a log message to the syslog daemon.
77
func (w *Writer) Write(b []byte) (int, error) {
78
        if w.priority > LOG_DEBUG || w.priority < LOG_EMERG {
79
                return 0, errors.New("log/syslog: invalid priority")
80
        }
81
        return w.conn.writeBytes(w.priority, w.prefix, b)
82
}
83
 
84
func (w *Writer) writeString(p Priority, s string) (int, error) {
85
        return w.conn.writeString(p, w.prefix, s)
86
}
87
 
88
func (w *Writer) Close() error { return w.conn.close() }
89
 
90
// Emerg logs a message using the LOG_EMERG priority.
91
func (w *Writer) Emerg(m string) (err error) {
92
        _, err = w.writeString(LOG_EMERG, m)
93
        return err
94
}
95
 
96
// Alert logs a message using the LOG_ALERT priority.
97
func (w *Writer) Alert(m string) (err error) {
98
        _, err = w.writeString(LOG_ALERT, m)
99
        return err
100
}
101
 
102
// Crit logs a message using the LOG_CRIT priority.
103
func (w *Writer) Crit(m string) (err error) {
104
        _, err = w.writeString(LOG_CRIT, m)
105
        return err
106
}
107
 
108
// Err logs a message using the LOG_ERR priority.
109
func (w *Writer) Err(m string) (err error) {
110
        _, err = w.writeString(LOG_ERR, m)
111
        return err
112
}
113
 
114
// Warning logs a message using the LOG_WARNING priority.
115
func (w *Writer) Warning(m string) (err error) {
116
        _, err = w.writeString(LOG_WARNING, m)
117
        return err
118
}
119
 
120
// Notice logs a message using the LOG_NOTICE priority.
121
func (w *Writer) Notice(m string) (err error) {
122
        _, err = w.writeString(LOG_NOTICE, m)
123
        return err
124
}
125
 
126
// Info logs a message using the LOG_INFO priority.
127
func (w *Writer) Info(m string) (err error) {
128
        _, err = w.writeString(LOG_INFO, m)
129
        return err
130
}
131
 
132
// Debug logs a message using the LOG_DEBUG priority.
133
func (w *Writer) Debug(m string) (err error) {
134
        _, err = w.writeString(LOG_DEBUG, m)
135
        return err
136
}
137
 
138
func (n netConn) writeBytes(p Priority, prefix string, b []byte) (int, error) {
139
        return fmt.Fprintf(n.conn, "<%d>%s: %s\n", p, prefix, b)
140
}
141
 
142
func (n netConn) writeString(p Priority, prefix string, s string) (int, error) {
143
        return fmt.Fprintf(n.conn, "<%d>%s: %s\n", p, prefix, s)
144
}
145
 
146
func (n netConn) close() error {
147
        return n.conn.Close()
148
}
149
 
150
// NewLogger provides an object that implements the full log.Logger interface,
151
// but sends messages to Syslog instead; flag is passed as is to Logger;
152
// priority will be used for all messages sent using this interface.
153
// All messages are logged with priority p.
154
func NewLogger(p Priority, flag int) *log.Logger {
155
        s, err := New(p, "")
156
        if err != nil {
157
                return nil
158
        }
159
        return log.New(s, "", flag)
160
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.