helixforge 1.0.7

Build, configure and optimise a Linux machine from a terminal menu: installs, dotfiles, updates, backups and health checks, every change planned and confirmed first.
//! Tracing to a log file in the state directory; `HELIXFORGE_LOG` sets the
//! level (error, warn, info, debug or trace; the default is info). The file
//! and its directory are private to the user, like the history beside them.

use helixforge_engine::history::state_dir;
use std::{
    fs,
    os::unix::fs::{DirBuilderExt, OpenOptionsExt},
};
use tracing::Level;

pub fn init() {
    let level = std::env::var("HELIXFORGE_LOG")
        .ok()
        .and_then(|value| value.parse::<Level>().ok())
        .unwrap_or(Level::INFO);
    let Some(directory) = state_dir().map(|state| state.join("logs")) else {
        return;
    };
    if fs::DirBuilder::new()
        .recursive(true)
        .mode(0o700)
        .create(&directory)
        .is_err()
    {
        return;
    }
    let Ok(file) = fs::OpenOptions::new()
        .append(true)
        .create(true)
        .mode(0o600)
        .open(directory.join("helixforge.log"))
    else {
        return;
    };
    let _ = tracing_subscriber::fmt()
        .with_max_level(level)
        .with_ansi(false)
        .with_writer(file)
        .try_init();
}