diff options
Diffstat (limited to 'cpp/src')
-rw-r--r-- | cpp/src/qpid/log/Statement.cpp | 23 | ||||
-rw-r--r-- | cpp/src/tests/logging.cpp | 6 |
2 files changed, 15 insertions, 14 deletions
diff --git a/cpp/src/qpid/log/Statement.cpp b/cpp/src/qpid/log/Statement.cpp index 2935de9071..9b6fb7feaf 100644 --- a/cpp/src/qpid/log/Statement.cpp +++ b/cpp/src/qpid/log/Statement.cpp @@ -22,6 +22,7 @@ #include <stdexcept> #include <algorithm> #include <syslog.h> +#include <ctype.h> namespace qpid { namespace log { @@ -29,21 +30,21 @@ namespace log { namespace { using namespace std; -struct IsControl { bool operator()(unsigned char c) { return c < 32; } }; +struct NonPrint { bool operator()(unsigned char c) { return !isprint(c); } }; -bool isClean(const std::string& str) { - return std::find_if(str.begin(), str.end(), IsControl()) == str.end(); -} +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) { - IsControl isControl; - size_t n = std::count_if(str.begin(), str.end(), isControl); + 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()+n); // Avoid extra allocations. + ret.reserve(str.size()+2*n); // Avoid extra allocations. for (string::const_iterator i = str.begin(); i != str.end(); ++i) { - if (isControl(*i)) { - ret.push_back('^'); - ret.push_back((*i)+64); + if (nonPrint(*i)) { + ret.push_back('\\'); + ret.push_back(hex[((*i) >> 4)&0xf]); + ret.push_back(hex[(*i) & 0xf]); } else ret.push_back(*i); } @@ -53,7 +54,7 @@ std::string quote(const std::string& str) { } void Statement::log(const std::string& message) { - Logger::instance().log(*this, isClean(message) ? message : quote(message)); + Logger::instance().log(*this, quote(message)); } Statement::Initializer::Initializer(Statement& s) : statement(s) { diff --git a/cpp/src/tests/logging.cpp b/cpp/src/tests/logging.cpp index 4969c3d6a9..2c0ed08105 100644 --- a/cpp/src/tests/logging.cpp +++ b/cpp/src/tests/logging.cpp @@ -367,7 +367,7 @@ BOOST_AUTO_TEST_CASE(testLoggerConfigure) { unlink("logging.tmp"); } -BOOST_AUTO_TEST_CASE(testQuoteControlChars) { +BOOST_AUTO_TEST_CASE(testQuoteNonPrintable) { Logger& l=Logger::instance(); l.clear(); Options opts; @@ -375,13 +375,13 @@ BOOST_AUTO_TEST_CASE(testQuoteControlChars) { opts.outputs.push_back("logging.tmp"); opts.time=false; l.configure(opts, "test"); - char s[] = "null\0tab\tspace newline\nret\r"; + char s[] = "null\0tab\tspace newline\nret\r\x80\x99\xff"; string str(s, sizeof(s)); QPID_LOG(critical, str); ifstream log("logging.tmp"); string line; getline(log, line); - string expect="critical null^@tab^Ispace newline^Jret^M^@"; + string expect="critical null\\00tab\\09space newline\\0Aret\\0D\\80\\99\\FF\\00"; BOOST_CHECK_EQUAL(expect, line); log.close(); unlink("logging.tmp"); |