Skip to content

Commit

Permalink
Add log rotation and special build profile for dist
Browse files Browse the repository at this point in the history
  • Loading branch information
semoro committed Dec 5, 2021
1 parent ac81933 commit 5d4d5bd
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,7 @@ default = ["test_time", "test_utils", "bench_utils", "tools"]
test_time = []
test_utils = []
bench_utils = ["criterion", "test_utils"]
tools = []
tools = []

[profile.release-dist]
inherits = "release"
8 changes: 7 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");


Expand Down
23 changes: 20 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand All @@ -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!());

Expand Down

0 comments on commit 5d4d5bd

Please sign in to comment.