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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
use std;
use libc;
use log;
use log4rs;
#[cfg(feature = "file")]
use serde;
const DEFAULT_BUF_SIZE: usize = 4096;
struct BufWriter {
buf: std::io::Cursor<Vec<u8>>,
}
impl BufWriter {
pub fn new() -> Self {
Self {
buf: std::io::Cursor::new(Vec::with_capacity(DEFAULT_BUF_SIZE)),
}
}
pub fn into_inner(self) -> Vec<u8> {
self.buf.into_inner()
}
}
impl std::io::Write for BufWriter {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
self.buf.write(buf)
}
fn flush(&mut self) -> std::io::Result<()> {
self.buf.flush()
}
}
impl log4rs::encode::Write for BufWriter {}
pub type LevelMap = Fn(log::LogLevel) -> libc::c_int + Send + Sync;
pub struct SyslogAppender {
encoder: Box<log4rs::encode::Encode>,
ident: Option<String>,
level_map: Option<Box<LevelMap>>,
}
impl std::fmt::Debug for SyslogAppender {
fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(
formatter,
"SyslogAppender {{encoder: {:?}, ident: {:?}, level_map: {}}}",
self.encoder,
self.ident,
match self.level_map {
Some(_) => "Some(_)",
None => "None",
}
)
}
}
impl SyslogAppender {
pub fn builder() -> SyslogAppenderBuilder {
SyslogAppenderBuilder {
encoder: None,
ident: None,
level_map: None,
}
}
}
impl Drop for SyslogAppender {
fn drop(&mut self) {
if self.ident.is_some() {
unsafe {
libc::closelog();
}
}
}
}
impl log4rs::append::Append for SyslogAppender {
fn append(&self, record: &log::LogRecord) -> std::result::Result<(), Box<std::error::Error + Sync + Send>> {
let mut buf = BufWriter::new();
self.encoder.encode(&mut buf, record)?;
let mut buf = buf.into_inner();
buf.push(0);
let level = match self.level_map {
Some(ref level_map) => level_map(record.level()),
None => {
match record.level() {
log::LogLevel::Error => libc::LOG_ERR,
log::LogLevel::Warn => libc::LOG_WARNING,
log::LogLevel::Info => libc::LOG_INFO,
log::LogLevel::Debug => libc::LOG_DEBUG,
log::LogLevel::Trace => libc::LOG_DEBUG,
}
},
};
unsafe {
libc::syslog(
level,
b"%s\0".as_ptr() as *const libc::c_char,
buf.as_ptr() as *const libc::c_char,
);
}
Ok(())
}
}
bitflags! {
pub struct LogOption: libc::c_int {
const LOG_CONS = libc::LOG_CONS;
const LOG_NDELAY = libc::LOG_NDELAY;
const LOG_NOWAIT = libc::LOG_NOWAIT;
const LOG_ODELAY = libc::LOG_ODELAY;
const LOG_PERROR = libc::LOG_PERROR;
const LOG_PID = libc::LOG_PID;
}
}
#[cfg(feature = "file")]
struct LogOptionVisitor;
#[cfg(feature = "file")]
impl<'de> serde::de::Visitor<'de> for LogOptionVisitor {
type Value = LogOption;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("list of flags separated by \"|\"")
}
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
let mut flags = LogOption::empty();
let value = value.trim();
if !value.is_empty() {
for str_flag in value.split('|') {
let str_flag = str_flag.trim();
match str_flag {
"LOG_CONS" => flags = flags | LOG_CONS,
"LOG_NDELAY" => flags = flags | LOG_NDELAY,
"LOG_NOWAIT" => flags = flags | LOG_NOWAIT,
"LOG_ODELAY" => flags = flags | LOG_ODELAY,
"LOG_PERROR" => flags = flags | LOG_PERROR,
"LOG_PID" => flags = flags | LOG_PID,
unknown => return Err(E::custom(format!("Unknown syslog flag: \"{}\"", unknown))),
}
}
}
Ok(flags)
}
}
#[cfg(feature = "file")]
impl<'de> serde::de::Deserialize<'de> for LogOption {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::de::Deserializer<'de>,
{
deserializer.deserialize_str(LogOptionVisitor)
}
}
#[cfg_attr(feature = "file", derive(Deserialize))]
pub enum Facility {
Auth,
AuthPriv,
Cron,
Daemon,
Ftp,
Kern,
Local0,
Local1,
Local2,
Local3,
Local4,
Local5,
Local6,
Local7,
Lpr,
Mail,
News,
Syslog,
User,
Uucp,
}
impl Into<libc::c_int> for Facility {
fn into(self) -> libc::c_int {
match self {
Facility::Auth => libc::LOG_AUTH,
Facility::AuthPriv => libc::LOG_AUTHPRIV,
Facility::Cron => libc::LOG_CRON,
Facility::Daemon => libc::LOG_DAEMON,
Facility::Ftp => libc::LOG_FTP,
Facility::Kern => libc::LOG_KERN,
Facility::Local0 => libc::LOG_LOCAL0,
Facility::Local1 => libc::LOG_LOCAL1,
Facility::Local2 => libc::LOG_LOCAL2,
Facility::Local3 => libc::LOG_LOCAL3,
Facility::Local4 => libc::LOG_LOCAL4,
Facility::Local5 => libc::LOG_LOCAL5,
Facility::Local6 => libc::LOG_LOCAL6,
Facility::Local7 => libc::LOG_LOCAL7,
Facility::Lpr => libc::LOG_LPR,
Facility::Mail => libc::LOG_MAIL,
Facility::News => libc::LOG_NEWS,
Facility::Syslog => libc::LOG_SYSLOG,
Facility::User => libc::LOG_USER,
Facility::Uucp => libc::LOG_UUCP,
}
}
}
pub struct SyslogAppenderBuilder {
encoder: Option<Box<log4rs::encode::Encode>>,
ident: Option<String>,
level_map: Option<Box<LevelMap>>,
}
impl SyslogAppenderBuilder {
pub fn encoder(mut self, encoder: Box<log4rs::encode::Encode>) -> Self {
self.encoder = Some(encoder);
self
}
pub fn openlog(mut self, ident: &str, option: LogOption, facility: Facility) -> Self {
let mut ident = String::from(ident);
ident.push('\0');
unsafe {
libc::openlog(
ident.as_ptr() as *const libc::c_char,
option.bits(),
facility.into(),
);
}
self.ident = Some(ident);
self
}
pub fn level_map(mut self, level_map: Box<LevelMap>) -> Self {
self.level_map = Some(level_map);
self
}
pub fn build(self) -> SyslogAppender {
SyslogAppender {
encoder: self.encoder.unwrap_or_else(|| {
Box::new(log4rs::encode::pattern::PatternEncoder::default())
}),
ident: self.ident,
level_map: self.level_map,
}
}
}