From 13b335cecc2dd6d9a3141f46263589381affaa17 Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Wed, 18 Aug 2021 06:21:16 -0700 Subject: [PATCH] feat(komorebic): add log command This commit adds a log command directly to the komorebic cli to make it easier for users to check the logs if they don't have tail installed or are not familiar with it. A separate logfile with ANSI color codes is now being written to the user's tempdir, which is tailed by the log command until the process is halted by a Ctrl-C signal. --- Cargo.lock | 18 ++++++++++++++---- komorebi/src/main.rs | 13 ++++++++++--- komorebic/Cargo.toml | 1 + komorebic/src/main.rs | 12 ++++++++++++ 4 files changed, 37 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d4430d43..52f4422a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -274,6 +274,15 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "fs-tail" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73c8ee8694b2ad6d79aa976ad8572ca376c0450290041e6e3ae75147356b6ad2" +dependencies = [ + "memchr", +] + [[package]] name = "fsevent" version = "0.4.0" @@ -488,6 +497,7 @@ dependencies = [ "clap", "color-eyre", "dirs", + "fs-tail", "komorebi-core", "paste", "powershell_script", @@ -1151,9 +1161,9 @@ dependencies = [ [[package]] name = "tracing-core" -version = "0.1.18" +version = "0.1.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a9ff14f98b1a4b289c6248a023c1c2fa1491062964e9fed67ab29c4e4da4a052" +checksum = "2ca517f43f0fb96e0c3072ed5c275fe5eece87e8cb52f4a77b69226d3b1c9df8" dependencies = [ "lazy_static", ] @@ -1191,9 +1201,9 @@ dependencies = [ [[package]] name = "tracing-subscriber" -version = "0.2.19" +version = "0.2.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ab69019741fca4d98be3c62d2b75254528b5432233fd8a4d2739fec20278de48" +checksum = "b9cbe87a2fa7e35900ce5de20220a582a9483a7063811defce79d7cbd59d4cfe" dependencies = [ "ansi_term", "chrono", diff --git a/komorebi/src/main.rs b/komorebi/src/main.rs index ab344e02..f119a49c 100644 --- a/komorebi/src/main.rs +++ b/komorebi/src/main.rs @@ -62,7 +62,7 @@ lazy_static! { ])); } -fn setup() -> Result { +fn setup() -> Result<(WorkerGuard, WorkerGuard)> { if std::env::var("RUST_LIB_BACKTRACE").is_err() { std::env::set_var("RUST_LIB_BACKTRACE", "1"); } @@ -75,7 +75,9 @@ fn setup() -> Result { let home = dirs::home_dir().context("there is no home directory")?; let appender = tracing_appender::rolling::never(home, "komorebi.log"); + let color_appender = tracing_appender::rolling::never(std::env::temp_dir(), "komorebi.log"); let (non_blocking, guard) = tracing_appender::non_blocking(appender); + let (color_non_blocking, color_guard) = tracing_appender::non_blocking(color_appender); tracing::subscriber::set_global_default( tracing_subscriber::fmt::Subscriber::builder() @@ -85,6 +87,11 @@ fn setup() -> Result { tracing_subscriber::fmt::Layer::default() .with_writer(non_blocking) .with_ansi(false), + ) + .with( + tracing_subscriber::fmt::Layer::default() + .with_writer(color_non_blocking) + .with_ansi(true), ), )?; @@ -113,7 +120,7 @@ fn setup() -> Result { } })); - Ok(guard) + Ok((guard, color_guard)) } pub fn load_configuration() -> Result<()> { @@ -167,7 +174,7 @@ fn main() -> Result<()> { } // File logging worker guard has to have an assignment in the main fn to work - let _guard = setup()?; + let (_guard, _color_guard) = setup()?; let process_id = WindowsApi::current_process_id(); WindowsApi::allow_set_foreground_window(process_id)?; diff --git a/komorebic/Cargo.toml b/komorebic/Cargo.toml index 4debf671..4919f978 100644 --- a/komorebic/Cargo.toml +++ b/komorebic/Cargo.toml @@ -17,6 +17,7 @@ komorebi-core = { path = "../komorebi-core" } clap = "3.0.0-beta.4" color-eyre = "0.5" dirs = "3" +fs-tail = "0.1" paste = "1" powershell_script = "0.2" serde = { version = "1", features = ["derive"] } diff --git a/komorebic/src/main.rs b/komorebic/src/main.rs index c0d0b581..ae489150 100644 --- a/komorebic/src/main.rs +++ b/komorebic/src/main.rs @@ -11,6 +11,7 @@ use clap::ArgEnum; use clap::Clap; use color_eyre::eyre::ContextCompat; use color_eyre::Result; +use fs_tail::TailedFile; use paste::paste; use uds_windows::UnixListener; use uds_windows::UnixStream; @@ -178,6 +179,8 @@ enum SubCommand { Stop, /// Show a JSON representation of the current window manager state State, + /// Tail komorebi.exe's process logs (cancel with Ctrl-C) + Log, /// Change focus to the window in the specified direction #[clap(setting = AppSettings::ArgRequiredElseHelp)] Focus(Focus), @@ -277,6 +280,15 @@ fn main() -> Result<()> { let opts: Opts = Opts::parse(); match opts.subcmd { + SubCommand::Log => { + let mut color_log = std::env::temp_dir(); + color_log.push("komorebi.log"); + let file = TailedFile::new(File::open(color_log)?); + let locked = file.lock(); + for line in locked.lines() { + println!("{}", line?) + } + } SubCommand::Focus(arg) => { send_message(&*SocketMessage::FocusWindow(arg.operation_direction).as_bytes()?)?; }