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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
use std;

use libc;
use log;
use log4rs;
#[cfg(feature = "file")]
use serde;

const DEFAULT_BUF_SIZE: usize = 4096;

type PersistentBuf = std::io::Cursor<Vec<u8>>;

thread_local! {
    static PERSISTENT_BUF: std::cell::RefCell<PersistentBuf> =
        std::cell::RefCell::new(PersistentBuf::new(Vec::with_capacity(DEFAULT_BUF_SIZE)));
}

struct BufWriter {}

impl BufWriter {
    fn new() -> Self {
        PERSISTENT_BUF.with(|pers_buf| pers_buf.borrow_mut().set_position(0));
        Self {}
    }

    fn as_c_str(&mut self) -> *const libc::c_char {
        use std::io::Write;

        PERSISTENT_BUF.with(|pers_buf| {
            let mut pers_buf = pers_buf.borrow_mut();
            pers_buf.write_all(&[0; 1]).unwrap();
            pers_buf.get_ref().as_ptr() as *const libc::c_char
        })
    }
}

impl std::io::Write for BufWriter {
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        PERSISTENT_BUF.with(|pers_buf| pers_buf.borrow_mut().write(buf))
    }

    fn flush(&mut self) -> std::io::Result<()> {
        PERSISTENT_BUF.with(|pers_buf| pers_buf.borrow_mut().flush())
    }
}

impl log4rs::encode::Write for BufWriter {}

/// Function for mapping rust's `log` levels to `libc`'s log levels.
pub type LevelMap = Fn(log::Level) -> libc::c_int + Send + Sync;

/// An appender which writes log invents into syslog using `libc`'s syslog() function.
pub struct SyslogAppender {
    encoder: Box<log4rs::encode::Encode>,
    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: {:?}, level_map: {}}}",
            self.encoder,
            match self.level_map {
                Some(_) => "Some(_)",
                None => "None",
            }
        )
    }
}

impl SyslogAppender {
    /// Create new builder for `SyslogAppender`.
    pub fn builder() -> SyslogAppenderBuilder {
        SyslogAppenderBuilder {
            encoder: None,
            openlog_args: None,
            level_map: None,
        }
    }
}

impl log4rs::append::Append for SyslogAppender {
    fn append(&self, record: &log::Record) -> std::result::Result<(), Box<std::error::Error + Sync + Send>> {
        let mut buf = BufWriter::new();

        self.encoder.encode(&mut buf, record)?;

        let level = match self.level_map {
            Some(ref level_map) => level_map(record.level()),

            None => match record.level() {
                log::Level::Error => libc::LOG_ERR,
                log::Level::Warn => libc::LOG_WARNING,
                log::Level::Info => libc::LOG_INFO,
                log::Level::Debug | log::Level::Trace => libc::LOG_DEBUG,
            },
        };

        unsafe {
            libc::syslog(
                level,
                b"%s\0".as_ptr() as *const libc::c_char,
                buf.as_c_str(),
            );
        }

        Ok(())
    }

    fn flush(&self) {}
}

bitflags! {
    /// Syslog option flags.
    pub struct LogOption: libc::c_int {
        /// Write directly to system console if there is an error while sending to system logger.
        const LOG_CONS   = libc::LOG_CONS;
        /// Open the connection immediately (normally, the connection is opened when the first message is logged).
        const LOG_NDELAY = libc::LOG_NDELAY;
        /// Don't wait for child processes that may have been created while logging the message.
        /// The GNU C library does not create a child process, so this option has no effect on Linux.
        const LOG_NOWAIT = libc::LOG_NOWAIT;
        /// The converse of LOG_NDELAY; opening of the connection is delayed until syslog() is called.
        /// This is the default, and need not be specified.
        const LOG_ODELAY = libc::LOG_ODELAY;
        /// Print to stderr as well. (Not in POSIX.1-2001 or POSIX.1-2008.)
        const LOG_PERROR = libc::LOG_PERROR;
        /// Include PID with each message.
        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 |= LogOption::LOG_CONS,
                    "LOG_NDELAY" => flags |= LogOption::LOG_NDELAY,
                    "LOG_NOWAIT" => flags |= LogOption::LOG_NOWAIT,
                    "LOG_ODELAY" => flags |= LogOption::LOG_ODELAY,
                    "LOG_PERROR" => flags |= LogOption::LOG_PERROR,
                    "LOG_PID" => flags |= LogOption::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)
    }
}

