captains-log 0.15.4

A minimalist customizable logger for rust, based on the `log` crate, but also adapted to `tracing`, for production and testing scenario.
Documentation
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
//! # Syslog support
//!
//! Config for syslog output, supports local and remote server.
//!
//! The underlayer protocol is implemented by [syslog](https://docs.rs/syslog) crate,
//! currently Formatter3164 is adapted.
//!
//! In order to achieve efficient socket I/O, the message is sent to channel,
//! and asynchronous flushed by backend writer.
//!
//! **When your program shutting down, should call flush to ensure the log is written to the socket.**
//!
//! ``` rust
//! log::logger().flush();
//! ```
//! On panic, our panic hook will call `flush()` explicitly.
//!
//! On connection, will output "syslog connected" message to stdout.
//!
//! On remote syslog server failure, will not panic, only "syslog: flush err" message will be print
//! to stderr, the backend thread will automatically reconnect to server.
//! In order to prevent hang up, the message will be dropped after a timeout.
//!
//! ## connect to local server
//!
//! ``` rust
//! use captains_log::{*, syslog::Facility};
//! recipe::syslog_local(Facility::LOG_USER, Level::Debug).build().expect("log_setup");
//! ```
//! ## connect to remote server
//!
//! ``` rust
//! use captains_log::{*, syslog::*};
//! let syslog = Syslog::new(Facility::LOG_USER, Level::Info).tcp("10.10.0.1:601");
//! let _ = Builder::default().add_sink(syslog).build();
//! ```

use crate::{
    config::{SinkConfigBuild, SinkConfigTrait},
    log_impl::{LogSink, LogSinkTrait},
    time::Timer,
};
use crossfire::*;
use log::{Level, Record};
use std::hash::{Hash, Hasher};
use std::io::{BufWriter, Error, ErrorKind, Result, Write};
use std::net::{TcpStream, ToSocketAddrs, UdpSocket};
use std::os::unix::net::{UnixDatagram, UnixStream};
use std::path::{Path, PathBuf};
use std::sync::{Arc, Once};
use std::thread;
use std::time::{Duration, Instant};
pub use syslog::Facility;
use syslog::{Formatter3164, LogFormat as SyslogFormat, LoggerBackend as SyslogBackend, Severity};

const TIMEOUT_DEFAULT: Duration = Duration::from_secs(5);
const UNIX_SOCK_PATHS: [&str; 3] = ["/dev/log", "/var/run/syslog", "/var/run/log"];
// NOTE: local /dev/log is always available

const LOCAL_TCP: &'static str = "127.0.0.1:601";

#[allow(dead_code)]
const LOCAL_UDP: &'static str = "127.0.0.1:514";

#[derive(Hash)]
pub enum SyslogProto {
    RFC3164,
}
// NOTE the document of syslog crate does not tell much how to adapt Formatter5424 to log crate,
// so we only support 3164 for now.

#[derive(Hash, Clone, Debug)]
pub enum SyslogAddr {
    /// remote server addr
    TCP(String),
    /// local socket addr and remote server addr
    UDP(String, String),
    /// Unix with specified path
    Unix(PathBuf),
}

/// Config for syslog sink, see [module level doc](crate::syslog) for usage:
pub struct Syslog {
    /// Syslog facility
    pub facility: Facility,
    /// Auto filled current process
    pub process: Option<String>,
    /// Auto filled localhost,
    pub hostname: Option<String>,
    /// max level of message goes to syslog
    pub level: Level,
    /// When in doubt, use RFC3164
    pub proto: SyslogProto,
    /// When None, connect local default unix socket.
    pub server: Option<SyslogAddr>,
    /// Drop msg when syslog server fail after a timeout, also apply to tcp connect timeout.
    pub timeout: Duration,
}

impl Hash for Syslog {
    fn hash<H: Hasher>(&self, hasher: &mut H) {
        hasher.write_u32(self.facility as u32);
        self.process.hash(hasher);
        self.hostname.hash(hasher);
        self.level.hash(hasher);
        self.proto.hash(hasher);
        self.timeout.hash(hasher);
        self.server.hash(hasher);
    }
}

impl Default for Syslog {
    fn default() -> Self {
        Self {
            proto: SyslogProto::RFC3164,
            facility: Facility::LOG_USER,
            process: None,
            hostname: None,
            level: Level::Trace,
            timeout: TIMEOUT_DEFAULT,
            server: None,
        }
    }
}

