Skip to main content

data_beans/aux/
logging.rs

1//! Shared logger setup for the workspace.
2//!
3//! The progress-bar style and the single [`MULTI_PROGRESS`] now live in
4//! [`legume_numeric::matrix::progress`] — the lowest common dependency, so `data-beans`
5//! and friends can share them without a dependency cycle (this crate depends
6//! *on* `data-beans`, so the primitive can't live here). This module
7//! re-exports [`new_progress_bar`], [`new_spinner`], and [`MULTI_PROGRESS`] so
8//! every existing caller (`data_beans::aux::logging::new_progress_bar`, senna,
9//! graph-embedding-util) keeps working unchanged, 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}