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.
This commit is contained in:
LGUG2Z
2021-08-18 06:21:16 -07:00
parent 23aada05d0
commit 13b335cecc
4 changed files with 37 additions and 7 deletions

18
Cargo.lock generated
View File

@@ -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",

View File

@@ -62,7 +62,7 @@ lazy_static! {
]));
}
fn setup() -> Result<WorkerGuard> {
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<WorkerGuard> {
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<WorkerGuard> {
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<WorkerGuard> {
}
}));
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)?;

View File

@@ -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"] }

View File

@@ -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()?)?;
}