2025-12-27 01:48:17 -03:00
|
|
|
use anyhow::{Context, Result};
|
2025-12-20 19:01:40 -03:00
|
|
|
use tracing_subscriber::Layer;
|
|
|
|
|
use tracing_subscriber::layer::SubscriberExt;
|
|
|
|
|
use tracing_subscriber::util::SubscriberInitExt;
|
|
|
|
|
|
2025-12-27 01:48:17 -03:00
|
|
|
use crate::constants::{APP_NAME, SNAKE_CASE_APP_NAME};
|
|
|
|
|
use crate::directories::data_directory;
|
2025-12-20 19:01:40 -03:00
|
|
|
|
2025-12-27 01:48:17 -03:00
|
|
|
pub enum LogMode {
|
|
|
|
|
File,
|
|
|
|
|
Print,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Sets up logging for the application.
|
|
|
|
|
pub async fn initialize_logging(mode: LogMode) -> Result<()> {
|
|
|
|
|
match mode {
|
|
|
|
|
LogMode::File => initialize_file_logging().await,
|
|
|
|
|
LogMode::Print => initialize_print_logging(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn initialize_file_logging() -> Result<()> {
|
|
|
|
|
let user_directory = data_directory()
|
|
|
|
|
.await
|
|
|
|
|
.context("While initializing logging")?;
|
2025-12-20 19:01:40 -03:00
|
|
|
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!(
|
2025-12-27 01:48:17 -03:00
|
|
|
"{SNAKE_CASE_APP_NAME}=debug"
|
2025-12-20 19:01:40 -03:00
|
|
|
)));
|
|
|
|
|
tracing_subscriber::registry().with(file_subscriber).init();
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
2025-12-27 01:48:17 -03:00
|
|
|
|
|
|
|
|
fn initialize_print_logging() -> Result<()> {
|
|
|
|
|
tracing_subscriber::registry()
|
|
|
|
|
.with(tracing_subscriber::filter::EnvFilter::from(format!(
|
|
|
|
|
"{SNAKE_CASE_APP_NAME}=debug"
|
|
|
|
|
)))
|
|
|
|
|
.with(tracing_subscriber::fmt::layer())
|
|
|
|
|
.init();
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|