diff --git a/Cargo.lock b/Cargo.lock index 499380a..25246a4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1152,6 +1152,7 @@ dependencies = [ name = "pop-launcher-bin" version = "1.2.0" dependencies = [ + "dirs 4.0.0", "pop-launcher-plugins", "pop-launcher-service", "tokio", diff --git a/bin/Cargo.toml b/bin/Cargo.toml index 546cbf0..1527b08 100644 --- a/bin/Cargo.toml +++ b/bin/Cargo.toml @@ -12,7 +12,8 @@ pop-launcher-plugins = { path = "../plugins" } pop-launcher-service = { path = "../service" } tracing = "0.1.32" tracing-subscriber = { version = "0.3.9", features = ["env-filter"] } +dirs = "4.0.0" [dependencies.tokio] version = "1.17.0" -features = ["rt"] \ No newline at end of file +features = ["rt"] diff --git a/bin/src/main.rs b/bin/src/main.rs index e48a0c2..d0cf714 100644 --- a/bin/src/main.rs +++ b/bin/src/main.rs @@ -3,18 +3,15 @@ use pop_launcher_plugins as plugins; use pop_launcher_service as service; -use std::io; #[tokio::main(flavor = "current_thread")] async fn main() { - tracing_subscriber::fmt() - .with_writer(io::stderr) - .with_env_filter(tracing_subscriber::EnvFilter::from_default_env()) - .init(); - if let Some(plugin) = std::env::args().next() { let start = plugin.rfind('/').map(|v| v + 1).unwrap_or(0); let cmd = &plugin.as_str()[start..]; + + init_logging(cmd); + match cmd { "calc" => plugins::calc::main().await, "desktop-entries" => plugins::desktop_entries::main().await, @@ -33,3 +30,28 @@ async fn main() { } } } + +fn init_logging(cmd: &str) { + let logdir = match dirs::state_dir() { + Some(dir) => dir.join("pop-launcher/"), + None => dirs::home_dir() + .expect("home directory required") + .join(".cache/pop-launcher"), + }; + + let _ = std::fs::create_dir_all(&logdir); + + let logfile = std::fs::OpenOptions::new() + .create(true) + .truncate(true) + .write(true) + .open(logdir.join([cmd, ".log"].concat().as_str()).as_path()); + + if let Ok(file) = logfile { + use tracing_subscriber::{fmt, EnvFilter}; + fmt() + .with_env_filter(EnvFilter::from_default_env()) + .with_writer(file) + .init(); + } +}