Skip to content

Commit

Permalink
feat: more verbose logging for cmd (#633)
Browse files Browse the repository at this point in the history
Co-authored-by: jtroo
  • Loading branch information
rszyma authored Nov 17, 2023
1 parent 3602d4d commit 84a7931
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
35 changes: 19 additions & 16 deletions src/kanata/cmd.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt::Write;

use kanata_parser::cfg::parse_mod_prefix;
use kanata_parser::cfg::sexpr::*;
use kanata_parser::keys::*;
Expand All @@ -8,32 +10,33 @@ const LP: &str = "cmd-out:";
pub(super) fn run_cmd_in_thread(cmd_and_args: Vec<String>) -> std::thread::JoinHandle<()> {
std::thread::spawn(move || {
let mut args = cmd_and_args.iter();
let mut cmd = std::process::Command::new(
args.next()
.expect("parsing should have forbidden empty cmd"),
);
let mut printable_cmd = String::new();
let executable = args
.next()
.expect("parsing should have forbidden empty cmd");
write!(
printable_cmd,
"Program: {}, Arguments:",
executable.as_str()
)
.expect("write to string should succeed");
let mut cmd = std::process::Command::new(executable);
for arg in args {
cmd.arg(arg);
printable_cmd.push(' ');
printable_cmd.push_str(arg.as_str());
}
log::info!("Running cmd: {}", printable_cmd);
match cmd.output() {
Ok(output) => {
log::info!(
"Successfully ran cmd {}\nstdout:\n{}\nstderr:\n{}",
{
let mut printable_cmd = Vec::new();
printable_cmd.push(format!("{:?}", cmd.get_program()));

let printable_cmd = cmd.get_args().fold(printable_cmd, |mut cmd, arg| {
cmd.push(format!("{arg:?}"));
cmd
});
printable_cmd.join(" ")
},
"Successfully ran cmd: {}\nstdout:\n{}\nstderr:\n{}",
printable_cmd,
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
);
}
Err(e) => log::error!("Failed to execute cmd: {}", e),
Err(e) => log::error!("Failed to execute program {:?}: {}", cmd.get_program(), e),
};
})
}
Expand Down
2 changes: 1 addition & 1 deletion src/oskbd/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ impl Symlink {
fn clean_when_killed(symlink: Self) {
thread::spawn(|| {
let mut signals = Signals::new([SIGINT, SIGTERM]).expect("signals register");
for signal in &mut signals {
if let Some(signal) = (&mut signals).into_iter().next() {
match signal {
SIGINT | SIGTERM => {
drop(symlink);
Expand Down

0 comments on commit 84a7931

Please sign in to comment.