1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
// Copyright 2015-2016 Luke Shumaker
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//go:generate make
package sd_daemon
import (
"bytes"
"io"
"log/syslog"
"os"
"strings"
"sync"
)
// Logger writes "<N>"-prefixed lines to an io.Writer, where N is a
// syslog priority number. It implements mostly the same interface as
// "log/syslog".Writer.
//
// You probably don't need any instance of this other than the
// constant "Log", which uses os.Stderr as the writer. It is
// implemented as a struct rather than a set of functions so that it
// can be passed around as an implementation of an interface.
type Logger struct {
mu sync.Mutex
out io.Writer
buf []byte
}
func NewLogger(w io.Writer) Logger {
return Logger{out: w}
}
// Log is a Logger that use used very similarly to
// "log/syslog".Writer, but writes to os.Stderr under the assumption
// that stderr is forwarded to syslog/journald.
//
// You are encouraged to use stderr unless you have a good reason to
// talk to syslog or journald directly.
var Log = Logger{out: os.Stderr}
// Cheap version of
//
// *buf = append(*buf, fmt.Sprintf("<%d>", n)...)
func appendPrefix(buf []byte, n syslog.Priority) []byte {
var b [22]byte // 21 = ceil(log_10(2^64))+len("<>")
b[len(b)-1] = '>'
i := len(b) - 2
for n >= 10 {
b[i] = byte('0' + n%10)
n = n / 10
i--
}
b[i] = byte('0' + n)
i--
b[i] = '<'
return append(buf, b[i:]...)
}
// WriteString writes a message with the specified priority to the
// log.
func (l Logger) WriteString(level syslog.Priority, msg string) (n int, err error) {
l.mu.Lock()
defer l.mu.Unlock()
msg = strings.TrimSuffix(msg, "\n")
// The following is a cheap version of:
//
// prefix := fmt.Sprintf("<%d>", level)
// buf := prefix + strings.Replace(msg, "\n", "\n"+prefix, -1)
// return io.WriteString(l.out, buf)
l.buf = l.buf[:0]
l.buf = appendPrefix(l.buf, level) // possible allocation
prefix := l.buf
nlines := strings.Count(msg, "\n") + 1
n = len(msg) + len(prefix)*nlines + 1
if cap(l.buf) < n {
l.buf = make([]byte, len(l.buf), n) // allocation
copy(l.buf, prefix)
}
for nlines > 1 {
nl := strings.IndexByte(msg, '\n')
l.buf = append(l.buf, msg[:nl+1]...)
l.buf = append(l.buf, prefix...)
msg = msg[nl+1:]
nlines--
}
l.buf = append(l.buf, msg...)
l.buf = append(l.buf, '\n')
return l.out.Write(l.buf)
}
// WriteString writes a message with the specified priority to the
// log.
func (l Logger) WriteBytes(level syslog.Priority, msg []byte) (n int, err error) {
// Copy/pasted from WriteString and
// * `strings.` -> `bytes.`
// * `"\n"` -> `[]byte{'\n'}`
l.mu.Lock()
defer l.mu.Unlock()
msg = bytes.TrimSuffix(msg, []byte{'\n'})
l.buf = l.buf[:0]
l.buf = appendPrefix(l.buf, level) // possible allocation
prefix := l.buf
nlines := bytes.Count(msg, []byte{'\n'}) + 1
n = len(msg) + len(prefix)*nlines + 1
if cap(l.buf) < n {
l.buf = make([]byte, len(l.buf), n) // allocation
copy(l.buf, prefix)
}
for nlines > 1 {
nl := bytes.IndexByte(msg, '\n')
l.buf = append(l.buf, msg[:nl+1]...)
l.buf = append(l.buf, prefix...)
msg = msg[nl+1:]
nlines--
}
l.buf = append(l.buf, msg...)
l.buf = append(l.buf, '\n')
return l.out.Write(l.buf)
}
type loggerWriter struct {
log Logger
level syslog.Priority
}
func (lw loggerWriter) Write(p []byte) (n int, err error) {
return lw.log.WriteBytes(lw.level, p)
}
// Writer returns an io.Writer that writes messages with the specified
// priority to the log.
func (l Logger) Writer(level syslog.Priority) io.Writer {
return loggerWriter{log: l, level: level}
}
|