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 118224c commit 37b2f5b
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 6 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
5 changes: 2 additions & 3 deletions libbz2-rs-sys-cdylib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ impl core::fmt::Write for StderrWritter {
use core::ffi::c_void;
use libc::write;

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

Ok(())
}
}
Expand Down
2 changes: 1 addition & 1 deletion libbz2-rs-sys/src/bzlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ mod stream {
let bzalloc = strm.bzalloc.get_or_insert(default_bzalloc);
let bzfree = strm.bzfree.get_or_insert(default_bzfree);

Some(Allocator::custom(*bzalloc, *bzfree, (*strm).opaque))
Some(Allocator::custom(*bzalloc, *bzfree, strm.opaque))
}
}
}
Expand Down
25 changes: 25 additions & 0 deletions libbz2-rs-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,42 @@ 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() as _) };

Ok(())
}
}

macro_rules! debug_log {
($($arg:tt)*) => {
#[cfg(feature = "std")]
std::eprint!($($arg)*);
#[cfg(all(not(feature = "std"), feature = "stdio"))]
{
use core::fmt::Write;
let _ = 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;
let _ = writeln!($crate::StderrWritter, $($arg)*);
}
};
}

Expand Down

0 comments on commit 37b2f5b

Please sign in to comment.