Skip to content

Commit 37b2f5b

Browse files
committed
print error logs in stdio mode
1 parent 118224c commit 37b2f5b

File tree

5 files changed

+31
-6
lines changed

5 files changed

+31
-6
lines changed

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ path = "bzip2recover.rs"
4444

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

4951
[dev-dependencies]
5052
tempfile = "3.13.0"

bzip2.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -965,7 +965,6 @@ fn outOfMemory(config: &Config) -> ! {
965965
}
966966

967967
fn configError() -> ! {
968-
panic!();
969968
const MSG: &str = concat!(
970969
"bzip2: I'm not configured correctly for this platform!\n",
971970
"\tI require Int32, Int16 and Char to have sizes\n",

libbz2-rs-sys-cdylib/src/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ impl core::fmt::Write for StderrWritter {
1414
use core::ffi::c_void;
1515
use libc::write;
1616

17-
unsafe {
18-
write(2, s.as_ptr() as *const c_void, s.len());
19-
}
17+
unsafe { write(2, s.as_ptr() as *const c_void, s.len() as _) };
18+
2019
Ok(())
2120
}
2221
}

libbz2-rs-sys/src/bzlib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ mod stream {
307307
let bzalloc = strm.bzalloc.get_or_insert(default_bzalloc);
308308
let bzfree = strm.bzfree.get_or_insert(default_bzfree);
309309

310-
Some(Allocator::custom(*bzalloc, *bzfree, (*strm).opaque))
310+
Some(Allocator::custom(*bzalloc, *bzfree, strm.opaque))
311311
}
312312
}
313313
}

libbz2-rs-sys/src/lib.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,42 @@ pub(crate) use libbz2_rs_sys_version;
8383

8484
// --- debug logs
8585

86+
#[cfg(all(not(feature = "std"), feature = "stdio"))]
87+
pub(crate) struct StderrWritter;
88+
89+
#[cfg(all(not(feature = "std"), feature = "stdio"))]
90+
impl core::fmt::Write for StderrWritter {
91+
fn write_str(&mut self, s: &str) -> core::fmt::Result {
92+
use core::ffi::c_void;
93+
use libc::write;
94+
95+
unsafe { write(2, s.as_ptr() as *const c_void, s.len() as _) };
96+
97+
Ok(())
98+
}
99+
}
100+
86101
macro_rules! debug_log {
87102
($($arg:tt)*) => {
88103
#[cfg(feature = "std")]
89104
std::eprint!($($arg)*);
105+
#[cfg(all(not(feature = "std"), feature = "stdio"))]
106+
{
107+
use core::fmt::Write;
108+
let _ = write!($crate::StderrWritter, $($arg)*);
109+
}
90110
};
91111
}
92112

93113
macro_rules! debug_logln {
94114
($($arg:tt)*) => {
95115
#[cfg(feature = "std")]
96116
std::eprintln!($($arg)*);
117+
#[cfg(all(not(feature = "std"), feature = "stdio"))]
118+
{
119+
use core::fmt::Write;
120+
let _ = writeln!($crate::StderrWritter, $($arg)*);
121+
}
97122
};
98123
}
99124

0 commit comments

Comments
 (0)