Skip to content

Commit bd7837f

Browse files
committed
fix stderr logging #23
e.g: cmd_info!("Memory usage (free -m):"); run_cmd!(free -m >&2)?; to logs: INFO - memory usage (free -m): INFO - total used free shared buff/cache available INFO - Mem: 7709 2003 1070 656 4635 4675 INFO - Swap: 8191 11 8180
1 parent 1cfb04a commit bd7837f

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

src/child.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ use std::process::{Child, ExitStatus};
66
use std::thread::JoinHandle;
77
use CmdChild::{ProcChild, SyncChild, ThreadChild};
88

9+
#[derive(Debug)]
910
pub enum CmdChild {
1011
ProcChild {
1112
child: Child,
1213
cmd: String,
14+
stderr: Option<PipeReader>,
1315
},
1416
ThreadChild {
1517
child: JoinHandle<CmdResult>,
@@ -35,9 +37,13 @@ impl CmdChild {
3537
Ok(())
3638
};
3739
match self {
38-
ProcChild { mut child, cmd } => {
40+
ProcChild {
41+
mut child,
42+
stderr,
43+
cmd,
44+
} => {
45+
Self::log_stderr_output(stderr);
3946
let status = child.wait()?;
40-
Self::log_stderr_output(child.stderr);
4147
if !status.success() && (is_last || pipefail) {
4248
return Err(Self::status_to_io_error(
4349
status,
@@ -76,9 +82,9 @@ impl CmdChild {
7682

7783
pub fn wait_with_output(self) -> Result<Vec<u8>> {
7884
match self {
79-
ProcChild { child, cmd } => {
85+
ProcChild { child, cmd, stderr } => {
86+
Self::log_stderr_output(stderr);
8087
let output = child.wait_with_output()?;
81-
Self::log_stderr_output(Some(&output.stderr[..]));
8288
if !output.status.success() {
8389
return Err(Self::status_to_io_error(
8490
output.status,

src/io.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::fs::File;
33
use std::io::{Read, Result, Write};
44
use std::process::Stdio;
55

6+
#[derive(Debug)]
67
pub enum CmdIn {
78
CmdNull,
89
CmdFile(File),
@@ -29,6 +30,7 @@ impl From<CmdIn> for Stdio {
2930
}
3031
}
3132

33+
#[derive(Debug)]
3234
pub enum CmdOut {
3335
CmdNull,
3436
CmdFile(File),

src/process.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,6 @@ impl Cmd {
431431
let cmds: Vec<String> = self.args.to_vec();
432432
let mut cmd = Command::new(&cmds[0]);
433433
cmd.args(&cmds[1..]);
434-
cmd.stderr(Stdio::piped());
435434
for (k, v) in self.vars.iter() {
436435
cmd.env(k, v);
437436
}
@@ -524,6 +523,7 @@ impl Cmd {
524523
let child = cmd.spawn()?;
525524
Ok(CmdChild::ProcChild {
526525
cmd: full_cmd,
526+
stderr: self.stderr_logging,
527527
child,
528528
})
529529
}
@@ -571,12 +571,10 @@ impl Cmd {
571571
pipe_in: &mut Option<PipeReader>,
572572
pipe_out: Option<PipeWriter>,
573573
) -> CmdResult {
574-
if self.in_cmd_map {
575-
// set up error pipe
576-
let (pipe_reader, pipe_writer) = os_pipe::pipe()?;
577-
self.stderr_redirect = Some(CmdOut::CmdPipe(pipe_writer));
578-
self.stderr_logging = Some(pipe_reader);
579-
}
574+
// set up error pipe
575+
let (pipe_reader, pipe_writer) = os_pipe::pipe()?;
576+
self.stderr_redirect = Some(CmdOut::CmdPipe(pipe_writer));
577+
self.stderr_logging = Some(pipe_reader);
580578

581579
if let Some(pipe) = pipe_in.take() {
582580
self.stdin_redirect = Some(CmdIn::CmdPipe(pipe));
@@ -598,14 +596,14 @@ impl Cmd {
598596
if let Some(ref redirect) = self.stderr_redirect {
599597
self.stdout_redirect = Some(redirect.try_clone()?);
600598
} else {
601-
self.stdout_redirect = Some(CmdOut::CmdPipe(os_pipe::dup_stdout()?));
599+
self.stdout_redirect = Some(CmdOut::CmdPipe(os_pipe::dup_stderr()?));
602600
}
603601
}
604602
Redirect::StderrToStdout => {
605603
if let Some(ref redirect) = self.stdout_redirect {
606604
self.stderr_redirect = Some(redirect.try_clone()?);
607605
} else {
608-
self.stderr_redirect = Some(CmdOut::CmdPipe(os_pipe::dup_stderr()?));
606+
self.stderr_redirect = Some(CmdOut::CmdPipe(os_pipe::dup_stdout()?));
609607
}
610608
}
611609
Redirect::StdoutToFile(path, append) => {

0 commit comments

Comments
 (0)