impl Syslog {
    pub fn new(facility: Facility, level: Level) -> Self {
        let mut s = Self::default();
        s.proto = SyslogProto::RFC3164;
        s.facility = facility;
        s.level = level;
        s
    }

    pub fn timeout(mut self, d: Duration) -> Self {
        self.timeout = d;
        self
    }

    /// Set hostname if you don't want the default
    pub fn hostname(mut self, name: String) -> Self {
        self.hostname = Some(name);
        self
    }

    /// Set process name if you don't want the default
    pub fn process_name(mut self, name: String) -> Self {
        self.process = Some(name);
        self
    }

    pub fn unix<P: Into<PathBuf>>(mut self, p: P) -> Self {
        self.server = Some(SyslogAddr::Unix(p.into()));
        self
    }

    pub fn tcp<S: AsRef<str>>(mut self, remote: S) -> Self {
        self.server = Some(SyslogAddr::TCP(remote.as_ref().to_string()));
        self
    }

    pub fn udp<S: AsRef<str>>(mut self, local: S, remote: S) -> Self {
        self.server =
            Some(SyslogAddr::UDP(local.as_ref().to_string(), remote.as_ref().to_string()));
        self
    }
}

impl SinkConfigBuild for Syslog {
    fn build(&self) -> LogSink {
        LogSink::Syslog(LogSinkSyslog::new(self))
    }
}

impl SinkConfigTrait for Syslog {
    fn get_level(&self) -> Level {
        self.level
    }

    fn get_file_path(&self) -> Option<Box<Path>> {
        None
    }

    fn write_hash(&self, hasher: &mut Box<dyn Hasher>) {
        self.hash(hasher);
        hasher.write(b"Syslog");
    }
}

enum Msg {
    Line(Vec<u8>),
    Flush(Arc<Once>),
}

pub(crate) struct LogSinkSyslog {
    tx: MTx<mpsc::Array<Msg>>,
    format: Formatter3164,
    max_level: Level,
}

impl LogSinkSyslog {
    fn new(config: &Syslog) -> Self {
        let (tx, rx) = mpsc::bounded_blocking(256);

        macro_rules! fill_format {
            ($f: expr, $config: expr) => {{
                $f.facility = $config.facility;
                if $config.server.is_some() {
                    // remote
                    if let Some(hostname) = &$config.hostname {
                        $f.hostname = Some(hostname.clone());
                    }
                } else {
                    // local don't need hostname
                    $f.hostname = None;
                }
                if let Some(process) = &$config.process {
                    $f.process = process.clone();
                }
            }};
        }
        let mut timeout = config.timeout;
        if timeout == Duration::from_secs(0) {
            timeout = TIMEOUT_DEFAULT;
        }
        let mut backend = Backend { server: config.server.clone(), timeout, writer: None };
        let _ = backend.reinit();

        let mut f = Formatter3164::default();
        fill_format!(f, config);
        thread::spawn(move || backend.run(rx));
        Self { tx, max_level: config.level, format: f }
    }
}

impl LogSinkTrait for LogSinkSyslog {
    fn open(&self) -> std::io::Result<()> {
        Ok(())
    }

    fn reopen(&self) -> std::io::Result<()> {
        Ok(())
    }

    #[inline(always)]
    fn log(&self, _now: &Timer, r: &Record) {
        let l = r.level();
        if r.level() <= self.max_level {
            let mut buf = Vec::with_capacity(128);
            let _level = match l {
                Level::Trace => Severity::LOG_DEBUG, // syslog don't have trace level
                Level::Debug => Severity::LOG_DEBUG,
                Level::Info => Severity::LOG_INFO,
                Level::Warn => Severity::LOG_WARNING,
                Level::Error => Severity::LOG_ERR,
            };
            let msg = format!("{}", r.args());
            self.format.format(&mut buf, _level, msg).expect("format");
            let _ = self.tx.send(Msg::Line(buf));
        }
    }

    #[inline(always)]
    fn flush(&self) {
        let o = Arc::new(Once::new());
        if self.tx.send(Msg::Flush(o.clone())).is_ok() {
            o.wait();
        }
    }
}

struct Backend {
    server: Option<SyslogAddr>,
    writer: Option<SyslogBackend>,
    timeout: Duration,
}

