Dump content to string for debugging. * Ascii chars are readable, the rest is printed as hex. * Probably ridiculously slow. * Use to_string() or to_string_view() for * interpreting the message as a string. */
| 692 | * interpreting the message as a string. |
| 693 | */ |
| 694 | std::string str(size_t max_size = 1000) const |
| 695 | { |
| 696 | // Partly mutuated from the same method in zmq::multipart_t |
| 697 | std::stringstream os; |
| 698 | |
| 699 | const unsigned char *msg_data = this->data<unsigned char>(); |
| 700 | size_t size_to_print = (std::min)(this->size(), max_size); |
| 701 | int is_ascii[2] = {0, 0}; |
| 702 | // Set is_ascii for the first character |
| 703 | if (size_to_print > 0) |
| 704 | is_ascii[0] = (*msg_data >= 32 && *msg_data < 127); |
| 705 | |
| 706 | os << "zmq::message_t [size " << std::dec << std::setw(3) |
| 707 | << std::setfill('0') << this->size() << "] ("; |
| 708 | while (size_to_print--) { |
| 709 | const unsigned char byte = *msg_data++; |
| 710 | |
| 711 | is_ascii[1] = (byte >= 32 && byte < 127); |
| 712 | if (is_ascii[1] != is_ascii[0]) |
| 713 | os << " "; // Separate text/non text |
| 714 | |
| 715 | if (is_ascii[1]) { |
| 716 | os << byte; |
| 717 | } else { |
| 718 | os << std::hex << std::uppercase << std::setw(2) << std::setfill('0') |
| 719 | << static_cast<short>(byte); |
| 720 | } |
| 721 | is_ascii[0] = is_ascii[1]; |
| 722 | } |
| 723 | // Elide the rest if the message is too large |
| 724 | if (max_size < this->size()) |
| 725 | os << "... too big to print)"; |
| 726 | else |
| 727 | os << ")"; |
| 728 | return os.str(); |
| 729 | } |
| 730 | |
| 731 | void swap(message_t &other) ZMQ_NOTHROW |
| 732 | { |