#[cfg(unix)]
pub(crate) fn pre_exec_log(msg: &[u8]) {
unsafe {
let _ = libc::write(
libc::STDERR_FILENO,
msg.as_ptr() as *const libc::c_void,
msg.len(),
);
}
}
#[cfg(not(unix))]
pub(crate) fn pre_exec_log(msg: &[u8]) {
use std::io::Write;
let _ = std::io::stderr().write_all(msg);
}
#[cfg(unix)]
pub(crate) fn pre_exec_log_errno(errno: i32) {
let mut buf = [0u8; 32];
let mut idx = buf.len();
let negative = errno < 0;
let mut n = if negative {
(-errno) as u32
} else {
errno as u32
};
if n == 0 {
idx -= 1;
buf[idx] = b'0';
} else {
while n > 0 {
let digit = (n % 10) as u8;
n /= 10;
idx -= 1;
buf[idx] = b'0' + digit;
}
}
if negative {
idx -= 1;
buf[idx] = b'-';
}
const PREFIX: &[u8] = b"errno=";
unsafe {
let _ = libc::write(
libc::STDERR_FILENO,
PREFIX.as_ptr() as *const libc::c_void,
PREFIX.len(),
);
let _ = libc::write(
libc::STDERR_FILENO,
buf[idx..].as_ptr() as *const libc::c_void,
buf.len() - idx,
);
let nl = b"\n";
let _ = libc::write(
libc::STDERR_FILENO,
nl.as_ptr() as *const libc::c_void,
nl.len(),
);
}
}
#[cfg(not(unix))]
pub(crate) fn pre_exec_log_errno(errno: i32) {
use std::io::Write;
let mut stderr = std::io::stderr();
let _ = write!(stderr, "errno={errno}\n");
}