diff --git a/Cargo.toml b/Cargo.toml index 2b19e0d..226f2b1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -40,4 +40,7 @@ default = ["test_time", "test_utils", "bench_utils", "tools"] test_time = [] test_utils = [] bench_utils = ["criterion", "test_utils"] -tools = [] \ No newline at end of file +tools = [] + +[profile.release-dist] +inherits = "release" \ No newline at end of file diff --git a/build.rs b/build.rs index e1f7ce4..8c39eac 100644 --- a/build.rs +++ b/build.rs @@ -7,7 +7,13 @@ fn main() -> Result<()> { *config.build_mut().timezone_mut() = TimeZone::Local; println!("cargo:rerun-if-changed=src"); - println!("cargo:rustc-cfg=debug_deopt_print"); + if std::env::var("OUT_DIR").unwrap().contains("release-dist") { + println!("cargo:rustc-cfg=rotate_logs"); + println!("cargo:rustc-env=DMJIT_LOG_PREFIX=data/logs/"); + } else { + println!("cargo:rustc-cfg=debug_deopt_print"); + println!("cargo:rustc-env=DMJIT_LOG_PREFIX="); + } //println!("cargo:rustc-cfg=debug_on_call_print"); diff --git a/src/lib.rs b/src/lib.rs index 17d7eef..8138d8d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -36,6 +36,7 @@ use auxtools::hooks::call_counts; use log::LevelFilter; use std::panic::{UnwindSafe, catch_unwind}; +use std::path::Path; pub struct DisassembleEnv; @@ -118,6 +119,21 @@ pub fn dump_call_count() { Ok(Value::null()) } +macro_rules! log_file { + ($name:literal) => { concat!(env!("DMJIT_LOG_PREFIX"), $name) }; +} + +fn rotate_logs(from: &Path, num: u32) { + let target_name = format!(log_file!("dmjit.log.{}"), num); + let target = Path::new(target_name.as_str()); + if target.exists() && num < 10 { + rotate_logs(target, num + 1) + } + if let Err(error) = std::fs::copy(from, target) { + log::error!("Failed to rotate logs ({:?} -> {:?}): {}", from, target, error) + } +} + #[hook("/proc/dmjit_hook_log_init")] pub fn log_init() { macro_rules! ver_string { @@ -126,9 +142,10 @@ pub fn log_init() { }; } - - - simple_logging::log_to_file("dmjit.log", LevelFilter::Debug).unwrap(); + if cfg!(rotate_logs) { + rotate_logs(Path::new(log_file!("dmjit.log")), 0); + } + simple_logging::log_to_file(log_file!("dmjit.log"), LevelFilter::Debug).unwrap(); log_panics::init(); log::info!("Log startup, {}", ver_string!());