Skip to main content

data_beans/
logging.rs

1//! Shared logger setup for the workspace.
2//!
3//! Reachable as `data_beans::logging` and, for callers that take it with the
4//! other helpers, as `data_beans::aux::logging`. The progress-bar style and
5//! the single [`MULTI_PROGRESS`] are defined in
6//! [`legume_numeric::matrix::progress`], the lowest common dependency of the
7//! workspace; this module re-exports [`new_progress_bar`], [`new_spinner`],
8//! and [`MULTI_PROGRESS`] from there, so every crate that draws a bar shares
9//! one set of them, and adds [`init_logger`],
10//! which wraps `env_logger` in `indicatif_log_bridge` so `log` output renders
11//! above the bars instead of corrupting them. With the `tui` feature the
12//! logger sits behind legume-tui's log gate, so a view's
13//! `legume_tui::logs::hold` keeps records back while it owns the terminal.
14
15pub use legume_numeric::matrix::progress::{new_progress_bar, new_spinner, MULTI_PROGRESS};
16
17/// Install `env_logger` wrapped in `indicatif_log_bridge::LogWrapper` so log
18/// messages render above any active progress bar. `verbose` selects between
19/// `legume_numeric::matrix::common_io::{VERBOSE,QUIET}_LOG_FILTER`; an external `RUST_LOG`
20/// still overrides the default filter.
21pub fn init_logger(verbose: bool) {
22    init_logger_with(if verbose {
23        legume_numeric::matrix::common_io::VERBOSE_LOG_FILTER
24    } else {
25        legume_numeric::matrix::common_io::QUIET_LOG_FILTER
26    });
27}
28
29/// [`init_logger`] with `default_filter` (`error`, `info`) where no
30/// `RUST_LOG` is set. For the `data-beans` binary's own default; not part
31/// of the library's surface.
32#[doc(hidden)]
33pub fn init_logger_with(default_filter: &str) {
34    let logger =
35        env_logger::Builder::from_env(env_logger::Env::default().default_filter_or(default_filter))
36            .build();
37    let max_level = logger.filter();
38    let wrapped = indicatif_log_bridge::LogWrapper::new(MULTI_PROGRESS.clone(), logger);
39    #[cfg(feature = "tui")]
40    let installed = log::set_logger(legume_tui::logs::gate(wrapped));
41    #[cfg(not(feature = "tui"))]
42    let installed = log::set_boxed_logger(Box::new(wrapped));
43    let _ = installed.map(|()| log::set_max_level(max_level));
44}