| 1 | use core::fmt::{self, Write}; |
| 2 | use std::ffi::c_int; |
| 3 | |
| 4 | use crate::log::{Level, Log}; |
| 5 | |
| 6 | pub struct Syslog; |
| 7 | |
| 8 | mod internal { |
| 9 | use crate::system::syslog; |
| 10 | use std::ffi::{CStr, c_int}; |
| 11 | |
| 12 | const DOTDOTDOT_START: &[u8] = b"[...] "; |
| 13 | const DOTDOTDOT_END: &[u8] = b" [...]"; |
| 14 | |
| 15 | const MAX_MSG_LEN: usize = 960; |
| 16 | const NULL_BYTE_LEN: usize = 1; // for C string compatibility |
| 17 | const BUFSZ: usize = MAX_MSG_LEN + DOTDOTDOT_END.len() + NULL_BYTE_LEN; |
| 18 | |
| 19 | pub struct SysLogMessageWriter { |
| 20 | buffer: [u8; BUFSZ], |
| 21 | cursor: usize, |
| 22 | facility: c_int, |
| 23 | priority: c_int, |
| 24 | } |
| 25 | |
| 26 | // - whenever a SysLogMessageWriter has been constructed, a syslog message WILL be created |
| 27 | // for one specific event; this struct functions as a low-level interface for that message |
| 28 | // - the caller of the pub functions will have to take care never to `append` more bytes than |
| 29 | // are `available`, or a panic will occur. |
| 30 | // - the impl guarantees that after `line_break()`, there will be enough room available for at |
| 31 | // least a single UTF8 character sequence (which is true since MAX_MSG_LEN >= 10) |
| 32 | impl SysLogMessageWriter { |
| 33 | pub fn new(priority: c_int, facility: c_int) -> Self { |
| 34 | Self { |
| 35 | buffer: [0; BUFSZ], |
| 36 | cursor: 0, |
| 37 | priority, |
| 38 | facility, |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | pub fn append(&mut self, bytes: &[u8]) { |
| 43 | let num_bytes = bytes.len(); |
| 44 | self.buffer[self.cursor..self.cursor + num_bytes].copy_from_slice(bytes); |
| 45 | self.cursor += num_bytes; |
| 46 | } |
| 47 | |
| 48 | pub fn line_break(&mut self) { |
| 49 | self.append(DOTDOTDOT_END); |
| 50 | self.commit_to_syslog(); |
| 51 | self.append(DOTDOTDOT_START); |
| 52 | } |
| 53 | |
| 54 | fn commit_to_syslog(&mut self) { |
| 55 | self.append(&[0]); |
| 56 | let message = CStr::from_bytes_with_nul(&self.buffer[..self.cursor]).unwrap(); |
| 57 | syslog(self.priority, self.facility, message); |
| 58 | self.cursor = 0; |
| 59 | } |
| 60 | |
| 61 | pub fn available(&self) -> usize { |
| 62 | MAX_MSG_LEN - self.cursor |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | impl Drop for SysLogMessageWriter { |
| 67 | fn drop(&mut self) { |
| 68 | self.commit_to_syslog(); |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | use internal::SysLogMessageWriter; |
| 74 | |
| 75 | /// `floor_char_boundary` is currently unstable in Rust |
| 76 | fn floor_char_boundary(data: &str, mut index: usize) -> usize { |
| 77 | if index >= data.len() { |
| 78 | return data.len(); |
| 79 | } |
| 80 | while !data.is_char_boundary(index) { |
| 81 | index -= 1; |
| 82 | } |
| 83 | |
| 84 | index |
| 85 | } |
| 86 | |
| 87 | /// This function REQUIRES that `message` is larger than `max_size` (or a panic will occur). |
| 88 | /// This function WILL return a non-zero result if `max_size` is large enough to fit |
| 89 | /// at least the first character of `message`. |
| 90 | fn suggested_break(message: &str, max_size: usize) -> usize { |
| 91 | // method A: try to split the message in two non-empty parts on an ASCII white space character |
| 92 | // method B: split on the utf8 character boundary that consumes the most data |
| 93 | if let Some(pos) = message.as_bytes()[1..max_size] |
| 94 | .iter() |
| 95 | .rposition(|c| c.is_ascii_whitespace()) |
| 96 | { |
| 97 | // since pos+1 contains ASCII whitespace, it acts as a valid utf8 boundary as well |
| 98 | pos + 1 |
| 99 | } else { |
| 100 | floor_char_boundary(message, max_size) |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | impl Write for SysLogMessageWriter { |
| 105 | fn write_str(&mut self, mut message: &str) -> fmt::Result { |
| 106 | while message.len() > self.available() { |
| 107 | let truncate_boundary = suggested_break(message, self.available()); |
| 108 | |
| 109 | let left = &message[..truncate_boundary]; |
| 110 | let right = &message[truncate_boundary..]; |
| 111 | |
| 112 | self.append(left.as_bytes()); |
| 113 | self.line_break(); |
| 114 | |
| 115 | // This loop while terminate, since either of the following is true: |
| 116 | // 1. truncate_boundary is strictly positive: |
| 117 | // message.len() has strictly decreased, and self.available() has not decreased |
| 118 | // 2. truncate_boundary is zero: |
| 119 | // message.len() has remained unchanged, but self.available() has strictly increased; |
| 120 | // this latter is true since, for truncate_boundary to be 0, self.available() must |
| 121 | // have been not large enough to fit a single UTF8 character |
| 122 | message = right; |
| 123 | } |
| 124 | |
| 125 | self.append(message.as_bytes()); |
| 126 | |
| 127 | Ok(()) |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | const FACILITY: c_int = libc::LOG_AUTH; |
| 132 | |
| 133 | impl Log for Syslog { |
| 134 | fn log(&self, level: Level, args: &dyn fmt::Display) { |
| 135 | let priority = match level { |
| 136 | Level::Error => libc::LOG_ERR, |
| 137 | Level::Warn => libc::LOG_WARNING, |
| 138 | Level::Info => libc::LOG_INFO, |
| 139 | Level::Debug => libc::LOG_DEBUG, |
| 140 | }; |
| 141 | |
| 142 | let mut writer = SysLogMessageWriter::new(priority, FACILITY); |
| 143 | let _ = write!(writer, "{}", args); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | #[cfg(test)] |
| 148 | mod tests { |
| 149 | |
| 150 | use std::fmt::Write; |
| 151 | |
| 152 | use super::*; |
| 153 | |
| 154 | #[test] |
| 155 | fn can_write_to_syslog() { |
| 156 | Syslog.log(Level::Info, &format_args!("Hello World!")); |
| 157 | } |
| 158 | |
| 159 | #[test] |
| 160 | fn can_handle_multiple_writes() { |
| 161 | let mut writer = SysLogMessageWriter::new(libc::LOG_DEBUG, FACILITY); |
| 162 | |
| 163 | for i in 1..20 { |
| 164 | let _ = write!(writer, "{}", "Test 123 ".repeat(i)); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | #[test] |
| 169 | fn can_truncate_syslog() { |
| 170 | Syslog.log( |
| 171 | Level::Info, |
| 172 | &format_args!("This is supposed to be a very long syslog message but idk what to write, so I am just going to tell you about the time I tried to make coffee with a teapot. So I woke up one morning and decided to make myself a pot of coffee, however after all the wild coffee parties and mishaps the coffee pot had evetually given it's last cup on a tragic morning I call wednsday. So it came to, that the only object capable of giving me hope for the day was my teapot. As I stood in the kitchen and reached for my teapot it, as if sensing the impending horrors that awaited the innocent little teapot, emmited a horse sheak of desperation. \"three hundred and seven\", it said. \"What?\" I asked with a voice of someone who clearly did not want to be bothered until he had his daily almost medically necessary dose of caffine. \"I am a teapot\" it responded with a voice of increasing forcefulness. \"I am a teapot, not a coffee pot\". It was then, in my moments of confusion that my brain finally understood, this was a teapot."), |
| 173 | ); |
| 174 | } |
| 175 | |
| 176 | #[test] |
| 177 | fn can_truncate_syslog_with_no_spaces() { |
| 178 | Syslog.log( |
| 179 | Level::Info, |
| 180 | &format_args!("iwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercasesiwillhandlecornercases"), |
| 181 | ); |
| 182 | } |
| 183 | |
| 184 | #[test] |
| 185 | fn will_not_break_utf8() { |
| 186 | let mut writer = SysLogMessageWriter::new(libc::LOG_DEBUG, FACILITY); |
| 187 | |
| 188 | let _ = write!(writer, "{}¢", "x".repeat(959)); |
| 189 | } |
| 190 | } |