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

can dbg print all elements in container #137

Open
heheda123123 opened this issue Aug 15, 2024 · 3 comments
Open

can dbg print all elements in container #137

heheda123123 opened this issue Aug 15, 2024 · 3 comments

Comments

@heheda123123
Copy link

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
Copy link
Owner

sharkdp commented Aug 15, 2024

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
Copy link
Author

heheda123123 commented Aug 19, 2024

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants