From d46119453c41747e760bf34aaceb5b6800fc82f2 Mon Sep 17 00:00:00 2001 From: Paul Aitken Date: Thu, 30 Jan 2020 13:13:40 +0000 Subject: [PATCH] Problem: zframe_print output is truncated Solution: new zmsg_print_n and zframe_print_n functions to print the specified amount of data without truncating. --- include/zframe.h | 8 +++ include/zmsg.h | 8 +++ src/zframe.c | 149 ++++++++++++++++++++++++++++++++++++++++++++--- src/zmsg.c | 10 +++- 4 files changed, 166 insertions(+), 9 deletions(-) diff --git a/include/zframe.h b/include/zframe.h index 47438a39d..de25468dc 100644 --- a/include/zframe.h +++ b/include/zframe.h @@ -115,9 +115,17 @@ CZMQ_EXPORT void // Send message to zsys log sink (may be stdout, or system facility as // configured by zsys_set_logstream). Prefix shows before frame, if not null. +// Long messages are truncated. CZMQ_EXPORT void zframe_print (zframe_t *self, const char *prefix); +// Send message to zsys log sink (may be stdout, or system facility as +// configured by zsys_set_logstream). Prefix shows before frame, if not null. +// Message length is specified; no truncation unless length is zero. +// Backwards compatible with zframe_print when length is zero. +CZMQ_EXPORT void + zframe_print_n (zframe_t *self, const char *prefix, size_t length); + // Probe the supplied object, and report if it looks like a zframe_t. CZMQ_EXPORT bool zframe_is (void *self); diff --git a/include/zmsg.h b/include/zmsg.h index 3d1d4d915..b6b88f4d9 100644 --- a/include/zmsg.h +++ b/include/zmsg.h @@ -191,9 +191,17 @@ CZMQ_EXPORT zmsg_t * // Send message to zsys log sink (may be stdout, or system facility as // configured by zsys_set_logstream). +// Long messages are truncated. CZMQ_EXPORT void zmsg_print (zmsg_t *self); +// Send message to zsys log sink (may be stdout, or system facility as +// configured by zsys_set_logstream). +// Message length is specified; no truncation unless length is zero. +// Backwards compatible with zframe_print when length is zero. +CZMQ_EXPORT void + zmsg_print_n (zmsg_t *self, size_t size); + // Return true if the two messages have the same number of frames and each // frame in the first message is identical to the corresponding frame in the // other message. As with zframe_eq, return false if either message is NULL. diff --git a/src/zframe.c b/src/zframe.c index d42276d19..c610aca24 100644 --- a/src/zframe.c +++ b/src/zframe.c @@ -502,8 +502,13 @@ zframe_reset (zframe_t *self, const void *data, size_t size) // Send message to zsys log sink (may be stdout, or system facility as // configured by zsys_set_logstream). Prefix shows before frame, if not null. +// Send the specified number of chars. +// If len is 0, then log 35 hex or 70 chars with ellipsis. +// If 0 < len < frame size, +// then log in blocks of 35 or 70 up to the specified length. +// void -zframe_print (zframe_t *self, const char *prefix) +zframe_print_n (zframe_t *self, const char *prefix, size_t length) { assert (self); assert (zframe_is (self)); @@ -515,18 +520,30 @@ zframe_print (zframe_t *self, const char *prefix) int is_bin = 0; uint char_nbr; for (char_nbr = 0; char_nbr < size; char_nbr++) - if (data [char_nbr] < 9 || data [char_nbr] > 127) + if (data [char_nbr] < 32 || data [char_nbr] > 127) is_bin = 1; char buffer [256] = ""; - snprintf (buffer, 30, "%s[%03d] ", prefix? prefix: "", (int) size); size_t max_size = is_bin? 35: 70; const char *ellipsis = ""; - if (size > max_size) { - size = max_size; - ellipsis = "..."; + + // backwards compatibility + if (!length) { + if (size > max_size) { + size = max_size; + ellipsis = "..."; + } + + length = size; } - for (char_nbr = 0; char_nbr < size; char_nbr++) { + if (length > size) + length = size; + + for (char_nbr = 0; char_nbr < length; char_nbr++) { + if (!(char_nbr % max_size)) { + if (char_nbr) zsys_debug (buffer); + snprintf (buffer, 30, "%s[%03d] ", prefix? prefix: "", (int) size); + } if (is_bin) sprintf (buffer + strlen (buffer), "%02X", (unsigned char) data [char_nbr]); else @@ -536,6 +553,12 @@ zframe_print (zframe_t *self, const char *prefix) zsys_debug (buffer); } +void +zframe_print (zframe_t *self, const char *prefix) +{ + zframe_print_n (self, prefix, 0); +} + // -------------------------------------------------------------------------- // Probe the supplied object, and report if it looks like a zframe_t. @@ -812,6 +835,118 @@ zframe_test (bool verbose) // so we can check if the destructor was invoked assert (streq (str, "world")); + + // zframe_print tests + + zsys_set_logstream(verbose ? stdout : NULL); + + // == No data == + frame = zframe_new ("", 0); + + // no prefix, backwards compatible + // - emits nothing but the timestamp + zframe_print (frame, ""); + zframe_print_n (frame, "", 0); + + // prefix, backwards compatible + // - emits nothing but the timestamp + zframe_print (frame, "Prefix"); + zframe_print_n (frame, "Prefix", 0); + + // len > data + // - emits nothing but the timestamp + zframe_print_n (frame, "", 15); + zframe_print_n (frame, "Prefix", 15); + + // max len + // - emits nothing but the timestamp + zframe_print_n (frame, "", -1); + zframe_print_n (frame, "Prefix", -1); + + zframe_destroy (&frame); + + + // == Short data == + frame = zframe_new ("Hello there!", 12); + + // no prefix, backwards compatible: ellipsis + // - "[012] Hello there!" + zframe_print (frame, ""); + zframe_print_n (frame, "", 0); + + // prefix, backwards compatible: ellipsis + // - "Prefix[012] Hello there!" + zframe_print (frame, "Prefix"); + zframe_print_n (frame, "Prefix", 0); + + // len < data + // - "[012] Hello" + // - "Prefix[012] Hello" + zframe_print_n (frame, "", 5); + zframe_print_n (frame, "Prefix", 5); + + // len > data + // - "[012] Hello there!" + // - "Prefix[012] Hello there!" + zframe_print_n (frame, "", 15); + zframe_print_n (frame, "Prefix", 15); + + // max len + // - "[012] Hello there!" + // - "Prefix[012] Hello there!" + zframe_print_n (frame, "", -1); + zframe_print_n (frame, "Prefix", -1); + + zframe_destroy (&frame); + + + // == Long data == + frame = zframe_new ("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin finibus ligula et aliquam tristique. Phasellus consequat, enim et blandit varius, sapien diam faucibus lorem, non ultricies lacus turpis sed lectus. Vivamus id elit urna. In sit amet lacinia mauris. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Integer ut cursus diam. Vestibulum semper vel leo eu finibus. Ut urna magna, commodo vel auctor sed, eleifend quis lacus. Aenean quis ipsum et velit auctor ultrices.", 519); + + // no prefix, backwards compatible: ellipsis + // - "[070] Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin finibus..." + zframe_print (frame, ""); + zframe_print_n (frame, "", 0); + + // prefix, backwards compatible: ellipsis + // - "Prefix[070] Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin finibus..." + zframe_print (frame, "Prefix"); + zframe_print_n (frame, "Prefix", 0); + + // len < data + // - "[519] Lorem" + // - "Prefix[519] Lorem" + zframe_print_n (frame, "", 5); + zframe_print_n (frame, "Prefix", 5); + + // small len + // - "[519] Lorem ipsum dolor sit amet" + // - "Prefix[519] Lorem ipsum dolor sit amet" + zframe_print_n (frame, "", 26); + zframe_print_n (frame, "Prefix", 26); + + // mid len + // - "[519] Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin finibus" + // "[519] ligula et aliquam tristique. Phasellus consequat, enim et blandit var" + // "[519] ius, sapie" + // - "Prefix[519] Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin finibus" + // "Prefix[519] ligula et aliquam tristique. Phasellus consequat, enim et blandit var" + // "Prefix[519] ius, sapie" + zframe_print_n (frame, "", 150); + zframe_print_n (frame, "Prefix", 150); + + // len > data + // - emits the whole paragraph + zframe_print_n (frame, "", 1500); + zframe_print_n (frame, "Prefix", 1500); + + // max len + // - emits the whole paragraph + zframe_print_n (frame, "", -1); + zframe_print_n (frame, "Prefix", -1); + + zframe_destroy (&frame); + #if defined (__WINDOWS__) zsys_shutdown(); #endif diff --git a/src/zmsg.c b/src/zmsg.c index 63f35c668..fc05ef4c4 100644 --- a/src/zmsg.c +++ b/src/zmsg.c @@ -737,7 +737,7 @@ zmsg_dup (zmsg_t *self) // configured by zsys_set_logstream). void -zmsg_print (zmsg_t *self) +zmsg_print_n (zmsg_t *self, size_t size) { assert (self); assert (zmsg_is (self)); @@ -748,11 +748,17 @@ zmsg_print (zmsg_t *self) } zframe_t *frame = zmsg_first (self); while (frame) { - zframe_print (frame, NULL); + zframe_print_n (frame, NULL, size); frame = zmsg_next (self); } } +void +zmsg_print (zmsg_t *self) +{ + zmsg_print_n (self, 0); +} + // -------------------------------------------------------------------------- // Return true if the two messages have the same number of frames and each