1
0
Fork 0
mirror of https://github.com/Zedfrigg/ironbar.git synced 2025-07-01 02:31:04 +02:00

chore(script): add debug logging

This commit is contained in:
Jake Stanger 2023-04-13 12:47:26 +01:00
parent 2815cef440
commit 1e1d65ae49
No known key found for this signature in database
GPG key ID: C51FC8F9CB0BEA61

View file

@ -9,7 +9,7 @@ use tokio::process::Command;
use tokio::sync::mpsc;
use tokio::time::sleep;
use tokio::{select, spawn};
use tracing::{error, warn};
use tracing::{debug, error, trace, warn};
#[derive(Debug, Deserialize, Clone)]
#[serde(untagged)]
@ -219,27 +219,38 @@ impl Script {
args_list.extend(args.iter().map(|s| s.as_str()));
}
debug!("Running sh with args: {args_list:?}");
let output = Command::new("sh")
.args(&args_list)
.output()
.await
.wrap_err("Failed to get script output")?;
trace!("Script output with args: {output:?}");
if output.status.success() {
let stdout = String::from_utf8(output.stdout)
.map(|output| output.trim().to_string())
.wrap_err("Script stdout not valid UTF-8")?;
debug!("sending stdout: '{stdout}'");
Ok((OutputStream::Stdout(stdout), true))
} else {
let stderr = String::from_utf8(output.stderr)
.map(|output| output.trim().to_string())
.wrap_err("Script stderr not valid UTF-8")?;
debug!("sending stderr: '{stderr}'");
Ok((OutputStream::Stderr(stderr), false))
}
}
/// Spawns a long-running process.
/// Returns a `mpsc::Receiver` that sends a message
/// every time a new line is written to `stdout` or `stderr`.
pub async fn spawn(&self) -> Result<mpsc::Receiver<OutputStream>> {
let mut handle = Command::new("sh")
.args(["-c", &self.cmd])
@ -248,6 +259,9 @@ impl Script {
.stdin(Stdio::null())
.spawn()?;
debug!("Spawned a long-running process for '{}'", self.cmd);
trace!("Handle: {:?}", handle);
let mut stdout_lines = BufReader::new(
handle
.stdout
@ -271,9 +285,11 @@ impl Script {
select! {
_ = handle.wait() => break,
Ok(Some(line)) = stdout_lines.next_line() => {
debug!("sending stdout line: '{line}'");
send_async!(tx, OutputStream::Stdout(line));
}
Ok(Some(line)) = stderr_lines.next_line() => {
debug!("sending stderr line: '{line}'");
send_async!(tx, OutputStream::Stderr(line));
}
}