Skip to content

Commit

Permalink
add get_stderr() and get_stderr_all() to SBProcess
Browse files Browse the repository at this point in the history
  • Loading branch information
roccoblues authored and waywardmonkeys committed Apr 10, 2024
1 parent 26774b3 commit 17fc55c
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,36 @@ impl SBProcess {
String::from_utf8(dst).ok()
}

/// Reads data from the current process's stderr stream until the end of the stream.
pub fn get_stderr_all(&self) -> Option<String> {
let dst_len = 0x1000;
let mut output = "".to_string();
let mut dst: Vec<u8> = Vec::with_capacity(dst_len);
loop {
let out_len =
unsafe { sys::SBProcessGetSTDERR(self.raw, dst.as_mut_ptr() as *mut i8, dst_len) };
if out_len == 0 {
break;
}
unsafe { dst.set_len(out_len) };
output += std::str::from_utf8(&dst).ok()?;
}

Some(output)
}

/// Reads data from the current process's stderr stream.
pub fn get_stderr(&self) -> Option<String> {
let dst_len = 0x1000;
let mut dst: Vec<u8> = Vec::with_capacity(dst_len);

let out_len =
unsafe { sys::SBProcessGetSTDERR(self.raw, dst.as_mut_ptr() as *mut i8, dst_len) };

unsafe { dst.set_len(out_len) };
String::from_utf8(dst).ok()
}

#[allow(missing_docs)]
pub fn broadcaster(&self) -> SBBroadcaster {
SBBroadcaster::wrap(unsafe { sys::SBProcessGetBroadcaster(self.raw) })
Expand Down

0 comments on commit 17fc55c

Please sign in to comment.