impl Backend {
    #[inline]
    fn connect_unix(path: &Path) -> Result<SyslogBackend> {
        let sock = UnixDatagram::unbound()?;
        match sock.connect(Path::new(path)) {
            Ok(()) => {
                println!("syslog: connect to unix {:?}", path);
                return Ok(SyslogBackend::Unix(sock));
            }
            Err(e) => {
                if e.raw_os_error() == Some(libc::EPROTOTYPE) {
                    let sock = UnixStream::connect(path)?;
                    println!("syslog: connect to unix {:?}", path);
                    return Ok(SyslogBackend::UnixStream(BufWriter::new(sock)));
                }
                return Err(e);
            }
        }
    }

    #[inline]
    fn connect_tcp(s: &str, timeout: Duration) -> Result<SyslogBackend> {
        for addr in s.to_socket_addrs()? {
            let socket = TcpStream::connect_timeout(&addr, timeout)?;
            return Ok(SyslogBackend::Tcp(BufWriter::new(socket)));
        }
        Err(Error::new(ErrorKind::NotFound, "syslog: no server address").into())
    }

    #[inline]
    fn connect_udp(local: &str, remote: &str) -> Result<SyslogBackend> {
        let server_addr = remote.to_socket_addrs().and_then(|mut server_addr_opt| {
            server_addr_opt
                .next()
                .ok_or_else(|| Error::new(ErrorKind::NotFound, "syslog: no server address").into())
        })?;
        println!("syslog: connect to udp {:?}", remote);
        let socket = UdpSocket::bind(local)?;
        return Ok(SyslogBackend::Udp(socket, server_addr));
    }

    fn connect(server: &Option<SyslogAddr>, timeout: Duration) -> Result<SyslogBackend> {
        match server {
            Some(SyslogAddr::Unix(p)) => Self::connect_unix(p.as_path()),
            Some(SyslogAddr::UDP(local, remote)) => Self::connect_udp(&local, &remote),
            Some(SyslogAddr::TCP(remote)) => Self::connect_tcp(&remote, timeout),
            None => {
                for p in &UNIX_SOCK_PATHS {
                    if let Ok(backend) = Self::connect_unix(Path::new(p)) {
                        return Ok(backend);
                    }
                }
                return Self::connect_tcp(LOCAL_TCP, timeout);
                // Self::connect_udp("127.0.0.1:0", "127.0.0.1:514")
                // XXX: do not connect local udp unless specified by user,
                // since we have no means to detect udp failure
            }
        }
    }

    #[inline(always)]
    fn reinit(&mut self) -> Result<()> {
        match Self::connect(&self.server, self.timeout) {
            Err(e) => {
                eprintln!("syslog: connect {:?} err {:?}", e, self.server);
                return Err(e);
            }
            Ok(backend) => {
                self.writer = Some(backend);
                Ok(())
            }
        }
    }

    #[inline(always)]
    fn flush(&mut self) {
        if let Some(writer) = self.writer.as_mut() {
            if let Err(e) = writer.flush() {
                eprintln!("syslog: flush err {:?}", e);
                self.writer = None;
            }
        }
    }

    #[inline]
    fn write(&mut self, msg: &[u8]) {
        if let Some(writer) = self.writer.as_mut() {
            match writer.write_all(msg) {
                Ok(_) => return,
                Err(e) => {
                    eprintln!("syslog: write err {:?}", e);
                    self.writer = None;
                }
            }
        }
        let start_ts = Instant::now();
        loop {
            thread::sleep(Duration::from_millis(500));
            if self.reinit().is_ok() {
                if let Some(writer) = self.writer.as_mut() {
                    match writer.write_all(msg) {
                        Ok(_) => return,
                        Err(e) => {
                            eprintln!("syslog: write err {:?}", e);
                            self.writer = None;
                        }
                    }
                }
            }
            if Instant::now().duration_since(start_ts) > self.timeout {
                // give up
                return;
            }
        }
    }

    fn run(&mut self, rx: Rx<mpsc::Array<Msg>>) {
        loop {
            match rx.recv() {
                Ok(Msg::Line(_msg)) => {
                    self.write(&_msg);
                    let mut need_flush = true;
                    while let Ok(msg) = rx.try_recv() {
                        match msg {
                            Msg::Line(_msg) => self.write(&_msg),
                            Msg::Flush(o) => {
                                self.flush();
                                o.call_once(|| {});
                                need_flush = false;
                            }
                        }
                    }
                    if need_flush {
                        self.flush();
                    }
                }
                Ok(Msg::Flush(o)) => {
                    self.flush();
                    o.call_once(|| {});
                }
                Err(_) => {
                    self.flush();
                    // exit
                    return;
                }
            }
        }
    }
}