Skip to content
Open
Changes from all commits
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
73 changes: 71 additions & 2 deletions src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ pub type StructuredData = HashMap<String, HashMap<String, String>>;
pub struct Formatter5424 {
pub facility: Facility,
pub hostname: Option<String>,
/// Called APP-NAME in RFC5424
pub process: String,
pub pid: u32,
}
Expand Down Expand Up @@ -165,15 +166,24 @@ impl Formatter5424 {
}
}

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

// XXX: seems a lot of effort per-call, we could do this via a wrapper type instead
Copy link
Contributor Author

@ctrlaltf24 ctrlaltf24 May 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thoughts? Is this OK or would you prefer a wrapper type? Or both?

I implemented such a wrapper type here: ctrlaltf24@7f0b138 seemed to add more complexity than it's worth.

// So the caller could do this once and pass it in
let message_id = message_id
.unwrap_or_else(|| NILL_VALUE.to_owned())
.chars()
.filter(is_us_print_ascii)
.take(32)
Comment on lines +183 to +184
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could also validate more of these fields to prevent the user from giving us values that violate the rfc, but at a runtime cost (as I've implemented here) ctrlaltf24@0362d74#diff-c875d3cb38acba1839738726b3d4aa920bec536459b52d5bc5069a91dd1620f5R187-R218 . Not sure if you'd want those changes, assuming no since the additional runtime complexity

.collect::<String>();

// Guard against sub-second precision over 6 digits per rfc5424 section 6
let timestamp = time::OffsetDateTime::now_utc();
// SAFETY: timestamp range is enforced, so this will never fail
Expand Down Expand Up @@ -203,6 +213,27 @@ impl<T: Display> LogFormat<(u32, StructuredData, T)> for Formatter5424 {
}
}

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<()> {
// Slight bit more overhead, but we can reuse the other implementation
LogFormat::<(Option<String>, StructuredData, T)>::format(
self,
w,
severity,
(
Some(log_message.0.to_string()),
log_message.1,
log_message.2,
),
)
}
}

impl Default for Formatter5424 {
/// Returns a `Formatter5424` with default settings.
///
Expand Down Expand Up @@ -241,10 +272,20 @@ fn escape_structure_data_param_value(value: &str) -> String {
.replace(']', "\\]")
}

/// Checks if a character is printable US ASCII
/// Defined by rfc5424 as between 33 and 126
fn is_us_print_ascii(c: &char) -> bool {
33 <= *c as u32 && *c as u32 <= 126
}

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

/// The value to use when a field is not present
/// Defined by rfc5424 as a single hyphen
const NILL_VALUE: &str = "-";

#[cfg(unix)]
// On unix platforms, time::OffsetDateTime::now_local always returns an error so use UTC instead
// https://github.com/time-rs/time/issues/380
Expand Down Expand Up @@ -280,6 +321,34 @@ mod test {
assert_eq!(value, "\\]");
}

#[test]
fn space_is_out_of_us_printable_ascii() {
assert!(!is_us_print_ascii(&' '));
}

#[test]
fn ascii_chars_are_in_range() {
for i in 33..=126 {
assert!(is_us_print_ascii(&char::from(i)));
}
for i in 'a'..'z' {
assert!(is_us_print_ascii(&i));
}
for i in 'A'..'Z' {
assert!(is_us_print_ascii(&i));
}
}

#[test]
fn ascii_thirty_two_out_of_range() {
assert!(!is_us_print_ascii(&char::from(32)));
}

#[test]
fn ascii_one_hundred_twenty_seven_out_of_range() {
assert!(!is_us_print_ascii(&char::from(127)));
}

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