Reads a log file, capped to its final [`MAX_LOG_BYTES`] bytes.
(path: &Path)
| 218 | |
| 219 | /// Reads a log file, capped to its final [`MAX_LOG_BYTES`] bytes. |
| 220 | fn read_log_tail(path: &Path) -> Option<String> { |
| 221 | use std::io::{Read, Seek, SeekFrom}; |
| 222 | |
| 223 | let mut file = std::fs::File::open(path).ok()?; |
| 224 | let len = file.metadata().ok()?.len(); |
| 225 | if len > MAX_LOG_BYTES { |
| 226 | file.seek(SeekFrom::End(-(MAX_LOG_BYTES as i64))).ok()?; |
| 227 | } |
| 228 | let mut contents = String::new(); |
| 229 | // Lossy-tolerant read: a mid-character seek point or stray bytes must not |
| 230 | // abort the whole scan. |
| 231 | let mut bytes = Vec::new(); |
| 232 | file.read_to_end(&mut bytes).ok()?; |
| 233 | contents.push_str(&String::from_utf8_lossy(&bytes)); |
| 234 | Some(contents) |
| 235 | } |
| 236 | |
| 237 | #[cfg(test)] |
| 238 | #[allow(clippy::unwrap_used, clippy::expect_used)] |
no test coverage detected