Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Default for formatters and Facility; get hostname automatically #51

Merged
merged 2 commits into from
Dec 5, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Implement Default for formatters and Facility; get hostname autom…
…atically.
  • Loading branch information
argv-minus-one committed May 21, 2020
commit d84c591e308cc83883e82bc8f065f7ce2fe0e84f
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ documentation = "https://docs.rs/syslog"
keywords = ["syslog", "logs", "logging"]

[dependencies]
hostname = "^0.3"
libc = "^0.2"
time = "^0.1"
log = { version = "^0.4", features = [ "std" ] }
Expand Down
9 changes: 9 additions & 0 deletions src/facility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,12 @@ impl FromStr for Facility {
Ok(result)
}
}

impl Default for Facility {
/// Returns the default `Facility`, which is `LOG_USER` (as [specified by POSIX]).
///
/// [specified by POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/closelog.html
fn default() -> Self {
Facility::LOG_USER
}
}
90 changes: 90 additions & 0 deletions src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use std::collections::HashMap;
use Priority;
use errors::*;
use facility::Facility;
use get_hostname;
use get_process_info;

#[allow(non_camel_case_types)]
#[derive(Copy,Clone)]
Expand Down Expand Up @@ -80,6 +82,33 @@ impl<T: Display> LogFormat<T> for Formatter3164 {
}
}

impl Default for Formatter3164 {
/// Returns a `Formatter3164` with default settings.
///
/// The default settings are as follows:
///
/// * `facility`: `LOG_USER`, as [specified by POSIX].
/// * `hostname`: Automatically detected using [the `hostname` crate], if possible.
/// * `process`: Automatically detected using [`std::env::current_exe`], or if that fails, an empty string.
/// * `pid`: Automatically detected using [`libc::getpid`].
///
/// [`libc::getpid`]: https://docs.rs/libc/0.2/libc/fn.getpid.html
/// [specified by POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/closelog.html
/// [`std::env::current_exe`]: https://doc.rust-lang.org/std/env/fn.current_exe.html
/// [the `hostname` crate]: https://crates.io/crates/hostname
fn default() -> Self {
let (process, pid) = get_process_info().unwrap_or((String::new(), unsafe { libc::getpid() }));
let hostname = get_hostname().ok();

Self {
facility: Default::default(),
hostname,
process,
pid,
}
}
}

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

Expand Down Expand Up @@ -124,6 +153,67 @@ impl<T: Display> LogFormat<(i32, StructuredData, T)> for Formatter5424 {
}
}

impl Default for Formatter5424 {
/// Returns a `Formatter5424` with default settings.
///
/// The default settings are as follows:
///
/// * `facility`: `LOG_USER`, as [specified by POSIX].
/// * `hostname`: Automatically detected using [the `hostname` crate], if possible.
/// * `process`: Automatically detected using [`std::env::current_exe`], or if that fails, an empty string.
/// * `pid`: Automatically detected using [`libc::getpid`].
///
/// [`libc::getpid`]: https://docs.rs/libc/0.2/libc/fn.getpid.html
/// [specified by POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/closelog.html
/// [`std::env::current_exe`]: https://doc.rust-lang.org/std/env/fn.current_exe.html
/// [the `hostname` crate]: https://crates.io/crates/hostname
fn default() -> Self {
// Get the defaults from `Formatter3164` and move them over.
let Formatter3164 { facility, hostname, process, pid } = Default::default();
Self { facility, hostname, process, pid }
}
}

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

#[test]
fn test_formatter3164_defaults() {
let d = Formatter3164::default();

// `Facility` doesn't implement `PartialEq`, so we use a `match` instead.
assert!(match d.facility {
Facility::LOG_USER => true,
_ => false
});

assert!(match &d.hostname {
Some(hostname) => !hostname.is_empty(),
None => false,
});

assert!(!d.process.is_empty());

// Can't really make any assertions about the pid.
}

#[test]
fn test_formatter5424_defaults() {
let d = Formatter5424::default();

// `Facility` doesn't implement `PartialEq`, so we use a `match` instead.
assert!(match d.facility {
Facility::LOG_USER => true,
_ => false
});

assert!(match &d.hostname {
Some(hostname) => !hostname.is_empty(),
None => false,
});

assert!(!d.process.is_empty());

// Can't really make any assertions about the pid.
}
5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ pub fn init(facility: Facility, log_level: log::LevelFilter,
let process = application_name.map(From::from).unwrap_or(process_name);
let formatter = Formatter3164 {
facility,
hostname: None,
hostname: get_hostname().ok(),
process,
pid,
};
Expand Down Expand Up @@ -469,3 +469,6 @@ fn get_process_info() -> Result<(String,i32)> {
})
}

fn get_hostname() -> Result<String> {
Ok(hostname::get()?.to_string_lossy().to_string())
}