summaryrefslogtreecommitdiff
path: root/cpp/src/qpid/log/Statement.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'cpp/src/qpid/log/Statement.cpp')
-rw-r--r--cpp/src/qpid/log/Statement.cpp25
1 files changed, 24 insertions, 1 deletions
diff --git a/cpp/src/qpid/log/Statement.cpp b/cpp/src/qpid/log/Statement.cpp
index 85b4d1f155..7dfdf08703 100644
--- a/cpp/src/qpid/log/Statement.cpp
+++ b/cpp/src/qpid/log/Statement.cpp
@@ -24,9 +24,32 @@
#include <ctype.h>
namespace qpid {
-std::string quote(const std::string& str); // Defined in Msg.cpp
namespace log {
+namespace {
+struct NonPrint { bool operator()(unsigned char c) { return !isprint(c) && !isspace(c); } };
+
+const char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
+
+std::string quote(const std::string& str) {
+ NonPrint nonPrint;
+ size_t n = std::count_if(str.begin(), str.end(), nonPrint);
+ if (n==0) return str;
+ std::string ret;
+ ret.reserve(str.size()+2*n); // Avoid extra allocations.
+ for (std::string::const_iterator i = str.begin(); i != str.end(); ++i) {
+ if (nonPrint(*i)) {
+ ret.push_back('\\');
+ ret.push_back('x');
+ ret.push_back(hex[((*i) >> 4)&0xf]);
+ ret.push_back(hex[(*i) & 0xf]);
+ }
+ else ret.push_back(*i);
+ }
+ return ret;
+}
+}
+
void Statement::log(const std::string& message) {
Logger::instance().log(*this, quote(message));
}