Skip to content

Commit

Permalink
Problem: zframe_print output is truncated
Browse files Browse the repository at this point in the history
Solution: new zmsg_print_n and zframe_print_n functions
to print the specified amount of data without truncating.
  • Loading branch information
pjaitken committed Jan 30, 2020
1 parent 39b2a79 commit e99df53
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 9 deletions.
11 changes: 11 additions & 0 deletions include/zframe.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -126,6 +134,9 @@ CZMQ_EXPORT bool
CZMQ_EXPORT void
zframe_test (bool verbose);

CZMQ_EXPORT void
zframe_print_test (bool verbose);

#ifdef CZMQ_BUILD_DRAFT_API
// Destroy an item
typedef void (zframe_destructor_fn) (
Expand Down
8 changes: 8 additions & 0 deletions include/zmsg.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions src/czmq_selftest.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ all_tests [] = {
{ "zdir_patch", zdir_patch_test, true, true, NULL },
{ "zfile", zfile_test, true, true, NULL },
{ "zframe", zframe_test, true, true, NULL },
{ "zprint", zframe_print_test, true, true, NULL },
{ "zhash", zhash_test, true, true, NULL },
{ "zhashx", zhashx_test, true, true, NULL },
{ "ziflist", ziflist_test, true, true, NULL },
Expand Down
158 changes: 151 additions & 7 deletions src/zframe.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -819,3 +842,124 @@ zframe_test (bool verbose)
// @end
printf ("OK\n");
}


void
zframe_print_test (bool verbose)
{
printf (" * zprint: ");

zframe_t* frame;

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);


printf ("OK\n");
}
10 changes: 8 additions & 2 deletions src/zmsg.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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
Expand Down

0 comments on commit e99df53

Please sign in to comment.