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:
parent
ee52a83cb2
commit
2cf127e16a
5 changed files with 41 additions and 15 deletions
|
|
@ -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>() << "»";
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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(...)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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>
|
||||
|
|
|
|||
Loading…
Reference in a new issue