feat(cli): add log cmd

This commit adds logs written to files in the app's datadir, which can
then be tailed by the komorebic log command. temp_dir() was being weird
so I just stuck with DATA_DIR.
This commit is contained in:
LGUG2Z committed 2025-09-22 15:55:19 -07:00
1 parent 7467b8e30a
commit d77ae2d03a
7 files changed
+140 -11

No files matched your search

Generated
+49 -3
View File
@@ -180,8 +180,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "145052bdd345b87320e369255277e3fb5152762ad123a901ef5c262dd38fe8d2"
dependencies = [
"iana-time-zone",
"js-sys",
"num-traits",
"serde",
"wasm-bindgen",
"windows-link 0.2.0",
]
@@ -498,6 +500,15 @@ dependencies = [
"percent-encoding",
]
[[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-sys"
version = "4.1.0"
@@ -1023,8 +1034,9 @@ dependencies = [
"serde_with",
"strum",
"sysinfo",
"thiserror",
"thiserror 2.0.16",
"tracing",
"tracing-appender",
"tracing-subscriber",
]
@@ -1040,9 +1052,11 @@ dependencies = [
name = "komorebic"
version = "0.1.0"
dependencies = [
"chrono",
"clap",
"color-eyre",
"dirs",
"fs-tail",
"komorebi-client",
"lazy_static",
"pastey",
@@ -1720,7 +1734,7 @@ checksum = "a4e608c6638b9c18977b00b475ac1f28d14e84b27d8d42f70e0bf1e3dec127ac"
dependencies = [
"getrandom 0.2.16",
"libredox",
"thiserror",
"thiserror 2.0.16",
]
[[package]]
@@ -2226,13 +2240,33 @@ dependencies = [
"windows-sys 0.60.2",
]
[[package]]
name = "thiserror"
version = "1.0.69"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52"
dependencies = [
"thiserror-impl 1.0.69",
]
[[package]]
name = "thiserror"
version = "2.0.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3467d614147380f2e4e374161426ff399c91084acd2363eaf549172b3d5e60c0"
dependencies = [
"thiserror-impl",
"thiserror-impl 2.0.16",
]
[[package]]
name = "thiserror-impl"
version = "1.0.69"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
@@ -2402,6 +2436,18 @@ dependencies = [
"tracing-core",
]
[[package]]
name = "tracing-appender"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3566e8ce28cc0a3fe42519fc80e6b4c943cc4c8cef275620eb8dac2d3d4e06cf"
dependencies = [
"crossbeam-channel",
"thiserror 1.0.69",
"time",
"tracing-subscriber",
]
[[package]]
name = "tracing-attributes"
version = "0.1.30"
+2
View File
@@ -9,6 +9,8 @@ members = [
[workspace.dependencies]
clap = { version = "4", features = ["derive", "wrap_help"] }
chrono-tz = "0.10"
chrono = "0.4"
color-eyre = "0.6"
dirs = "6"
lazy_static = "1"
+22
View File
@@ -21,6 +21,7 @@
"base64 0.22.1 registry+https://github.com/rust-lang/crates.io-index",
"bitflags 2.9.4 registry+https://github.com/rust-lang/crates.io-index",
"cfg-if 1.0.3 registry+https://github.com/rust-lang/crates.io-index",
"chrono 0.4.42 registry+https://github.com/rust-lang/crates.io-index",
"clap 4.5.48 registry+https://github.com/rust-lang/crates.io-index",
"clap_builder 4.5.48 registry+https://github.com/rust-lang/crates.io-index",
"clap_derive 4.5.47 registry+https://github.com/rust-lang/crates.io-index",
@@ -33,6 +34,7 @@
"crossbeam-channel 0.5.15 registry+https://github.com/rust-lang/crates.io-index",
"crossbeam-utils 0.8.21 registry+https://github.com/rust-lang/crates.io-index",
"ctrlc 3.5.0 registry+https://github.com/rust-lang/crates.io-index",
"deranged 0.5.3 registry+https://github.com/rust-lang/crates.io-index",
"dirs 6.0.0 registry+https://github.com/rust-lang/crates.io-index",
"dirs-sys 0.5.0 registry+https://github.com/rust-lang/crates.io-index",
"dispatch2 0.3.0 registry+https://github.com/rust-lang/crates.io-index",
@@ -59,6 +61,7 @@
"http 1.3.1 registry+https://github.com/rust-lang/crates.io-index",
"httparse 1.10.1 registry+https://github.com/rust-lang/crates.io-index",
"hyper-tls 0.6.0 registry+https://github.com/rust-lang/crates.io-index",
"iana-time-zone 0.1.64 registry+https://github.com/rust-lang/crates.io-index",
"ident_case 1.0.1 registry+https://github.com/rust-lang/crates.io-index",
"idna 1.1.0 registry+https://github.com/rust-lang/crates.io-index",
"idna_adapter 1.2.1 registry+https://github.com/rust-lang/crates.io-index",
@@ -75,6 +78,8 @@
"mime 0.3.17 registry+https://github.com/rust-lang/crates.io-index",
"miniz_oxide 0.8.9 registry+https://github.com/rust-lang/crates.io-index",
"native-tls 0.2.14 registry+https://github.com/rust-lang/crates.io-index",
"num-conv 0.1.0 registry+https://github.com/rust-lang/crates.io-index",
"num-traits 0.2.19 registry+https://github.com/rust-lang/crates.io-index",
"objc2-app-kit 0.3.1 registry+https://github.com/rust-lang/crates.io-index",
"objc2-application-services 0.3.1 registry+https://github.com/rust-lang/crates.io-index",
"objc2-cloud-kit 0.3.1 registry+https://github.com/rust-lang/crates.io-index",
@@ -93,6 +98,7 @@
"percent-encoding 2.3.2 registry+https://github.com/rust-lang/crates.io-index",
"pin-project-lite 0.2.16 registry+https://github.com/rust-lang/crates.io-index",
"pin-utils 0.1.0 registry+https://github.com/rust-lang/crates.io-index",
"powerfmt 0.2.0 registry+https://github.com/rust-lang/crates.io-index",
"ppv-lite86 0.2.21 registry+https://github.com/rust-lang/crates.io-index",
"proc-macro2 1.0.101 registry+https://github.com/rust-lang/crates.io-index",
"quote 1.0.40 registry+https://github.com/rust-lang/crates.io-index",
@@ -126,9 +132,13 @@
"system-configuration-sys 0.6.0 registry+https://github.com/rust-lang/crates.io-index",
"tempfile 3.22.0 registry+https://github.com/rust-lang/crates.io-index",
"terminal_size 0.4.3 registry+https://github.com/rust-lang/crates.io-index",
"thiserror 1.0.69 registry+https://github.com/rust-lang/crates.io-index",
"thiserror 2.0.16 registry+https://github.com/rust-lang/crates.io-index",
"thiserror-impl 1.0.69 registry+https://github.com/rust-lang/crates.io-index",
"thiserror-impl 2.0.16 registry+https://github.com/rust-lang/crates.io-index",
"thread_local 1.1.9 registry+https://github.com/rust-lang/crates.io-index",
"time 0.3.44 registry+https://github.com/rust-lang/crates.io-index",
"time-core 0.1.6 registry+https://github.com/rust-lang/crates.io-index",
"unicode-ident 1.0.19 registry+https://github.com/rust-lang/crates.io-index",
"url 2.5.7 registry+https://github.com/rust-lang/crates.io-index",
"utf8_iter 1.0.4 registry+https://github.com/rust-lang/crates.io-index",
@@ -195,6 +205,7 @@
"bytes 1.10.1 registry+https://github.com/rust-lang/crates.io-index",
"cfg-if 1.0.3 registry+https://github.com/rust-lang/crates.io-index",
"cfg_aliases 0.2.1 registry+https://github.com/rust-lang/crates.io-index",
"chrono 0.4.42 registry+https://github.com/rust-lang/crates.io-index",
"clap 4.5.48 registry+https://github.com/rust-lang/crates.io-index",
"clap_builder 4.5.48 registry+https://github.com/rust-lang/crates.io-index",
"clap_derive 4.5.47 registry+https://github.com/rust-lang/crates.io-index",
@@ -210,6 +221,7 @@
"darling 0.21.3 registry+https://github.com/rust-lang/crates.io-index",
"darling_core 0.21.3 registry+https://github.com/rust-lang/crates.io-index",
"darling_macro 0.21.3 registry+https://github.com/rust-lang/crates.io-index",
"deranged 0.5.3 registry+https://github.com/rust-lang/crates.io-index",
"dirs 6.0.0 registry+https://github.com/rust-lang/crates.io-index",
"dirs-sys 0.5.0 registry+https://github.com/rust-lang/crates.io-index",
"dispatch 0.2.0 registry+https://github.com/rust-lang/crates.io-index",
@@ -223,6 +235,7 @@
"filetime 0.2.26 registry+https://github.com/rust-lang/crates.io-index",
"fnv 1.0.7 registry+https://github.com/rust-lang/crates.io-index",
"form_urlencoded 1.2.2 registry+https://github.com/rust-lang/crates.io-index",
"fs-tail 0.1.4 registry+https://github.com/rust-lang/crates.io-index",
"fsevent-sys 4.1.0 registry+https://github.com/rust-lang/crates.io-index",
"futures-channel 0.3.31 registry+https://github.com/rust-lang/crates.io-index",
"futures-core 0.3.31 registry+https://github.com/rust-lang/crates.io-index",
@@ -243,6 +256,7 @@
"hyper 1.7.0 registry+https://github.com/rust-lang/crates.io-index",
"hyper-tls 0.6.0 registry+https://github.com/rust-lang/crates.io-index",
"hyper-util 0.1.17 registry+https://github.com/rust-lang/crates.io-index",
"iana-time-zone 0.1.64 registry+https://github.com/rust-lang/crates.io-index",
"ident_case 1.0.1 registry+https://github.com/rust-lang/crates.io-index",
"idna 1.1.0 registry+https://github.com/rust-lang/crates.io-index",
"idna_adapter 1.2.1 registry+https://github.com/rust-lang/crates.io-index",
@@ -265,6 +279,8 @@
"native-tls 0.2.14 registry+https://github.com/rust-lang/crates.io-index",
"nix 0.30.1 registry+https://github.com/rust-lang/crates.io-index",
"nu-ansi-term 0.50.1 registry+https://github.com/rust-lang/crates.io-index",
"num-conv 0.1.0 registry+https://github.com/rust-lang/crates.io-index",
"num-traits 0.2.19 registry+https://github.com/rust-lang/crates.io-index",
"objc2 0.6.2 registry+https://github.com/rust-lang/crates.io-index",
"objc2-app-kit 0.3.1 registry+https://github.com/rust-lang/crates.io-index",
"objc2-application-services 0.3.1 registry+https://github.com/rust-lang/crates.io-index",
@@ -287,6 +303,7 @@
"percent-encoding 2.3.2 registry+https://github.com/rust-lang/crates.io-index",
"pin-project-lite 0.2.16 registry+https://github.com/rust-lang/crates.io-index",
"pin-utils 0.1.0 registry+https://github.com/rust-lang/crates.io-index",
"powerfmt 0.2.0 registry+https://github.com/rust-lang/crates.io-index",
"ppv-lite86 0.2.21 registry+https://github.com/rust-lang/crates.io-index",
"proc-macro2 1.0.101 registry+https://github.com/rust-lang/crates.io-index",
"quick-xml 0.17.2 registry+https://github.com/rust-lang/crates.io-index",
@@ -329,9 +346,13 @@
"system-configuration-sys 0.6.0 registry+https://github.com/rust-lang/crates.io-index",
"tempfile 3.22.0 registry+https://github.com/rust-lang/crates.io-index",
"terminal_size 0.4.3 registry+https://github.com/rust-lang/crates.io-index",
"thiserror 1.0.69 registry+https://github.com/rust-lang/crates.io-index",
"thiserror 2.0.16 registry+https://github.com/rust-lang/crates.io-index",
"thiserror-impl 1.0.69 registry+https://github.com/rust-lang/crates.io-index",
"thiserror-impl 2.0.16 registry+https://github.com/rust-lang/crates.io-index",
"thread_local 1.1.9 registry+https://github.com/rust-lang/crates.io-index",
"time 0.3.44 registry+https://github.com/rust-lang/crates.io-index",
"time-core 0.1.6 registry+https://github.com/rust-lang/crates.io-index",
"tokio 1.47.1 registry+https://github.com/rust-lang/crates.io-index",
"tokio-native-tls 0.3.1 registry+https://github.com/rust-lang/crates.io-index",
"tokio-util 0.7.16 registry+https://github.com/rust-lang/crates.io-index",
@@ -340,6 +361,7 @@
"tower-layer 0.3.3 registry+https://github.com/rust-lang/crates.io-index",
"tower-service 0.3.3 registry+https://github.com/rust-lang/crates.io-index",
"tracing 0.1.41 registry+https://github.com/rust-lang/crates.io-index",
"tracing-appender 0.2.3 registry+https://github.com/rust-lang/crates.io-index",
"tracing-attributes 0.1.30 registry+https://github.com/rust-lang/crates.io-index",
"tracing-core 0.1.34 registry+https://github.com/rust-lang/crates.io-index",
"tracing-error 0.2.1 registry+https://github.com/rust-lang/crates.io-index",
+1
View File
@@ -31,4 +31,5 @@ strum = { version = "0.27", features = ["derive"] }
sysinfo = { workspace = true }
thiserror = { workspace = true }
tracing = "0.1"
tracing-appender = "0.2"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
+48 -8
View File
@@ -1,6 +1,7 @@
#![warn(clippy::all)]
use clap::Parser;
use clap::ValueEnum;
use color_eyre::eyre;
use color_eyre::eyre::OptionExt;
use komorebi::DATA_DIR;
@@ -27,6 +28,7 @@ use objc2_core_graphics::CGMainDisplayID;
use objc2_core_graphics::CGPreflightScreenCaptureAccess;
use objc2_core_graphics::CGRequestScreenCaptureAccess;
use parking_lot::Mutex;
use serde::Deserialize;
use std::path::PathBuf;
use std::sync::Arc;
use std::sync::atomic::AtomicBool;
@@ -34,7 +36,9 @@ use std::sync::atomic::Ordering;
use std::sync::mpsc;
use sysinfo::Process;
use sysinfo::ProcessesToUpdate;
use tracing_appender::non_blocking::WorkerGuard;
use tracing_subscriber::EnvFilter;
use tracing_subscriber::layer::SubscriberExt;
fn check_permissions() -> eyre::Result<()> {
unsafe {
@@ -54,7 +58,7 @@ fn check_permissions() -> eyre::Result<()> {
}
}
pub fn setup() -> eyre::Result<()> {
fn setup(log_level: LogLevel) -> eyre::Result<(WorkerGuard, WorkerGuard)> {
if std::env::var("RUST_LIB_BACKTRACE").is_err() {
unsafe {
std::env::set_var("RUST_LIB_BACKTRACE", "1");
@@ -65,13 +69,38 @@ pub fn setup() -> eyre::Result<()> {
if std::env::var("RUST_LOG").is_err() {
unsafe {
std::env::set_var("RUST_LOG", "info");
std::env::set_var(
"RUST_LOG",
match log_level {
LogLevel::Error => "error",
LogLevel::Warn => "warn",
LogLevel::Info => "info",
LogLevel::Debug => "debug",
LogLevel::Trace => "trace",
},
);
}
}
let appender = tracing_appender::rolling::daily(&*DATA_DIR, "komorebi_plaintext.log");
let color_appender = tracing_appender::rolling::daily(&*DATA_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()
.with_env_filter(EnvFilter::from_default_env())
.finish(),
.finish()
.with(
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),
),
)?;
// https://github.com/tokio-rs/tracing/blob/master/examples/examples/panic_hook.rs
@@ -102,7 +131,18 @@ pub fn setup() -> eyre::Result<()> {
);
}));
Ok(())
Ok((guard, color_guard))
}
#[derive(Default, Deserialize, ValueEnum, Clone)]
#[serde(rename_all = "snake_case")]
enum LogLevel {
Error,
Warn,
#[default]
Info,
Debug,
Trace,
}
#[derive(Parser)]
@@ -115,14 +155,14 @@ struct Opts {
// /// Do not attempt to auto-apply a dumped state temp file from a previously running instance of komorebi
// #[clap(long)]
// clean_state: bool,
// /// Level of log output verbosity
// #[clap(long, value_enum, default_value_t=LogLevel::Info)]
// log_level: LogLevel,
/// Level of log output verbosity
#[clap(long, value_enum, default_value_t=LogLevel::Info)]
log_level: LogLevel,
}
fn main() -> eyre::Result<()> {
let opts: Opts = Opts::parse();
setup()?;
let (_guard, _color_guard) = setup(opts.log_level)?;
let mut system = sysinfo::System::new();
system.refresh_processes(ProcessesToUpdate::All, true);
+2
View File
@@ -7,8 +7,10 @@ edition = "2024"
komorebi-client = { path = "../komorebi-client", default-features = false }
clap = { workspace = true }
chrono = { workspace = true }
color-eyre = { workspace = true }
dirs = { workspace = true }
fs-tail = "0.1"
lazy_static = { workspace = true }
pastey = { workspace = true }
reqwest = { version = "0.12", features = ["blocking"] }
+16
View File
@@ -1,7 +1,9 @@
#![warn(clippy::all)]
use chrono::Utc;
use clap::Parser;
use color_eyre::eyre;
use fs_tail::TailedFile;
use komorebi_client::Axis;
use komorebi_client::CycleDirection;
use komorebi_client::DefaultLayout;
@@ -12,7 +14,9 @@ use komorebi_client::SocketMessage;
use komorebi_client::send_message;
use komorebi_client::send_query;
use lazy_static::lazy_static;
use std::fs::File;
use std::fs::OpenOptions;
use std::io::BufRead;
use std::io::Write;
use std::path::PathBuf;
use std::sync::atomic::AtomicBool;
@@ -185,6 +189,8 @@ enum SubCommand {
/// Show the path to komorebi's data directory in $HOME/Library/Application Support
#[clap(alias = "datadir")]
DataDirectory,
/// Tail komorebi's process logs (cancel with Ctrl-C)
Log,
/// Show a JSON representation of the current window manager state
State,
/// Show a JSON representation of the current global state
@@ -384,6 +390,16 @@ fn main() -> eyre::Result<()> {
println!("{}", dir.display());
}
}
SubCommand::Log => {
let timestamp = Utc::now().format("%Y-%m-%d").to_string();
let color_log = DATA_DIR.join(format!("komorebi.log.{timestamp}"));
let file = TailedFile::new(File::open(color_log)?);
let locked = file.lock();
#[allow(clippy::significant_drop_in_scrutinee, clippy::lines_filter_map_ok)]
for line in locked.lines().flatten() {
println!("{line}");
}
}
SubCommand::Focus(arg) => {
send_message(&SocketMessage::FocusWindow(arg.operation_direction))?;
}