Pārlūkot izejas kodu

Merge #563

563: Print log to stdout in test r=Dirbaio a=deantvv

Use simple [trick](https://github.com/rust-lang/log/issues/106) to allow print log to stdout in test

Co-authored-by: Dean Li <[email protected]>
Co-authored-by: Dario Nieuwenhuis <[email protected]>
bors[bot] 3 gadi atpakaļ
vecāks
revīzija
c90d54ac42
3 mainītis faili ar 8 papildinājumiem un 57 dzēšanām
  1. 8 0
      src/macros.rs
  2. 0 27
      src/socket/dhcpv4.rs
  3. 0 30
      src/socket/tcp.rs

+ 8 - 0
src/macros.rs

@@ -1,9 +1,17 @@
+#[cfg(not(test))]
 #[cfg(feature = "log")]
 macro_rules! net_log {
     (trace, $($arg:expr),*) => { log::trace!($($arg),*) };
     (debug, $($arg:expr),*) => { log::debug!($($arg),*) };
 }
 
+#[cfg(test)]
+#[cfg(feature = "log")]
+macro_rules! net_log {
+    (trace, $($arg:expr),*) => { println!($($arg),*) };
+    (debug, $($arg:expr),*) => { println!($($arg),*) };
+}
+
 #[cfg(feature = "defmt")]
 macro_rules! net_log {
     (trace, $($arg:expr),*) => { defmt::trace!($($arg),*) };

+ 0 - 27
src/socket/dhcpv4.rs

@@ -651,30 +651,6 @@ mod test {
         });
     }
 
-    #[cfg(feature = "log")]
-    fn init_logger() {
-        struct Logger;
-        static LOGGER: Logger = Logger;
-
-        impl log::Log for Logger {
-            fn enabled(&self, _metadata: &log::Metadata) -> bool {
-                true
-            }
-
-            fn log(&self, record: &log::Record) {
-                println!("{}", record.args());
-            }
-
-            fn flush(&self) {}
-        }
-
-        // If it fails, that just means we've already set it to the same value.
-        let _ = log::set_logger(&LOGGER);
-        log::set_max_level(log::LevelFilter::Trace);
-
-        println!();
-    }
-
     // =========================================================================================//
     // Constants
 
@@ -822,9 +798,6 @@ mod test {
     // Tests
 
     fn socket() -> Dhcpv4Socket {
-        #[cfg(feature = "log")]
-        init_logger();
-
         let mut s = Dhcpv4Socket::new();
         assert_eq!(s.poll(), Some(Event::Deconfigured));
         s

+ 0 - 30
src/socket/tcp.rs

@@ -2650,38 +2650,11 @@ mod test {
         }};
     }
 
-    #[cfg(feature = "log")]
-    fn init_logger() {
-        struct Logger;
-        static LOGGER: Logger = Logger;
-
-        impl log::Log for Logger {
-            fn enabled(&self, _metadata: &log::Metadata) -> bool {
-                true
-            }
-
-            fn log(&self, record: &log::Record) {
-                println!("{}", record.args());
-            }
-
-            fn flush(&self) {}
-        }
-
-        // If it fails, that just means we've already set it to the same value.
-        let _ = log::set_logger(&LOGGER);
-        log::set_max_level(log::LevelFilter::Trace);
-
-        println!();
-    }
-
     fn socket() -> TcpSocket<'static> {
         socket_with_buffer_sizes(64, 64)
     }
 
     fn socket_with_buffer_sizes(tx_len: usize, rx_len: usize) -> TcpSocket<'static> {
-        #[cfg(feature = "log")]
-        init_logger();
-
         let rx_buffer = SocketBuffer::new(vec![0; rx_len]);
         let tx_buffer = SocketBuffer::new(vec![0; tx_len]);
         let mut socket = TcpSocket::new(rx_buffer, tx_buffer);
@@ -7050,9 +7023,6 @@ mod test {
 
     #[test]
     fn test_rtt_estimator() {
-        #[cfg(feature = "log")]
-        init_logger();
-
         let mut r = RttEstimator::default();
 
         let rtos = &[