Skip to content
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
18 changes: 18 additions & 0 deletions tests/message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,24 @@ TEST_CASE("message to string", "[message]")
#endif
}

TEST_CASE("message to debug string", "[message]")
{
const zmq::message_t a;
const zmq::message_t b("Foo", 3);
const zmq::message_t c("ascii\x01\x02\x03%%%\x04\x05\x06###", 17);
const zmq::message_t d("\x01\x02\x03|||", 6);
CHECK(a.str() == "zmq::message_t [size 000] ()");
CHECK(b.str() == "zmq::message_t [size 003] (Foo)");
CHECK(c.str() == "zmq::message_t [size 017] (ascii 010203 %%% 040506 ###)");
CHECK(d.str() == "zmq::message_t [size 006] (010203 |||)");
// With max_size
CHECK(a.str(100) == "zmq::message_t [size 000] ()");
CHECK(b.str(2) == "zmq::message_t [size 003] (Fo... too big to print)");
CHECK(c.str(10)
== "zmq::message_t [size 017] (ascii 010203 %%... too big to print)");
CHECK(d.str(2) == "zmq::message_t [size 006] (0102... too big to print)");
}

#if defined(ZMQ_BUILD_DRAFT_API) && ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 0)
TEST_CASE("message routing id persists", "[message]")
{
Expand Down
47 changes: 24 additions & 23 deletions zmq.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -691,39 +691,40 @@ class message_t
* Use to_string() or to_string_view() for
* interpreting the message as a string.
*/
std::string str() const
std::string str(size_t max_size = 1000) const
{
// Partly mutuated from the same method in zmq::multipart_t
std::stringstream os;

const unsigned char *msg_data = this->data<unsigned char>();
unsigned char byte;
size_t size = this->size();
size_t size_to_print = (std::min)(this->size(), max_size);
int is_ascii[2] = {0, 0};
// Set is_ascii for the first character
if (size_to_print > 0)
is_ascii[0] = (*msg_data >= 32 && *msg_data < 127);

os << "zmq::message_t [size " << std::dec << std::setw(3)
<< std::setfill('0') << size << "] (";
// Totally arbitrary
if (size >= 1000) {
os << "... too big to print)";
} else {
while (size--) {
byte = *msg_data++;

is_ascii[1] = (byte >= 32 && byte < 127);
if (is_ascii[1] != is_ascii[0])
os << " "; // Separate text/non text

if (is_ascii[1]) {
os << byte;
} else {
os << std::hex << std::uppercase << std::setw(2)
<< std::setfill('0') << static_cast<short>(byte);
}
is_ascii[0] = is_ascii[1];
<< std::setfill('0') << this->size() << "] (";
while (size_to_print--) {
const unsigned char byte = *msg_data++;

is_ascii[1] = (byte >= 32 && byte < 127);
if (is_ascii[1] != is_ascii[0])
os << " "; // Separate text/non text

if (is_ascii[1]) {
os << byte;
} else {
os << std::hex << std::uppercase << std::setw(2) << std::setfill('0')
<< static_cast<short>(byte);
}
os << ")";
is_ascii[0] = is_ascii[1];
}
// Elide the rest if the message is too large
if (max_size < this->size())
os << "... too big to print)";
else
os << ")";
return os.str();
}

Expand Down