From: Alex Rousskov Date: Thu, 29 Oct 2015 05:43:10 +0000 (-0600) Subject: Support dumping raw data in hex. Suitable for decoding with xxd -r -p. X-Git-Tag: SQUID_4_0_13~5^2~25 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=808449fc52754479b3e3f71b5585ff762e54bee7;p=thirdparty%2Fsquid.git Support dumping raw data in hex. Suitable for decoding with xxd -r -p. --- diff --git a/src/Debug.h b/src/Debug.h index bb083a7b21..94a6fc3552 100644 --- a/src/Debug.h +++ b/src/Debug.h @@ -159,11 +159,14 @@ class Raw { public: Raw(const char *label, const char *data, const size_t size): - level(-1), label_(label), data_(data), size_(size) {} + level(-1), label_(label), data_(data), size_(size), useHex_(false) {} /// limit data printing to at least the given debugging level Raw &minLevel(const int aLevel) { level = aLevel; return *this; } + /// print data using two hex digits per byte (decoder: xxd -r -p) + Raw &hex() { useHex_ = true; return *this; } + /// If debugging is prohibited by the current debugs() or section level, /// prints nothing. Otherwise, dumps data using one of these formats: /// " label[size]=data" if label was set and data size is positive @@ -178,9 +181,12 @@ public: int level; private: + void printHex(std::ostream &os) const; + const char *label_; ///< optional data name or ID; triggers size printing const char *data_; ///< raw data to be printed size_t size_; ///< data length + bool useHex_; ///< whether hex() has been called }; inline diff --git a/src/debug.cc b/src/debug.cc index 92b7f4b4a5..1f9392a360 100644 --- a/src/debug.cc +++ b/src/debug.cc @@ -14,6 +14,8 @@ #include "SquidTime.h" #include "util.h" +#include + /* for shutting_down flag in xassert() */ #include "globals.h" @@ -793,6 +795,19 @@ SkipBuildPrefix(const char* path) return path+BuildPrefixLength; } +/// print data bytes using hex notation +void +Raw::printHex(std::ostream &os) const +{ + const auto savedFill = os.fill('0'); + const auto savedFlags = os.flags(); // std::ios_base::fmtflags + os << std::hex; + std::for_each(data_, data_ + size_, + [&os](const char &c) { os << std::setw(2) << static_cast(c); }); + os.flags(savedFlags); + os.fill(savedFill); +} + std::ostream & Raw::print(std::ostream &os) const { @@ -807,10 +822,14 @@ Raw::print(std::ostream &os) const (size_ > 40 ? DBG_DATA : Debug::sectionLevel); if (finalLevel <= Debug::sectionLevel) { os << (label_ ? '=' : ' '); - if (data_) - os.write(data_, size_); - else + if (data_) { + if (useHex_) + printHex(os); + else + os.write(data_, size_); + } else { os << "[null]"; + } } return os;