Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
Geal authored Dec 5, 2021
2 parents 4f5d801 + 8137a40 commit df32a81
Show file tree
Hide file tree
Showing 6 changed files with 529 additions and 461 deletions.
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ documentation = "https://docs.rs/syslog"
keywords = ["syslog", "logs", "logging"]

[dependencies]
libc = "^0.2"
time = "^0.1"
log = { version = "^0.4", features = [ "std" ] }
error-chain = { version = "^0.12", default-features = false }
libc = "0.2.67"
time = "0.1.42"
log = { version = "0.4.8", features = [ "std" ] }
error-chain = { version = "0.12.2", default-features = false }

[features]
nightly = []
Expand Down
7 changes: 4 additions & 3 deletions examples/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ extern crate syslog;
#[macro_use]
extern crate log;

use syslog::{Facility, Formatter3164, BasicLogger};
use log:: LevelFilter;
use log::LevelFilter;
use syslog::{BasicLogger, Facility, Formatter3164};

fn main() {
let formatter = Formatter3164 {
Expand All @@ -16,7 +16,8 @@ fn main() {

let logger = syslog::unix(formatter).expect("could not connect to syslog");
log::set_boxed_logger(Box::new(BasicLogger::new(logger)))
.map(|()| log::set_max_level(LevelFilter::Info)).expect("could not register logger");
.map(|()| log::set_max_level(LevelFilter::Info))
.expect("could not register logger");

info!("hello world");
}
8 changes: 6 additions & 2 deletions examples/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ fn main() {
match syslog::unix(formatter) {
Err(e) => println!("impossible to connect to syslog: {:?}", e),
Ok(mut writer) => {
writer.err("hello world").expect("could not write error message");
writer.err("hello all".to_string()).expect("could not write error message");
writer
.err("hello world")
.expect("could not write error message");
writer
.err("hello all".to_string())
.expect("could not write error message");
}
}
}
84 changes: 42 additions & 42 deletions src/facility.rs
Original file line number Diff line number Diff line change
@@ -1,55 +1,55 @@
use std::str::FromStr;

#[allow(non_camel_case_types)]
#[derive(Copy,Clone,Debug)]
#[derive(Copy, Clone, Debug)]
pub enum Facility {
LOG_KERN = 0 << 3,
LOG_USER = 1 << 3,
LOG_MAIL = 2 << 3,
LOG_DAEMON = 3 << 3,
LOG_AUTH = 4 << 3,
LOG_SYSLOG = 5 << 3,
LOG_LPR = 6 << 3,
LOG_NEWS = 7 << 3,
LOG_UUCP = 8 << 3,
LOG_CRON = 9 << 3,
LOG_AUTHPRIV = 10 << 3,
LOG_FTP = 11 << 3,
LOG_LOCAL0 = 16 << 3,
LOG_LOCAL1 = 17 << 3,
LOG_LOCAL2 = 18 << 3,
LOG_LOCAL3 = 19 << 3,
LOG_LOCAL4 = 20 << 3,
LOG_LOCAL5 = 21 << 3,
LOG_LOCAL6 = 22 << 3,
LOG_LOCAL7 = 23 << 3
LOG_KERN = 0 << 3,
LOG_USER = 1 << 3,
LOG_MAIL = 2 << 3,
LOG_DAEMON = 3 << 3,
LOG_AUTH = 4 << 3,
LOG_SYSLOG = 5 << 3,
LOG_LPR = 6 << 3,
LOG_NEWS = 7 << 3,
LOG_UUCP = 8 << 3,
LOG_CRON = 9 << 3,
LOG_AUTHPRIV = 10 << 3,
LOG_FTP = 11 << 3,
LOG_LOCAL0 = 16 << 3,
LOG_LOCAL1 = 17 << 3,
LOG_LOCAL2 = 18 << 3,
LOG_LOCAL3 = 19 << 3,
LOG_LOCAL4 = 20 << 3,
LOG_LOCAL5 = 21 << 3,
LOG_LOCAL6 = 22 << 3,
LOG_LOCAL7 = 23 << 3,
}

impl FromStr for Facility {
type Err = ();
fn from_str(s: &str) -> Result<Facility, ()> {
let result = match &s.to_lowercase()[..] {
"log_kern" | "kern" => Facility::LOG_KERN,
"log_user" | "user" => Facility::LOG_USER,
"log_mail" | "mail" => Facility::LOG_MAIL,
"log_daemon" | "daemon" => Facility::LOG_DAEMON,
"log_auth" | "auth" => Facility::LOG_AUTH,
"log_syslog" | "syslog" => Facility::LOG_SYSLOG,
"log_lpr" | "lpr" => Facility::LOG_LPR,
"log_news" | "news" => Facility::LOG_NEWS,
"log_uucp" | "uucp" => Facility::LOG_UUCP,
"log_cron" | "cron" => Facility::LOG_CRON,
"log_authpriv"| "authpriv" => Facility::LOG_AUTHPRIV,
"log_ftp" | "ftp" => Facility::LOG_FTP,
"log_local0" | "local0" => Facility::LOG_LOCAL0,
"log_local1" | "local1" => Facility::LOG_LOCAL1,
"log_local2" | "local2" => Facility::LOG_LOCAL2,
"log_local3" | "local3" => Facility::LOG_LOCAL3,
"log_local4" | "local4" => Facility::LOG_LOCAL4,
"log_local5" | "local5" => Facility::LOG_LOCAL5,
"log_local6" | "local6" => Facility::LOG_LOCAL6,
"log_local7" | "local7" => Facility::LOG_LOCAL7,
_ => return Err(())
"log_kern" | "kern" => Facility::LOG_KERN,
"log_user" | "user" => Facility::LOG_USER,
"log_mail" | "mail" => Facility::LOG_MAIL,
"log_daemon" | "daemon" => Facility::LOG_DAEMON,
"log_auth" | "auth" => Facility::LOG_AUTH,
"log_syslog" | "syslog" => Facility::LOG_SYSLOG,
"log_lpr" | "lpr" => Facility::LOG_LPR,
"log_news" | "news" => Facility::LOG_NEWS,
"log_uucp" | "uucp" => Facility::LOG_UUCP,
"log_cron" | "cron" => Facility::LOG_CRON,
"log_authpriv" | "authpriv" => Facility::LOG_AUTHPRIV,
"log_ftp" | "ftp" => Facility::LOG_FTP,
"log_local0" | "local0" => Facility::LOG_LOCAL0,
"log_local1" | "local1" => Facility::LOG_LOCAL1,
"log_local2" | "local2" => Facility::LOG_LOCAL2,
"log_local3" | "local3" => Facility::LOG_LOCAL3,
"log_local4" | "local4" => Facility::LOG_LOCAL4,
"log_local5" | "local5" => Facility::LOG_LOCAL5,
"log_local6" | "local6" => Facility::LOG_LOCAL6,
"log_local7" | "local7" => Facility::LOG_LOCAL7,
_ => return Err(()),
};
Ok(result)
}
Expand Down
201 changes: 114 additions & 87 deletions src/format.rs
Original file line number Diff line number Diff line change
@@ -1,129 +1,156 @@
use time;
use std::io::Write;
use std::fmt::Display;
use std::collections::HashMap;
use std::fmt::Display;
use std::io::Write;
use time;

use Priority;
use errors::*;
use facility::Facility;
use Priority;

#[allow(non_camel_case_types)]
#[derive(Copy,Clone)]
#[derive(Copy, Clone)]
pub enum Severity {
LOG_EMERG,
LOG_ALERT,
LOG_CRIT,
LOG_ERR,
LOG_WARNING,
LOG_NOTICE,
LOG_INFO,
LOG_DEBUG
LOG_EMERG,
LOG_ALERT,
LOG_CRIT,
LOG_ERR,
LOG_WARNING,
LOG_NOTICE,
LOG_INFO,
LOG_DEBUG,
}

pub trait LogFormat<T> {
fn format<W: Write>(&self, w: &mut W, severity: Severity, message: T) -> Result<()>;
fn format<W: Write>(&self, w: &mut W, severity: Severity, message: T) -> Result<()>;

fn emerg<W: Write>(&mut self, w: &mut W, message: T) -> Result<()> {
self.format(w, Severity::LOG_EMERG, message)
}
fn emerg<W: Write>(&mut self, w: &mut W, message: T) -> Result<()> {
self.format(w, Severity::LOG_EMERG, message)
}

fn alert<W: Write>(&mut self, w: &mut W, message: T) -> Result<()> {
self.format(w, Severity::LOG_ALERT, message)
}
fn alert<W: Write>(&mut self, w: &mut W, message: T) -> Result<()> {
self.format(w, Severity::LOG_ALERT, message)
}

fn crit<W: Write>(&mut self, w: &mut W, message: T) -> Result<()> {
self.format(w, Severity::LOG_CRIT, message)
}
fn crit<W: Write>(&mut self, w: &mut W, message: T) -> Result<()> {
self.format(w, Severity::LOG_CRIT, message)
}

fn err<W: Write>(&mut self, w: &mut W, message: T) -> Result<()> {
self.format(w, Severity::LOG_ERR, message)
}
fn err<W: Write>(&mut self, w: &mut W, message: T) -> Result<()> {
self.format(w, Severity::LOG_ERR, message)
}

fn warning<W: Write>(&mut self, w: &mut W, message: T) -> Result<()> {
self.format(w, Severity::LOG_WARNING, message)
}
fn warning<W: Write>(&mut self, w: &mut W, message: T) -> Result<()> {
self.format(w, Severity::LOG_WARNING, message)
}

fn notice<W: Write>(&mut self, w: &mut W, message: T) -> Result<()> {
self.format(w, Severity::LOG_NOTICE, message)
}
fn notice<W: Write>(&mut self, w: &mut W, message: T) -> Result<()> {
self.format(w, Severity::LOG_NOTICE, message)
}

fn info<W: Write>(&mut self, w: &mut W, message: T) -> Result<()> {
self.format(w, Severity::LOG_INFO, message)
}
fn info<W: Write>(&mut self, w: &mut W, message: T) -> Result<()> {
self.format(w, Severity::LOG_INFO, message)
}

fn debug<W: Write>(&mut self, w: &mut W, message: T) -> Result<()> {
self.format(w, Severity::LOG_DEBUG, message)
}
fn debug<W: Write>(&mut self, w: &mut W, message: T) -> Result<()> {
self.format(w, Severity::LOG_DEBUG, message)
}
}

#[derive(Clone,Debug)]
#[derive(Clone, Debug)]
pub struct Formatter3164 {
pub facility: Facility,
pub hostname: Option<String>,
pub process: String,
pub pid: i32,
pub facility: Facility,
pub hostname: Option<String>,
pub process: String,
pub pid: u32,
}

impl<T: Display> LogFormat<T> for Formatter3164 {
fn format<W: Write>(&self, w: &mut W, severity: Severity, message: T) -> Result<()> {
if let Some(ref hostname) = self.hostname {
write!(w, "<{}>{} {} {}[{}]: {}",
encode_priority(severity, self.facility),
time::now().strftime("%b %d %T").unwrap(),
hostname, self.process, self.pid, message).chain_err(|| ErrorKind::Format)
} else {
write!(w, "<{}>{} {}[{}]: {}",
encode_priority(severity, self.facility),
time::now().strftime("%b %d %T").unwrap(),
self.process, self.pid, message).chain_err(|| ErrorKind::Format)
fn format<W: Write>(&self, w: &mut W, severity: Severity, message: T) -> Result<()> {
if let Some(ref hostname) = self.hostname {
write!(
w,
"<{}>{} {} {}[{}]: {}",
encode_priority(severity, self.facility),
time::now().strftime("%b %d %T").unwrap(),
hostname,
self.process,
self.pid,
message
)
.chain_err(|| ErrorKind::Format)
} else {
write!(
w,
"<{}>{} {}[{}]: {}",
encode_priority(severity, self.facility),
time::now().strftime("%b %d %T").unwrap(),
self.process,
self.pid,
message
)
.chain_err(|| ErrorKind::Format)
}
}
}
}

/// RFC 5424 structured data
pub type StructuredData = HashMap<String, HashMap<String, String>>;

#[derive(Clone,Debug)]
#[derive(Clone, Debug)]
pub struct Formatter5424 {
pub facility: Facility,
pub hostname: Option<String>,
pub process: String,
pub pid: i32,
pub facility: Facility,
pub hostname: Option<String>,
pub process: String,
pub pid: u32,
}

impl Formatter5424 {
pub fn format_5424_structured_data(&self, data: StructuredData) -> String {
if data.is_empty() {
"-".to_string()
} else {
let mut res = String::new();
for (id, params) in &data {
res = res + "["+id;
for (name,value) in params {
res = res + " " + name + "=\"" + value + "\"";
pub fn format_5424_structured_data(&self, data: StructuredData) -> String {
if data.is_empty() {
"-".to_string()
} else {
let mut res = String::new();
for (id, params) in &data {
res = res + "[" + id;
for (name, value) in params {
res = res + " " + name + "=\"" + value + "\"";
}
res += "]";
}

res
}
res += "]";
}

res
}
}
}

impl<T: Display> LogFormat<(i32, StructuredData, T)> for Formatter5424 {
fn format<W: Write>(&self, w: &mut W, severity: Severity, log_message: (i32, StructuredData, T)) -> Result<()> {
let (message_id, data, message) = log_message;

write!(w, "<{}> {} {} {} {} {} {} {} {}",
encode_priority(severity, self.facility),
1, // version
time::now_utc().rfc3339(),
self.hostname.as_ref().map(|x| &x[..]).unwrap_or("localhost"),
self.process, self.pid, message_id,
self.format_5424_structured_data(data), message).chain_err(|| ErrorKind::Format)
}
impl<T: Display> LogFormat<(u32, StructuredData, T)> for Formatter5424 {
fn format<W: Write>(
&self,
w: &mut W,
severity: Severity,
log_message: (u32, StructuredData, T),
) -> Result<()> {
let (message_id, data, message) = log_message;

write!(
w,
"<{}> 1 {} {} {} {} {} {} {}", // v1
encode_priority(severity, self.facility),
time::now_utc().rfc3339(),
self.hostname
.as_ref()
.map(|x| &x[..])
.unwrap_or("localhost"),
self.process,
self.pid,
message_id,
self.format_5424_structured_data(data),
message
)
.chain_err(|| ErrorKind::Format)
}
}

fn encode_priority(severity: Severity, facility: Facility) -> Priority {
facility as u8 | severity as u8
facility as u8 | severity as u8
}
Loading

0 comments on commit df32a81

Please sign in to comment.