sharkdp / dbg-macro

A dbg(…) macro for C++
MIT License
2.97k stars 257 forks source link

can dbg print all elements in container #137

Open heheda123123 opened 1 month ago

heheda123123 commented 1 month ago

if i dbg an container, it only show some elements, such as

std::vector<uint32_t>(recv_body.begin(), recv_body.end()) = {245, 77, 208, 80, 0, 33, 234, 171, 123, 38, ... size:48} (std::vector<uint32_t>)

other elements only show ... Can I print all elements, I want to check every elements.

sharkdp commented 1 month ago

I think that is currently not possible. We could potentially add a helper function to do this(?). Similar to what we do for dbg::hex and friends: https://github.com/sharkdp/dbg-macro?tab=readme-ov-file#hexadecimal-octal-and-binary-format

heheda123123 commented 1 month ago

If I changed the function in dbg.h to below


template <typename Container>
inline typename std::enable_if<detail::is_container<const Container&>::value,
                               bool>::type
pretty_print(std::ostream& out, const Container& v) {
   constexpr int BYTES_PER_LINE = 16;
    std::stringstream ss;
    ss << '\n';

    for (size_t i = 0; i < v.size(); i++) {
        if (i % BYTES_PER_LINE == 0) {
            // Print address at the beginning of each line
            ss << std::setw(8) << std::setfill('0') << std::hex << i << ": ";
        }

        // Print hex value
        ss << std::setw(2) << std::setfill('0') << std::hex << static_cast<int>(v[i]) << " ";

        // Print ASCII representation at the end of each line or at the end of the vector
        if ((i + 1) % BYTES_PER_LINE == 0 || i == v.size() - 1) {
            // Pad with spaces if the last line is not full
            if (i == v.size() - 1) {
                for (size_t j = 0; j < BYTES_PER_LINE - (i % BYTES_PER_LINE) - 1; j++) {
                    ss << "   ";
                }
            }

            ss << " |";
            for (size_t j = i - (i % BYTES_PER_LINE); j <= i; j++) {
                if (v[j] >= 32 && v[j] <= 126) {
                    ss << static_cast<char>(v[j]);
                } else {
                    ss << '.';
                }
            }
            ss << "|\n";
        }
    }

    out << ss.str();
  return true;
}

the below output will be more clear

PS C:\Users\Administrator\temp\testbotan> xr
[src\main.cpp:14 (main)] test_data =
00000000: 11 65 6c 6c 6f 20 57 6f 72 6c 64 21 00 ff 7f 80  |.ello World!....|
00000010: 81 82 83 84 85 86 87 88                          |........|
 (std::vector<uint8_t>)
PS C:\Users\Administrator\temp\testbotan>

How to integrate this to dbg-macro(I am not familiar with c++ template) (the above code not support map)

heheda123123 commented 1 month ago

maybe use this output format to the default fotmat to container is better