Bug 23837 - dbus-monitor: print 'ay' with hexdump-style formatting.
Summary: dbus-monitor: print 'ay' with hexdump-style formatting.
Status: RESOLVED FIXED
Alias: None
Product: dbus
Classification: Unclassified
Component: core (show other bugs)
Version: unspecified
Hardware: Other All
: medium enhancement
Assignee: Havoc Pennington
QA Contact: John (J5) Palmieri
URL: http://git.collabora.co.uk/?p=user/wj...
Whiteboard:
Keywords: patch
Depends on:
Blocks:
 
Reported: 2009-09-10 04:48 UTC by Will Thompson
Modified: 2009-11-05 03:18 UTC (History)
0 users

See Also:
i915 platform:
i915 features:


Attachments
Make array-printing case more readable (1.16 KB, patch)
2009-09-10 04:49 UTC, Will Thompson
Details | Splinter Review
Print byte arrays as nicely-formatted hex. (2.09 KB, patch)
2009-09-10 04:49 UTC, Will Thompson
Details | Splinter Review
Print ASCIIfied version of byte arrays where possible (2.26 KB, patch)
2009-09-10 04:49 UTC, Will Thompson
Details | Splinter Review
Make array-printing code easier to follow (1.39 KB, patch)
2009-11-01 05:51 UTC, Will Thompson
Details | Splinter Review
Print byte arrays as nicely-formatted hex. (2.09 KB, patch)
2009-11-01 05:51 UTC, Will Thompson
Details | Splinter Review
Print all-printable-ASCII byte arrays as strings (3.11 KB, patch)
2009-11-01 05:51 UTC, Will Thompson
Details | Splinter Review
Print byte arrays as nicely-formatted hex. (2.10 KB, patch)
2009-11-04 09:14 UTC, Will Thompson
Details | Splinter Review

Description Will Thompson 2009-09-10 04:48:49 UTC
Printing out pages and pages of byte arrays, one byte per line, is not the most
helpful format dbus-monitor could use. :-) My branch 'monitor-ay-output'
special-cases arrays of bytes, printing them as tables of bytes with their
ascii counterparts (where possible) to the right, rather like hexdump -C.
Comment 1 Will Thompson 2009-09-10 04:49:23 UTC
Created attachment 29378 [details] [review]
Make array-printing case more readable
Comment 2 Will Thompson 2009-09-10 04:49:25 UTC
Created attachment 29379 [details] [review]
Print byte arrays as nicely-formatted hex.
Comment 3 Will Thompson 2009-09-10 04:49:26 UTC
Created attachment 29380 [details] [review]
Print ASCIIfied version of byte arrays where possible
Comment 4 Colin Walters 2009-10-16 11:59:58 UTC
Comment on attachment 29378 [details] [review]
Make array-printing case more readable

Won't this just add a "," at the end of existing elements?  I don't see how we're removing a newline here.
Comment 5 Colin Walters 2009-10-16 12:04:48 UTC
Comment on attachment 29379 [details] [review]
Print byte arrays as nicely-formatted hex.


>+
>+  /* Each byte takes 3 cells (two hexits, and a space), except the last one. */
>+  columns = (80 - ((depth + 1) * INDENT)) / 3;

I suppose if you were feeling ambitious you could check isatty (1) and get the width, but...80 is probably fine.

>+      if (current_type != DBUS_TYPE_INVALID)
>+        {
>+          if (n == columns)
>+            {
>+              printf ("\n");
>+              indent (depth + 1);
>+              n = 0;
>+            }
>+          else
>+            {
>+              printf (" ");
>+            }
>+        }
>+    }
>+
>+  printf ("\n");

This will give a double newline if the number of bytes equals the number of columns, no?

Otherwise looks fine.
Comment 6 Colin Walters 2009-10-16 12:07:42 UTC
Comment on attachment 29380 [details] [review]
Print ASCIIfied version of byte arrays where possible

Might it be better to detect the case where it's entirely ASCII, and just print it as-is?

