fix: mark unsafety of std::env::set_var

and document the function.
This commit is contained in:
Gwen Lg
2026-01-27 15:32:12 +01:00
parent 5c8a31708e
commit 473b66ca5b
2 changed files with 10 additions and 2 deletions
+6 -1
View File
@@ -181,13 +181,18 @@ async fn run(opt: Opt) -> Result<(), Error> {
}
}
/// # Safety
///
/// should be called before tokio runtime initialization
/// to limit multithread problem with `std::env::set_var` which is unsafe
fn init_logging(opt: &Opt) {
if std::env::var("RUST_LOG").is_err() {
let default_log = match &opt.cmd {
Command::Server => "netapp=info,garage=info",
_ => "netapp=warn,garage=warn",
};
std::env::set_var("RUST_LOG", default_log)
unsafe { std::env::set_var("RUST_LOG", default_log) };
}
let env_filter = tracing_subscriber::filter::EnvFilter::from_default_env();
+4 -1
View File
@@ -388,9 +388,12 @@ impl Filter {
}
}
/// # Safety
///
/// initialize `RUST_LOG` env var before start tokio runtime to limit multithread problem with `std::env::set_var` which is unsafe
fn main() -> Result<(), Error> {
if std::env::var("RUST_LOG").is_err() {
std::env::set_var("RUST_LOG", "warn")
unsafe { std::env::set_var("RUST_LOG", "warn") };
}
tracing_subscriber::fmt()