formatting(#985): define pretty-printing format for addresses

use a shortened display, showing only the last 4 bytes for diagnostics
since we're typically only interested in spotting "same" and "different",
while the full memory address is irrelevant
This commit is contained in:
Fischlurch 2016-01-06 04:51:37 +01:00
parent ee52a83cb2
commit 2cf127e16a
5 changed files with 41 additions and 15 deletions

View file

@ -93,7 +93,7 @@ namespace std {
operator<< (ostream& os, X* ptr)
{
if (ptr)
return os << (void*)ptr << "" << *ptr;
return util::showAddr(os, ptr) << "" << *ptr;
else
return os << "⟂ «" << lib::meta::typeStr<X>() << "»";
}

View file

@ -64,8 +64,8 @@ namespace { // hard-wired configuration for debugging output....
const auto DIAGNOSTICS_DOUBLE_PRECISION = 8;
const auto DIAGNOSTICS_FLOAT_PRECISION = 5;
/** amount of hex digits required to represent an address on this plattform */
const auto PLATFORM_ADDRESS_HEX_DIGITS = sizeof(size_t) * 2;
/** show only this amount of trailing bytes from an address */
const size_t DIAGNOSTICS_ADDRESS_SUFFIX_LEN = 4;
}
@ -162,12 +162,13 @@ namespace util {
}//(End) implementation details
using std::ostringstream;
using std::hex;
using std::setw;
using std::right;
using std::setfill;
using std::noshowbase;
using std::ostringstream;
using std::ostream;
/**
@ -192,7 +193,7 @@ namespace util {
string
showFloat (float val) noexcept
try {
std::ostringstream buffer;
ostringstream buffer;
buffer.precision (DIAGNOSTICS_FLOAT_PRECISION);
buffer << val;
return buffer.str();
@ -201,16 +202,26 @@ namespace util {
{ return ""; }
/** @note show only the trailing X bytes of any address */
ostream&
showAddr (ostream& stream, void* addr)
{
size_t suffix_modulus = size_t(1) << DIAGNOSTICS_ADDRESS_SUFFIX_LEN * 8;
return stream << ""
<< hex
<< noshowbase
<< setw (DIAGNOSTICS_ADDRESS_SUFFIX_LEN * 2) // need 2 hex digits per byte
<< setfill('_')
<< right
<< size_t(addr) % suffix_modulus;
}
string
showAddr (void *addr) noexcept
try {
std::ostringstream buffer;
buffer << hex
<< noshowbase
<< setw (PLATFORM_ADDRESS_HEX_DIGITS)
<< setfill('_')
<< right
<< addr;
ostringstream buffer;
showAddr (buffer, addr);
return buffer.str();
}
catch(...)

View file

@ -49,6 +49,18 @@
#include <boost/lexical_cast.hpp>
namespace std { // forward declaration to avoid including <iostream>
template<typename C>
struct char_traits;
template<typename C, class _TRAITS>
class basic_ostream;
using ostream = basic_ostream<char, char_traits<char>>;
}
namespace lib {
class Literal;
@ -77,6 +89,9 @@ namespace util {
std::string showFloat (float) noexcept;
std::string showAddr (void *addr) noexcept;
/** preconfigured format for pretty-printing of addresses */
std::ostream& showAddr (std::ostream&, void* addr);
} // namespace util

View file

@ -124,10 +124,10 @@ namespace std { // forward declaration to avoid including <iostream>
template<typename C, class _TRAITS>
class basic_ostream;
typedef basic_ostream<char, char_traits<char> > ostream;
using ostream = basic_ostream<char, char_traits<char>>;
}
namespace lib {
class Literal;
class Symbol;

View file

@ -264,7 +264,7 @@ namespace util {
std::string showDouble (double) noexcept;
std::string showFloat (float) noexcept;
/** pretty-print an address as hex-string */
/** pretty-print an address as hex-suffix */
std::string showAddr (void *addr) noexcept;
template<typename X>