#[derive(Debug)]
#[cfg_attr(feature = "file", derive(Deserialize))]
/// The type of program.
pub enum Facility {
    /// Security/authorization.
    Auth,
    /// Security/authorization (private).
    AuthPriv,
    /// Clock daemon (cron and at).
    Cron,
    /// System daemons without separate facility value.
    Daemon,
    /// FTP daemon.
    Ftp,
    /// Kernel messages (these can't be generated from user processes).
    Kern,
    /// Reserved for local use.
    Local0,
    /// Reserved for local use.
    Local1,
    /// Reserved for local use.
    Local2,
    /// Reserved for local use.
    Local3,
    /// Reserved for local use.
    Local4,
    /// Reserved for local use.
    Local5,
    /// Reserved for local use.
    Local6,
    /// Reserved for local use.
    Local7,
    /// Line printer subsystem.
    Lpr,
    /// Mail subsystem.
    Mail,
    /// USENET news subsystem.
    News,
    /// Messages generated internally by syslogd.
    Syslog,
    /// Generic user-level messages. This is the default when not calling openlog().
    User,
    /// UUCP subsystem.
    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,
        }
    }
}

struct OpenLogArgs {
    ident: String,
    log_option: LogOption,
    facility: Facility,
}

struct IdentHolder {
    ident: Option<String>,
}

impl IdentHolder {
    fn new() -> Self {
        Self { ident: None }
    }

    fn openlog(&mut self, mut args: OpenLogArgs) {
        args.ident.push('\0');

        unsafe {
            libc::openlog(
                args.ident.as_ptr() as *const libc::c_char,
                args.log_option.bits(),
                args.facility.into(),
            );
        }

        // At least on Linux openlog() does not copy this string, so we should keep it available.
        self.ident = Some(args.ident);
    }

    fn closelog(&mut self) {
        if self.ident.is_some() {
            unsafe {
                libc::closelog();
            }
        }
    }

    fn no_openlog(&mut self) {
        self.closelog();
        self.ident = None;
    }
}

impl Drop for IdentHolder {
    fn drop(&mut self) {
        // Currently this function is never used automatically because IdentHolder is created only by lazy_static.
        self.closelog();
    }
}

lazy_static! {
    static ref IDENT_HOLDER: std::sync::Mutex<IdentHolder> = std::sync::Mutex::new(IdentHolder::new());
}

/// Builder for `SyslogAppender`.
pub struct SyslogAppenderBuilder {
    encoder: Option<Box<log4rs::encode::Encode>>,
    openlog_args: Option<OpenLogArgs>,
    level_map: Option<Box<LevelMap>>,
}

impl SyslogAppenderBuilder {
    /// Set custom encoder.
    pub fn encoder(mut self, encoder: Box<log4rs::encode::Encode>) -> Self {
        self.encoder = Some(encoder);
        self
    }

    /// Call openlog().
    pub fn openlog(mut self, ident: &str, option: LogOption, facility: Facility) -> Self {
        self.openlog_args = Some(OpenLogArgs {
            ident: String::from(ident),
            log_option: option,
            facility,
        });
        self
    }

    /// Set custom log level mapping.
    pub fn level_map(mut self, level_map: Box<LevelMap>) -> Self {
        self.level_map = Some(level_map);
        self
    }

    /// Consume builder and produce `SyslogAppender`.
    pub fn build(self) -> SyslogAppender {
        self.openlog_args.map_or_else(
            || IDENT_HOLDER.lock().unwrap().no_openlog(),
            |openlog_args| IDENT_HOLDER.lock().unwrap().openlog(openlog_args),
        );

        SyslogAppender {
            encoder: self.encoder
                .unwrap_or_else(|| Box::new(log4rs::encode::pattern::PatternEncoder::default())),
            level_map: self.level_map,
        }
    }
}

#[cfg(all(feature = "unstable", test))]
mod bench {
    use test;

    fn bench(bencher: &mut test::Bencher, data: &[u8]) {
        use std::io::Write;

        bencher.iter(|| {
            let mut buf = super::BufWriter::new();
            buf.write_all(data).unwrap();
            buf.as_c_str()
        })
    }

    #[bench]
    fn buf_writer_no_realloc(bencher: &mut test::Bencher) {
        bench(bencher, &[b'x'; super::DEFAULT_BUF_SIZE - 1])
    }

    #[bench]
    fn buf_writer_realloc(bencher: &mut test::Bencher) {
        bench(bencher, &[b'x'; super::DEFAULT_BUF_SIZE])
    }
}