Commit a0ec14ed authored by Zeeshan Ali's avatar Zeeshan Ali

gst-inspect: Page stdout and stderr

Gets the pager commandline from PAGER env. If PAGER env is not set, just
use 'less -FR`.
parent 27546173
Pipeline #8860 failed with stages
in 7 minutes and 57 seconds
......@@ -7,7 +7,21 @@ extern crate glib_sys as glib_ffi;
extern crate nix;
mod piped_stdio;
fn main() {
let _piped = if cfg!(unix) {
match piped_stdio::PipedStdio::new() {
Ok(p) => Some(p),
Err(e) => {
eprintln!("Error redirecting output to pager: {}", e);
None
}
}
} else {
None
};
if let Err(e) = gst::init() {
// FIXME: Color output
println!("Error initializing GStreamer: {}", e);
......
use nix::unistd::dup2;
use std::ffi::OsString;
use std::fs::File;
use std::io::{stderr, stdin, stdout, Error, ErrorKind, Result, Write};
use std::os::unix::prelude::*;
use std::process::{Child, Command, Stdio};
const DEFAULT_PAGER: &'static str = "less -FR";
pub struct PipedStdio {
pager: Child,
}
impl PipedStdio {
pub fn new() -> Result<Self> {
let cmdline = std::env::var_os("PAGER").unwrap_or(OsString::from(DEFAULT_PAGER));
let mut cmd: Vec<&str> = cmdline.to_str().unwrap().split(" ").collect();
let mut pager = Command::new(OsString::from(cmd.remove(0)))
.stdin(Stdio::piped())
.args(cmd)
.spawn()?;
let child_stdin = pager.stdin.take().unwrap(); // Can't fail
// Redirect our stdout/stderr to the `pager` child process
dup2(child_stdin.as_raw_fd(), stdout().as_raw_fd())
.map_err(|e| Error::new(ErrorKind::Other, e))?;
dup2(child_stdin.as_raw_fd(), stderr().as_raw_fd())
.map_err(|e| Error::new(ErrorKind::Other, e))?;
Ok(PipedStdio { pager })
}
}
impl Drop for PipedStdio {
fn drop(&mut self) {
if let Err(e) = stdout().flush() {
eprintln!("{}", e);
};
if let Err(e) = stderr().flush() {
eprintln!("{}", e);
};
match File::open("/dev/null") {
Ok(null) => {
// "close" stdio streams now that our program is done
dup2(null.as_raw_fd(), stdin().as_raw_fd()).ok();
dup2(null.as_raw_fd(), stdout().as_raw_fd()).ok();
dup2(null.as_raw_fd(), stderr().as_raw_fd()).ok();
}
Err(e) => eprintln!("{}", e),
}
if let Err(e) = self.pager.wait() {
eprintln!("{}", e);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment