This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Extending vfprintf and family
- From: Philip Prindeville <philipp_subx at redfish-solutions dot com>
- To: libc-alpha at sourceware dot org
- Date: Thu, 14 Aug 2014 12:39:04 -0600
- Subject: Extending vfprintf and family
- Authentication-results: sourceware.org; auth=none
Hi.
I work a lot with routing, protocols, and firewalls so I’m constantly having to dump packets for tracing, logging, auditing, etc.
Common types I need to format are:
* IPv4 dotted quads
* MAC addresses (not just Ethernet/Wifi)
* IPv6 addresses
* uint16_t and uint32_t in network (BigEndian) byte order
* n-byte hex dump (BigEndian) of an arbitrary blob
* safe n-byte string formatting (printable characters get printed, others [including NUL] get escaped into \xHH or \OOO)
It’s a bit tedious (not to mention inefficient) to have to call inet_ntop() to format a machine address into a readable format (via a temporary buffer), then interpolate that string into a logging message via syslog() or snprintf(), etc. What would be really great would be adding extensions (which can be turned off or on, depending on whether strict compliance with C99, ANSI C, etc is required) as format specifiers.
When I was at Cisco in the IOS group, there was a way to register on-the-fly additional format specifiers that *printf() could then leverage; some of the formatting helpers were quite sophisticated, understanding ASN.1 or X.509 certs, etc. Not sure we need to be that complex, though I wouldn’t reject it if it were offered, either.
One would be able to specify both precision and width, e.g. “I have a 20 byte safe string I want to format, but not into more than 32 columns” so that if there was a lot of escaping and the string overflowed the 32-byte output count, it would be truncated.
Does anyone else see the value in doing this?
Thanks,
-Philip