In the mixed case you can probably copy&paste it into some hex-viewing tool (assuming it has some way to take hex-print as input I guess)
Comment 7 Will Thompson 2009-10-21 13:47:04 UTC
(In reply to comment #4)
> (From update of attachment 29378 [details] [review])
> Won't this just add a "," at the end of existing elements?  I don't see how
> we're removing a newline here.

Ah, the commit message is misleading. This is not meant to change behaviour at all; it just made the code easier for me to read.
Comment 8 Will Thompson 2009-10-21 13:52:21 UTC
(In reply to comment #5)
> I suppose if you were feeling ambitious you could check isatty (1) and get the
> width, but...80 is probably fine.

I didn't want to have to cope with the terminal resizing while dbus-monitor is running, which is why I just hard-coded 80. :-)

> >+      if (current_type != DBUS_TYPE_INVALID)
> >+        {
> >+          if (n == columns)
> >+            {
> >+              printf ("\n");
> >+              indent (depth + 1);
> >+              n = 0;
> >+            }
> >+          else
> >+            {
> >+              printf (" ");
> >+            }
> >+        }
> >+    }
> >+
> >+  printf ("\n");
> 
> This will give a double newline if the number of bytes equals the number of
> columns, no?

I don't think so: this block does not run for the last byte. The iterator is advanced (updating current_type, that is) just before this block.

Suggestions for a less misleading name for the variable? Or maybe I should just document it better. I had to re-read it a few times to figure out how it works. :)
Comment 9 Will Thompson 2009-10-21 13:55:46 UTC
(In reply to comment #6)
> (From update of attachment 29380 [details] [review])
> Might it be better to detect the case where it's entirely ASCII, and just print
> it as-is?
> 
> In the mixed case you can probably copy&paste it into some hex-viewing tool
> (assuming it has some way to take hex-print as input I guess)

I agree; I'll change this. (I guess it's actually pretty rare to pass a mixed byte-array over D-Bus. The ay cases I see in practice are file paths (Telepathy uses ay for those because they're not necessarily UTF-8) and avatars.)
Comment 10 Will Thompson 2009-11-01 05:51:23 UTC
Created attachment 30880 [details] [review]
Make array-printing code easier to follow

Previously dbus_message_iter_get_arg_type() was called twice: once in
the loop condition to update 'current_type', and once to check if the
loop will run again. This patch moves updating current_type to the end
of the loop body.
Comment 11 Will Thompson 2009-11-01 05:51:28 UTC
Created attachment 30881 [details] [review]
Print byte arrays as nicely-formatted hex.
Comment 12 Will Thompson 2009-11-01 05:51:34 UTC
Created attachment 30882 [details] [review]
Print all-printable-ASCII byte arrays as strings

In practice, ay seems to be used mostly for binary data (in which case,
hex output is fine) or for Unix file paths (because they may be
non-UTF-8) and similar human-readable strings. So let's print the latter
similarly to strings.
Comment 13 Will Thompson 2009-11-01 05:53:00 UTC
I've updated the commit message for the second patch, and replaced the third patch by one which just prints out the ASCII if possible, and otherwise just prints the hex without the column of possibly-ascii on the RHS.
Comment 14 Colin Walters 2009-11-03 13:13:36 UTC
Comment on attachment 30880 [details] [review]
Make array-printing code easier to follow

Looks good.
Comment 15 Colin Walters 2009-11-03 13:50:57 UTC
Comment on attachment 30881 [details] [review]
Print byte arrays as nicely-formatted hex.


>+  printf("array of bytes [\n");
>+
>+  indent(depth + 1);

Space between identifier and paren.

>+  printf ("\n");
>+  indent(depth);
>+  printf("]\n");
> }

Ditto.

Otherwise looks good.
Comment 16 Will Thompson 2009-11-04 09:14:08 UTC
Created attachment 30963 [details] [review]
Print byte arrays as nicely-formatted hex.

An updated version of the second patch, adding the missing spaces.
Comment 17 Will Thompson 2009-11-05 03:18:10 UTC
I merged the branch with the whitespace errors fixed. Thanks for the review!


Use of freedesktop.org services, including Bugzilla, is subject to our Code of Conduct. How we collect and use information is described in our Privacy Policy.