-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
844 additions
and
537 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
mod builder; | ||
mod connection; | ||
mod facility; | ||
mod formats; | ||
mod line; | ||
mod severity; | ||
mod syslog_connection; | ||
mod writer; | ||
|
||
#[allow(clippy::module_name_repetitions)] | ||
pub use self::{ | ||
builder::SyslogWriterBuilder, | ||
facility::SyslogFacility, | ||
formats::{syslog_default_format, syslog_format_with_thread}, | ||
line::SyslogLineHeader, | ||
severity::{LevelToSyslogSeverity, SyslogSeverity}, | ||
syslog_connection::SyslogConnection, | ||
writer::SyslogWriter, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
use super::{ | ||
line::SyslogLineHeader, severity::default_mapping, syslog_default_format, | ||
LevelToSyslogSeverity, SyslogConnection, SyslogFacility, SyslogWriter, | ||
}; | ||
use crate::FormatFunction; | ||
use std::io::{Error as IoError, ErrorKind, Result as IoResult}; | ||
|
||
#[allow(clippy::module_name_repetitions)] | ||
/// Builder for the `SyslogWriter`. | ||
/// | ||
/// Is created with [`SyslogWriter::builder`]. | ||
pub struct SyslogWriterBuilder { | ||
syslog_connection: SyslogConnection, | ||
syslog_line_header: SyslogLineHeader, | ||
syslog_facility: SyslogFacility, | ||
determine_severity: LevelToSyslogSeverity, | ||
max_log_level: log::LevelFilter, | ||
format: FormatFunction, | ||
} | ||
impl SyslogWriterBuilder { | ||
#[must_use] | ||
pub(super) fn new( | ||
syslog: SyslogConnection, | ||
syslog_line_header: SyslogLineHeader, | ||
syslog_facility: SyslogFacility, | ||
) -> SyslogWriterBuilder { | ||
SyslogWriterBuilder { | ||
syslog_connection: syslog, | ||
syslog_line_header, | ||
syslog_facility, | ||
determine_severity: default_mapping, | ||
max_log_level: log::LevelFilter::Warn, | ||
format: syslog_default_format, | ||
} | ||
} | ||
|
||
/// Use the given function to map the rust log levels to the syslog severities. | ||
/// By default a trivial mapping is used, which should be good enough in most cases. | ||
#[must_use] | ||
pub fn determine_severity(mut self, mapping: LevelToSyslogSeverity) -> Self { | ||
self.determine_severity = mapping; | ||
self | ||
} | ||
|
||
/// Specify up to which level log messages should be sent to the syslog. | ||
/// | ||
/// Default is: only warnings and errors. | ||
#[must_use] | ||
pub fn max_log_level(mut self, max_log_level: log::LevelFilter) -> Self { | ||
self.max_log_level = max_log_level; | ||
self | ||
} | ||
|
||
/// Use the given format function to write the message part of the syslog entries. | ||
/// | ||
/// By default, [`syslog_default_format`](crate::writers::syslog_default_format) is used. | ||
/// | ||
/// You can instead use [`syslog_format_with_thread`](crate::writers::syslog_format_with_thread) | ||
/// or your own `FormatFunction` | ||
/// (see the source code of the provided functions if you want to write your own). | ||
#[must_use] | ||
pub fn format(mut self, format: FormatFunction) -> Self { | ||
self.format = format; | ||
self | ||
} | ||
|
||
/// Returns a boxed instance of `SysLogWriter`. | ||
/// | ||
/// # Errors | ||
/// | ||
/// `std::io::Error` if the program's argument list is empty so that the process | ||
/// identifier for the syslog cannot be determined | ||
pub fn build(self) -> IoResult<Box<SyslogWriter>> { | ||
Ok(Box::new(SyslogWriter::new( | ||
std::process::id(), | ||
std::env::args().next().ok_or_else(|| { | ||
IoError::new( | ||
ErrorKind::Other, | ||
"Can't infer app name as no env args are present".to_owned(), | ||
) | ||
})?, | ||
self.syslog_line_header, | ||
self.syslog_facility, | ||
self.determine_severity, | ||
self.syslog_connection, | ||
self.max_log_level, | ||
self.format, | ||
)?)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use std::{ | ||
io::{Result as IoResult, Write}, | ||
net::{TcpStream, UdpSocket}, | ||
}; | ||
|
||
// Writable and flushable connection to the syslog backend. | ||
#[derive(Debug)] | ||
pub(super) enum Connection { | ||
// Sends log lines to the syslog via a | ||
// [UnixStream](https://doc.rust-lang.org/std/os/unix/net/struct.UnixStream.html). | ||
#[cfg_attr(docsrs, doc(cfg(target_family = "unix")))] | ||
#[cfg(target_family = "unix")] | ||
Stream(std::os::unix::net::UnixStream), | ||
|
||
// Sends log lines to the syslog via a | ||
// [UnixDatagram](https://doc.rust-lang.org/std/os/unix/net/struct.UnixDatagram.html). | ||
#[cfg_attr(docsrs, doc(cfg(target_family = "unix")))] | ||
#[cfg(target_family = "unix")] | ||
Datagram(std::os::unix::net::UnixDatagram), | ||
|
||
// Sends log lines to the syslog via UDP. | ||
// | ||
// UDP is fragile and thus discouraged except for local communication. | ||
Udp(UdpSocket), | ||
|
||
// Sends log lines to the syslog via TCP. | ||
Tcp(TcpStream), | ||
} | ||
|
||
impl Write for Connection { | ||
fn write(&mut self, buf: &[u8]) -> IoResult<usize> { | ||
match *self { | ||
#[cfg(target_family = "unix")] | ||
Self::Datagram(ref ud) => { | ||
// todo: reconnect if conn is broken | ||
ud.send(buf) | ||
} | ||
#[cfg(target_family = "unix")] | ||
Self::Stream(ref mut w) => { | ||
// todo: reconnect if conn is broken | ||
w.write(buf) | ||
.and_then(|sz| w.write_all(&[0; 1]).map(|()| sz)) | ||
} | ||
Self::Tcp(ref mut w) => { | ||
// todo: reconnect if conn is broken | ||
let n = w.write(buf)?; | ||
Ok(w.write(b"\n")? + n) | ||
} | ||
Self::Udp(ref socket) => { | ||
// ?? | ||
socket.send(buf) | ||
} | ||
} | ||
} | ||
|
||
fn flush(&mut self) -> IoResult<()> { | ||
match *self { | ||
#[cfg(target_family = "unix")] | ||
Self::Datagram(_) => Ok(()), | ||
|
||
#[cfg(target_family = "unix")] | ||
Self::Stream(ref mut w) => w.flush(), | ||
|
||
Self::Udp(_) => Ok(()), | ||
|
||
Self::Tcp(ref mut w) => w.flush(), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/// Syslog Facility, according to [RFC 5424](https://datatracker.ietf.org/doc/rfc5424). | ||
/// | ||
/// Note that the original integer values are already multiplied by 8. | ||
#[derive(Copy, Clone, Debug)] | ||
#[allow(clippy::module_name_repetitions)] | ||
pub enum SyslogFacility { | ||
/// kernel messages. | ||
Kernel = 0 << 3, | ||
/// user-level messages. | ||
UserLevel = 1 << 3, | ||
/// mail system. | ||
MailSystem = 2 << 3, | ||
/// system daemons. | ||
SystemDaemons = 3 << 3, | ||
/// security/authorization messages. | ||
Authorization = 4 << 3, | ||
/// messages generated internally by syslogd. | ||
SyslogD = 5 << 3, | ||
/// line printer subsystem. | ||
LinePrinter = 6 << 3, | ||
/// network news subsystem. | ||
News = 7 << 3, | ||
/// UUCP subsystem. | ||
Uucp = 8 << 3, | ||
/// clock daemon. | ||
Clock = 9 << 3, | ||
/// security/authorization messages. | ||
Authorization2 = 10 << 3, | ||
/// FTP daemon. | ||
Ftp = 11 << 3, | ||
/// NTP subsystem. | ||
Ntp = 12 << 3, | ||
/// log audit. | ||
LogAudit = 13 << 3, | ||
/// log alert. | ||
LogAlert = 14 << 3, | ||
/// clock daemon (note 2). | ||
Clock2 = 15 << 3, | ||
/// local use 0 (local0). | ||
LocalUse0 = 16 << 3, | ||
/// local use 1 (local1). | ||
LocalUse1 = 17 << 3, | ||
/// local use 2 (local2). | ||
LocalUse2 = 18 << 3, | ||
/// local use 3 (local3). | ||
LocalUse3 = 19 << 3, | ||
/// local use 4 (local4). | ||
LocalUse4 = 20 << 3, | ||
/// local use 5 (local5). | ||
LocalUse5 = 21 << 3, | ||
/// local use 6 (local6). | ||
LocalUse6 = 22 << 3, | ||
/// local use 7 (local7). | ||
LocalUse7 = 23 << 3, | ||
} |
Oops, something went wrong.