LCOV - code coverage report
Current view: top level - safekeeper/tests/walproposer_sim - log.rs (source / functions) Coverage Total Hit
Test: 792183ae0ef4f1f8b22e9ac7e8748740ab73f873.info Lines: 100.0 % 45 45
Test Date: 2024-06-26 01:04:33 Functions: 95.2 % 21 20

            Line data    Source code
       1              : use std::{fmt, sync::Arc};
       2              : 
       3              : use desim::time::Timing;
       4              : use once_cell::sync::OnceCell;
       5              : use parking_lot::Mutex;
       6              : use tracing_subscriber::fmt::{format::Writer, time::FormatTime};
       7              : 
       8              : /// SimClock can be plugged into tracing logger to print simulation time.
       9              : #[derive(Clone)]
      10              : pub struct SimClock {
      11              :     clock_ptr: Arc<Mutex<Option<Arc<Timing>>>>,
      12              : }
      13              : 
      14              : impl Default for SimClock {
      15           18 :     fn default() -> Self {
      16           18 :         SimClock {
      17           18 :             clock_ptr: Arc::new(Mutex::new(None)),
      18           18 :         }
      19           18 :     }
      20              : }
      21              : 
      22              : impl SimClock {
      23         4016 :     pub fn set_clock(&self, clock: Arc<Timing>) {
      24         4016 :         *self.clock_ptr.lock() = Some(clock);
      25         4016 :     }
      26              : }
      27              : 
      28              : impl FormatTime for SimClock {
      29        26506 :     fn format_time(&self, w: &mut Writer<'_>) -> fmt::Result {
      30        26506 :         let clock = self.clock_ptr.lock();
      31              : 
      32        26506 :         if let Some(clock) = clock.as_ref() {
      33        26496 :             let now = clock.now();
      34        26496 :             write!(w, "[{}]", now)
      35              :         } else {
      36           10 :             write!(w, "[?]")
      37              :         }
      38        26506 :     }
      39              : }
      40              : 
      41              : static LOGGING_DONE: OnceCell<SimClock> = OnceCell::new();
      42              : 
      43              : /// Returns ptr to clocks attached to tracing logger to update them when the
      44              : /// world is (re)created.
      45           18 : pub fn init_tracing_logger(debug_enabled: bool) -> SimClock {
      46           18 :     LOGGING_DONE
      47           18 :         .get_or_init(|| {
      48           18 :             let clock = SimClock::default();
      49           18 :             let base_logger = tracing_subscriber::fmt()
      50           18 :                 .with_target(false)
      51           18 :                 // prefix log lines with simulated time timestamp
      52           18 :                 .with_timer(clock.clone())
      53           18 :                 // .with_ansi(true) TODO
      54           18 :                 .with_max_level(match debug_enabled {
      55            4 :                     true => tracing::Level::DEBUG,
      56           14 :                     false => tracing::Level::WARN,
      57              :                 })
      58           18 :                 .with_writer(std::io::stdout);
      59           18 :             base_logger.init();
      60           18 : 
      61           18 :             // logging::replace_panic_hook_with_tracing_panic_hook().forget();
      62           18 : 
      63           18 :             if !debug_enabled {
      64       156434 :                 std::panic::set_hook(Box::new(|_| {}));
      65           14 :             }
      66              : 
      67           18 :             clock
      68           18 :         })
      69           18 :         .clone()
      70           18 : }
      71              : 
      72           14 : pub fn init_logger() -> SimClock {
      73           14 :     // RUST_TRACEBACK envvar controls whether we print all logs or only warnings.
      74           14 :     let debug_enabled = std::env::var("RUST_TRACEBACK").is_ok();
      75           14 : 
      76           14 :     init_tracing_logger(debug_enabled)
      77           14 : }
        

Generated by: LCOV version 2.1-beta