32 lines
1.2 KiB
Rust
32 lines
1.2 KiB
Rust
use anyhow::{Result, anyhow};
|
|
use camino::Utf8PathBuf;
|
|
use directories::ProjectDirs;
|
|
use tracing_subscriber::Layer;
|
|
use tracing_subscriber::layer::SubscriberExt;
|
|
use tracing_subscriber::util::SubscriberInitExt;
|
|
|
|
const APP_NAME: &str = "ptcg-tools";
|
|
|
|
/// Sets up logging for the application. Since the mode we will need logging for is a TUI, we can't
|
|
/// print our logs into stdout, so we will log into a file instead.
|
|
pub async fn initialize_logging() -> Result<()> {
|
|
let user_directory = ProjectDirs::from("cl", "bstr", APP_NAME)
|
|
.ok_or_else(|| anyhow!("failed to get ProjectDirs"))?
|
|
.data_dir()
|
|
.to_path_buf();
|
|
let user_directory = Utf8PathBuf::try_from(user_directory)?;
|
|
tokio::fs::create_dir_all(&user_directory).await?;
|
|
let log_path = user_directory.join(format!("{APP_NAME}.log"));
|
|
let log_file = std::fs::File::create(log_path)?;
|
|
let file_subscriber = tracing_subscriber::fmt::layer()
|
|
.with_file(true)
|
|
.with_line_number(true)
|
|
.with_writer(log_file)
|
|
.with_target(false)
|
|
.with_ansi(false)
|
|
.with_filter(tracing_subscriber::filter::EnvFilter::from(format!(
|
|
"{APP_NAME}=debug"
|
|
)));
|
|
tracing_subscriber::registry().with(file_subscriber).init();
|
|
Ok(())
|
|
}
|