Skip to content

Commit

Permalink
print error logs in stdio mode
Browse files Browse the repository at this point in the history
  • Loading branch information
folkertdev committed Dec 4, 2024
1 parent a9d648b commit 3d255e4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ path = "bzip2recover.rs"

[dependencies]
libc.workspace = true
libbz2-rs-sys = { workspace = true, features = ["stdio", "std", "rust-allocator"] }
# we need `std` here, because we test the log output of the binaries, and those only
# get printed when std is enabled.
libbz2-rs-sys = { workspace = true, features = ["stdio", "c-allocator"] }

[dev-dependencies]
tempfile = "3.13.0"
Expand Down
1 change: 0 additions & 1 deletion bzip2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -965,7 +965,6 @@ fn outOfMemory(config: &Config) -> ! {
}

fn configError() -> ! {
panic!();
const MSG: &str = concat!(
"bzip2: I'm not configured correctly for this platform!\n",
"\tI require Int32, Int16 and Char to have sizes\n",
Expand Down
26 changes: 26 additions & 0 deletions libbz2-rs-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,43 @@ pub(crate) use libbz2_rs_sys_version;

// --- debug logs

#[cfg(all(not(feature = "std"), feature = "stdio"))]
pub(crate) struct StderrWritter;

#[cfg(all(not(feature = "std"), feature = "stdio"))]
impl core::fmt::Write for StderrWritter {
fn write_str(&mut self, s: &str) -> core::fmt::Result {
use core::ffi::c_void;
use libc::write;

unsafe {
write(2, s.as_ptr() as *const c_void, s.len());
}
Ok(())
}
}

macro_rules! debug_log {
($($arg:tt)*) => {
#[cfg(feature = "std")]
std::eprint!($($arg)*);
#[cfg(all(not(feature = "std"), feature = "stdio"))]
{
use core::fmt::Write;
write!($crate::StderrWritter, $($arg)*);
}
};
}

macro_rules! debug_logln {
($($arg:tt)*) => {
#[cfg(feature = "std")]
std::eprintln!($($arg)*);
#[cfg(all(not(feature = "std"), feature = "stdio"))]
{
use core::fmt::Write;
writeln!($crate::StderrWritter, $($arg)*);
}
};
}

Expand Down

0 comments on commit 3d255e4

Please sign in to comment.