use crate::eval::RuleMatch;
use log::info;
use simplelog::{Config, LevelFilter, WriteLogger};
use std::sync::Once;
static INIT: Once = Once::new();
pub fn init() {
INIT.call_once(|| {
let Some(home) = std::env::var_os("HOME") else {
return;
};
let log_dir = std::path::Path::new(&home).join(".local/share/cc-toolgate");
let _ = std::fs::create_dir_all(&log_dir);
let log_path = log_dir.join("decisions.log");
let Ok(file) = std::fs::OpenOptions::new()
.create(true)
.append(true)
.open(log_path)
else {
return;
};
let _ = WriteLogger::init(LevelFilter::Info, Config::default(), file);
});
}
pub fn log_decision(command: &str, result: &RuleMatch) {
let reason_oneline = result.reason.replace('\n', "; ");
let cmd_truncated: String = command.chars().take(200).collect();
info!(
"{decision}\t{cmd}\t{reason}",
decision = result.decision.as_str(),
cmd = cmd_truncated,
reason = reason_oneline,
);
}