|
| 1 | +use std::sync::Mutex; |
| 2 | +use std::sync::atomic::{AtomicUsize, Ordering}; |
| 3 | + |
| 4 | +use slog::Drain; |
| 5 | +use log::RecordBuilder; |
| 6 | +use fragile::Fragile; |
| 7 | + |
| 8 | +struct StdLogAssertExpected<'a> { |
| 9 | + expected: Mutex<Vec<Fragile<log::Record<'a>>>>, |
| 10 | + current_index: AtomicUsize, |
| 11 | +} |
| 12 | +impl log::Log for StdLogAssertExpected<'_> { |
| 13 | + fn enabled(&self, _: &log::Metadata) -> bool { |
| 14 | + true |
| 15 | + } |
| 16 | + fn log(&self, actual: &log::Record<'_>) { |
| 17 | + let expected = { |
| 18 | + let expected = self.expected.lock().unwrap(); |
| 19 | + // NOTE: I think load fence is implied by the lock |
| 20 | + let old_index = self.current_index.load(Ordering::Relaxed); |
| 21 | + match expected.get(old_index) { |
| 22 | + Some(e) => { |
| 23 | + assert_eq!( |
| 24 | + old_index, |
| 25 | + // Do we need a store fence, or is that implied as well? |
| 26 | + self.current_index.fetch_add(1, Ordering::Acquire) |
| 27 | + ); |
| 28 | + e.get().clone() |
| 29 | + }, |
| 30 | + None => panic!("Expected no more log records. but got {:?}", actual) |
| 31 | + } |
| 32 | + }; |
| 33 | + assert_eq!(expected.metadata(), actual.metadata()); |
| 34 | + assert_eq!(expected.args().to_string(), actual.args().to_string()); |
| 35 | + assert_eq!(expected.level(), actual.level()); |
| 36 | + assert_eq!(expected.target(), actual.target()); |
| 37 | + assert_eq!(expected.module_path(), actual.module_path()); |
| 38 | + assert_eq!(expected.file(), actual.file()); |
| 39 | + // NOTE: Intentionally ignored `line` |
| 40 | + if cfg!(feature = "kv_unstable") { |
| 41 | + todo!("Structure not currently used. See PR #26"); |
| 42 | + } |
| 43 | + } |
| 44 | + fn flush(&self) {} |
| 45 | +} |
| 46 | +impl StdLogAssertExpected<'_> { |
| 47 | + fn assert_finished(&self) { |
| 48 | + let expected = self.expected.lock().unwrap(); |
| 49 | + // load fence implied (I think) |
| 50 | + let remaining = expected.len() - self.current_index.load(Ordering::Relaxed); |
| 51 | + assert!( |
| 52 | + remaining == 0, |
| 53 | + "Expected {remaining} more values (first={first:?}) {maybeLast}", |
| 54 | + first = expected.first().unwrap(), |
| 55 | + maybeLast = if remaining >= 2 { |
| 56 | + format!("(last={:?})", expected.last().unwrap()) |
| 57 | + } else { |
| 58 | + String::new() |
| 59 | + } |
| 60 | + ); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +macro_rules! record { |
| 65 | + ($level:ident, $fmt:expr) => { |
| 66 | + RecordBuilder::new() |
| 67 | + .args(format_args!($fmt)) |
| 68 | + .level(log::Level::$level) |
| 69 | + .file(Some(file!())) |
| 70 | + .module_path(Some(module_path!())) |
| 71 | + .target(module_path!()) |
| 72 | + .build() |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +#[test] |
| 77 | +fn test_slog2log() { |
| 78 | + let expected = vec![ |
| 79 | + record!(Info, "Hello World!"), |
| 80 | + record!(Debug, "Hello World, I am 100 years old") |
| 81 | + ].into_iter().map(Fragile::new).collect::<Vec<Fragile<log::Record>>>(); |
| 82 | + let std_logger = Box::leak(Box::new(StdLogAssertExpected { |
| 83 | + expected: Mutex::new(expected), |
| 84 | + current_index: 0.into(), |
| 85 | + })); |
| 86 | + log::set_logger(std_logger).unwrap(); |
| 87 | + let sl = slog::Logger::root(slog_stdlog::StdLog.fuse(), slog::o!()); |
| 88 | + slog::info!(sl, "Hello {}", "World!"); |
| 89 | + slog::debug!(sl, "Hello {}, I am {} years old", "World", 100); |
| 90 | + std_logger.assert_finished(); |
| 91 | +} |
0 commit comments