use std::path::Path;
use log::error;
use super::sysexits::*;
use super::system_config::DiagnosticConfig;
pub fn apply_diagnostics(
root: &Path,
config: &DiagnosticConfig,
) -> Result<(), Sysexit> {
if let Some(ref stderr_path) = config.stderr {
redirect_stderr(&root.join(stderr_path))?;
}
Ok(())
}
fn redirect_stderr(stderr_path: &Path) -> Result<(), Sysexit> {
if let Err(e) = nix::unistd::close(2) {
error!("failed to redirect stderr: close(stderr): {:?}", e);
return Err(EX_IOERR);
}
match nix::fcntl::open(
stderr_path,
nix::fcntl::OFlag::O_APPEND
| nix::fcntl::OFlag::O_WRONLY
| nix::fcntl::OFlag::O_CREAT,
nix::sys::stat::Mode::from_bits(0o640).unwrap(),
) {
Err(e) => {
error!(
"failed to redirect stderr: open({}): {:?}",
stderr_path.display(),
e,
);
Err(EX_CANTCREAT)
},
Ok(2) => Ok(()),
Ok(fd) => {
error!("failed to redirect stderr: got fd {} instead of 2", fd);
Err(EX_OSERR)
},
}
}