Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add log macro #62

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 11 additions & 20 deletions libbz2-rs-sys/src/blocksort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{
assert_h,
bzlib::{Arr2, EState, BZ_N_OVERSHOOT, BZ_N_QSORT, BZ_N_RADIX, FTAB_LEN},
};
use crate::{debug_log, debug_logln};

/// Fallback O(N log(N)^2) sorting algorithm, for repetitive blocks
#[inline]
Expand Down Expand Up @@ -238,8 +239,7 @@ fn fallbackSort(
initial fmap and initial BH bits.
--*/
if verb >= 4 {
#[cfg(feature = "std")]
std::eprintln!(" bucket sorting ...");
debug_logln!(" bucket sorting ...");
}

{
Expand Down Expand Up @@ -285,8 +285,7 @@ fn fallbackSort(
H = 1;
loop {
if verb >= 4 {
#[cfg(feature = "std")]
std::eprint!(" depth {:>6} has ", H);
debug_log!(" depth {:>6} has ", H);
}
j = 0;
i = 0;
Expand Down Expand Up @@ -354,8 +353,7 @@ fn fallbackSort(
}
}
if verb >= 4 {
#[cfg(feature = "std")]
std::eprintln!("{:>6} unresolved strings", nNotDone);
debug_logln!("{:>6} unresolved strings", nNotDone);
}
H *= 2;
if H > nblock || nNotDone == 0 {
Expand All @@ -364,8 +362,7 @@ fn fallbackSort(
}

if verb >= 4 {
#[cfg(feature = "std")]
std::eprintln!(" reconstructing block ...");
debug_logln!(" reconstructing block ...");
}

{
Expand Down Expand Up @@ -963,8 +960,7 @@ fn mainSort(
let mut numQSorted: i32;
let mut s: u16;
if verb >= 4 as c_int {
#[cfg(feature = "std")]
std::eprintln!(" main sort initialise ...");
debug_logln!(" main sort initialise ...");
}

/*-- set up the 2-byte frequency table --*/
Expand Down Expand Up @@ -999,8 +995,7 @@ fn mainSort(
}

if verb >= 4 as c_int {
#[cfg(feature = "std")]
std::eprintln!(" bucket sorting ...");
debug_logln!(" bucket sorting ...");
}

/*-- Complete the initial radix sort --*/
Expand Down Expand Up @@ -1120,8 +1115,7 @@ fn mainSort(

if hi > lo {
if verb >= 4 as c_int {
#[cfg(feature = "std")]
std::eprintln!(
debug_logln!(
" qsort [{:#x}, {:#x}] done {} this {}",
ss,
j,
Expand Down Expand Up @@ -1265,8 +1259,7 @@ fn mainSort(
}
}
if verb >= 4 as c_int {
#[cfg(feature = "std")]
std::eprintln!(
debug_logln!(
" {} pointers, {} sorted, {} scanned",
nblock,
numQSorted,
Expand Down Expand Up @@ -1340,8 +1333,7 @@ fn BZ2_blockSortHelp(
);

if verbosity >= 3 {
#[cfg(feature = "std")]
std::eprintln!(
debug_logln!(
" {} work, {} block, ratio {:5.2}",
budgetInit - budget,
nblock,
Expand All @@ -1351,8 +1343,7 @@ fn BZ2_blockSortHelp(

if budget < 0 {
if verbosity >= 2 as c_int {
#[cfg(feature = "std")]
std::eprintln!(" too repetitive; using fallback sorting algorithm");
debug_logln!(" too repetitive; using fallback sorting algorithm");
}

fallbackSort(ptr, arr2, ftab, nblock as i32, verbosity);
Expand Down
10 changes: 4 additions & 6 deletions libbz2-rs-sys/src/bzlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use core::{mem, ptr};
use crate::allocator::Allocator;
use crate::compress::compress_block;
use crate::crctable::BZ2_CRC32TABLE;
use crate::debug_log;
use crate::decompress::{self, decompress};
#[cfg(feature = "stdio")]
use crate::libbz2_rs_sys_version;
Expand Down Expand Up @@ -1732,16 +1733,14 @@ pub(crate) fn BZ2_bzDecompressHelp(strm: &mut BzStream<DState>) -> ReturnCode {
if s.nblock_used == s.save_nblock + 1 && s.state_out_len == 0 {
s.calculatedBlockCRC = !s.calculatedBlockCRC;
if s.verbosity >= 3 {
#[cfg(feature = "std")]
std::eprint!(
debug_log!(
" {{{:#08x}, {:#08x}}}",
s.storedBlockCRC,
s.calculatedBlockCRC,
);
}
if s.verbosity >= 2 {
#[cfg(feature = "std")]
std::eprint!("]");
debug_log!("]");
}
#[cfg(not(feature = "__internal-fuzz-disable-checksum"))]
if s.calculatedBlockCRC != s.storedBlockCRC {
Expand All @@ -1760,8 +1759,7 @@ pub(crate) fn BZ2_bzDecompressHelp(strm: &mut BzStream<DState>) -> ReturnCode {
_ => match decompress(strm, s, &allocator) {
ReturnCode::BZ_STREAM_END => {
if s.verbosity >= 3 {
#[cfg(feature = "std")]
std::eprint!(
debug_log!(
"\n combined CRCs: stored = {:#08x}, computed = {:#08x}",
s.storedCombinedCRC,
s.calculatedCombinedCRC,
Expand Down
35 changes: 12 additions & 23 deletions libbz2-rs-sys/src/compress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use crate::blocksort::block_sort;
use crate::bzlib::{EState, BZ_MAX_SELECTORS, BZ_N_GROUPS, BZ_N_ITERS, BZ_RUNA, BZ_RUNB};
use crate::{assert_h, huffman};
use crate::{assert_h, debug_log, debug_logln, huffman};

pub(crate) struct EWriter {
pub num_z: u32,
Expand Down Expand Up @@ -233,8 +233,7 @@ fn send_mtf_values(s: &mut EState) {
let mtfv = s.arr1.mtfv();

if s.verbosity >= 3 {
#[cfg(feature = "std")]
std::eprintln!(
debug_logln!(
" {} in block, {} after MTF & 1-2 coding, {}+2 syms in use",
s.nblock,
s.nMTF,
Expand Down Expand Up @@ -285,8 +284,7 @@ fn send_mtf_values(s: &mut EState) {
}

if s.verbosity >= 3 {
#[cfg(feature = "std")]
std::eprintln!(
debug_logln!(
" initial group {}, [{} .. {}], has {} syms ({:4.1}%)",
nPart,
gs,
Expand Down Expand Up @@ -447,18 +445,15 @@ fn send_mtf_values(s: &mut EState) {
}

if s.verbosity >= 3 {
#[cfg(feature = "std")]
std::eprint!(
debug_log!(
" pass {}: size is {}, grp uses are ",
iter + 1,
totc / 8,
);
for t in 0..nGroups {
#[cfg(feature = "std")]
std::eprint!("{} ", fave[t],);
debug_log!("{} ", fave[t],);
}
#[cfg(feature = "std")]
std::eprintln!();
debug_logln!();
}

/*--
Expand Down Expand Up @@ -538,8 +533,7 @@ fn send_mtf_values(s: &mut EState) {
}
}
if s.verbosity >= 3 {
#[cfg(feature = "std")]
std::eprint!(" bytes: mapping {}, ", writer.num_z as i32 - nBytes,);
debug_log!(" bytes: mapping {}, ", writer.num_z as i32 - nBytes,);
}
}

Expand All @@ -555,8 +549,7 @@ fn send_mtf_values(s: &mut EState) {
writer.write(1, 0);
}
if s.verbosity >= 3 {
#[cfg(feature = "std")]
std::eprint!("selectors {}, ", writer.num_z as i32 - nBytes);
debug_log!("selectors {}, ", writer.num_z as i32 - nBytes);
}

/*--- Now the coding tables. ---*/
Expand All @@ -578,8 +571,7 @@ fn send_mtf_values(s: &mut EState) {
}
}
if s.verbosity >= 3 {
#[cfg(feature = "std")]
std::eprint!("code lengths {}, ", writer.num_z as i32 - nBytes);
debug_log!("code lengths {}, ", writer.num_z as i32 - nBytes);
}

/*--- And finally, the block data proper ---*/
Expand Down Expand Up @@ -640,8 +632,7 @@ fn send_mtf_values(s: &mut EState) {
assert_h!(selCtr == nSelectors, 3007);

if s.verbosity >= 3 {
#[cfg(feature = "std")]
std::eprintln!("codes {}", writer.num_z as i32 - nBytes);
debug_logln!("codes {}", writer.num_z as i32 - nBytes);
}
}

Expand All @@ -655,8 +646,7 @@ pub(crate) fn compress_block(s: &mut EState, is_last_block: bool) {
}

if s.verbosity >= 2 {
#[cfg(feature = "std")]
std::eprintln!(
debug_logln!(
" block {}: crc = 0x{:08x}, combined CRC = 0x{:08x}, size = {}",
s.blockNo,
s.blockCRC,
Expand Down Expand Up @@ -727,8 +717,7 @@ pub(crate) fn compress_block(s: &mut EState, is_last_block: bool) {
writer.write_u32(s.combinedCRC);

if s.verbosity >= 2 {
#[cfg(feature = "std")]
std::eprint!(" final combined CRC = 0x{:08x}\n ", s.combinedCRC);
debug_log!(" final combined CRC = 0x{:08x}\n ", s.combinedCRC);
}

writer.finish();
Expand Down
8 changes: 3 additions & 5 deletions libbz2-rs-sys/src/decompress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use core::ffi::{c_int, c_uint};

use crate::allocator::Allocator;
use crate::bzlib::{index_into_f, BzStream, DSlice, DState, DecompressMode, ReturnCode};
use crate::huffman;
use crate::randtable::BZ2_RNUMS;
use crate::{debug_log, huffman};

/*-- Constants for the fast MTF decoder. --*/

Expand Down Expand Up @@ -556,8 +556,7 @@ pub(crate) fn decompress(

s.currBlockNo += 1;
if s.verbosity >= 2 {
#[cfg(feature = "std")]
std::eprint!("\n [{}: huff+mtf ", s.currBlockNo);
debug_log!("\n [{}: huff+mtf ", s.currBlockNo);
}
s.storedBlockCRC = 0_u32;
current_block = BZ_X_BCRC_1;
Expand Down Expand Up @@ -1054,8 +1053,7 @@ pub(crate) fn decompress(
s.calculatedBlockCRC = 0xffffffffu32;
s.state = State::BZ_X_OUTPUT;
if s.verbosity >= 2 {
#[cfg(feature = "std")]
std::eprint!("rt+rld");
debug_log!("rt+rld");
}
match s.smallDecompress {
DecompressMode::Small => {
Expand Down
19 changes: 19 additions & 0 deletions libbz2-rs-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,25 @@ macro_rules! libbz2_rs_sys_version {

pub(crate) use libbz2_rs_sys_version;

// --- debug logs

macro_rules! debug_log {
($($arg:tt)*) => {
#[cfg(feature = "std")]
std::eprint!($($arg)*);
};
}

macro_rules! debug_logln {
($($arg:tt)*) => {
#[cfg(feature = "std")]
std::eprintln!($($arg)*);
};
}

pub(crate) use debug_log;
pub(crate) use debug_logln;

// --- assert failure logic

macro_rules! assert_h {
Expand Down
Loading