fix: let logger use the crate name of the end-crate #91
cargo devel CI / cargo CI (push) Has been cancelled Details

This commit is contained in:
Christoph J. Scherr 2024-08-30 17:39:10 +02:00
parent 687fd94870
commit 442e17f9a8
2 changed files with 20 additions and 1 deletions

View File

@ -8,3 +8,16 @@
/// macros to make things faster in your code /// macros to make things faster in your code
pub mod macros; pub mod macros;
/// ## Get the name of the crate that uses your library
///
/// Let's say you're writing the library `foo` and need the name of the crate that uses `foo`. With
/// this function, you can get the name of the crate that uses `foo`.
///
/// Will return [None] if [`std::env::current_exe()`] errors or if conversion to [String] from [std::ffi::OsStr] fails.
pub fn get_crate_name() -> Option<String> {
if let Ok(exe) = std::env::current_exe() {
return Some(exe.file_stem()?.to_str()?.to_string());
}
None
}

View File

@ -480,5 +480,11 @@ impl Default for Logger {
} }
fn new_file_appender(log_dir: PathBuf) -> tracing_appender::rolling::RollingFileAppender { fn new_file_appender(log_dir: PathBuf) -> tracing_appender::rolling::RollingFileAppender {
tracing_appender::rolling::daily(log_dir, format!("{}.log", env!("CARGO_CRATE_NAME"))) tracing_appender::rolling::daily(
log_dir,
format!(
"{}.log",
libpt_core::get_crate_name().unwrap_or_else(|| "logfile".to_string())
),
)
} }