From e011feaf7fa3ae6a4f9fd75433801d5a5883f394 Mon Sep 17 00:00:00 2001 From: Folkert de Vries Date: Wed, 4 Dec 2024 12:05:17 +0100 Subject: [PATCH] add log macro --- libbz2-rs-sys/src/blocksort.rs | 31 +++++++++++------------------ libbz2-rs-sys/src/bzlib.rs | 10 ++++------ libbz2-rs-sys/src/compress.rs | 35 +++++++++++---------------------- libbz2-rs-sys/src/decompress.rs | 8 +++----- libbz2-rs-sys/src/lib.rs | 19 ++++++++++++++++++ 5 files changed, 49 insertions(+), 54 deletions(-) diff --git a/libbz2-rs-sys/src/blocksort.rs b/libbz2-rs-sys/src/blocksort.rs index b51848a30..450e4a01a 100644 --- a/libbz2-rs-sys/src/blocksort.rs +++ b/libbz2-rs-sys/src/blocksort.rs @@ -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] @@ -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 ..."); } { @@ -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; @@ -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 { @@ -364,8 +362,7 @@ fn fallbackSort( } if verb >= 4 { - #[cfg(feature = "std")] - std::eprintln!(" reconstructing block ..."); + debug_logln!(" reconstructing block ..."); } { @@ -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 --*/ @@ -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 --*/ @@ -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, @@ -1265,8 +1259,7 @@ fn mainSort( } } if verb >= 4 as c_int { - #[cfg(feature = "std")] - std::eprintln!( + debug_logln!( " {} pointers, {} sorted, {} scanned", nblock, numQSorted, @@ -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, @@ -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); diff --git a/libbz2-rs-sys/src/bzlib.rs b/libbz2-rs-sys/src/bzlib.rs index 7c6cca0d8..7e436d3d1 100644 --- a/libbz2-rs-sys/src/bzlib.rs +++ b/libbz2-rs-sys/src/bzlib.rs @@ -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; @@ -1732,16 +1733,14 @@ pub(crate) fn BZ2_bzDecompressHelp(strm: &mut BzStream) -> 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 { @@ -1760,8 +1759,7 @@ pub(crate) fn BZ2_bzDecompressHelp(strm: &mut BzStream) -> 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, diff --git a/libbz2-rs-sys/src/compress.rs b/libbz2-rs-sys/src/compress.rs index 56721dc33..700d1966d 100644 --- a/libbz2-rs-sys/src/compress.rs +++ b/libbz2-rs-sys/src/compress.rs @@ -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, @@ -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, @@ -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, @@ -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!(); } /*-- @@ -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,); } } @@ -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. ---*/ @@ -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 ---*/ @@ -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); } } @@ -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, @@ -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(); diff --git a/libbz2-rs-sys/src/decompress.rs b/libbz2-rs-sys/src/decompress.rs index da1aa2be9..3ee795be9 100644 --- a/libbz2-rs-sys/src/decompress.rs +++ b/libbz2-rs-sys/src/decompress.rs @@ -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. --*/ @@ -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; @@ -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 => { diff --git a/libbz2-rs-sys/src/lib.rs b/libbz2-rs-sys/src/lib.rs index e5d4287b8..11fac3228 100644 --- a/libbz2-rs-sys/src/lib.rs +++ b/libbz2-rs-sys/src/lib.rs @@ -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 {