Skip to main content

r/
lib.rs

1pub mod interpreter;
2pub mod parser;
3#[cfg(feature = "repl")]
4pub mod repl;
5pub mod session;
6
7pub use session::{is_invisible_result, EvalOutput, Session, SessionError};
8
9/// Initialize logging and tracing from the `MINIR_LOG` environment variable.
10///
11/// When the `tracing-output` feature is enabled (default), this configures
12/// `tracing-subscriber` with an env filter read from `MINIR_LOG` (e.g.
13/// `MINIR_LOG=debug`). The tracing subscriber also bridges `log` records, so
14/// existing `log::*` calls are captured too.
15///
16/// When only the `logging` feature is enabled, this configures `env_logger`
17/// as a fallback. When neither feature is enabled, this is a no-op.
18///
19/// # Examples
20///
21/// ```bash
22/// MINIR_LOG=debug cargo run -- -e '1+1'   # shows evaluation trace
23/// MINIR_LOG=trace cargo run -- -e '1+1'   # shows all trace-level detail
24/// MINIR_LOG=r=debug cargo run -- -e '1+1' # only miniR debug output
25/// ```
26pub fn init_logging() {
27    #[cfg(feature = "tracing-output")]
28    {
29        use std::sync::Once;
30        static INIT: Once = Once::new();
31        use std::env;
32        if env::var("MINIR_LOG").is_ok() {
33            INIT.call_once(|| {
34                use tracing_subscriber::EnvFilter;
35                let filter =
36                    EnvFilter::try_from_env("MINIR_LOG").unwrap_or_else(|_| EnvFilter::new("info"));
37                tracing_subscriber::fmt()
38                    .with_env_filter(filter)
39                    .without_time()
40                    .with_target(false)
41                    .init();
42            });
43        }
44    }
45
46    // env_logger fallback removed — tracing-subscriber handles all logging
47}
48
49#[cfg(all(test, feature = "tracing-output"))]
50mod tests {
51    use super::init_logging;
52
53    #[test]
54    fn init_logging_is_idempotent() {
55        std::env::set_var("MINIR_LOG", "debug");
56        init_logging();
57        init_logging();
58    }
59}