You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by ac...@apache.org on 2008/02/12 19:04:16 UTC

svn commit: r620889 - in /incubator/qpid/trunk/qpid/cpp/src: qpid/log/Statement.cpp tests/logging.cpp

Author: aconway
Date: Tue Feb 12 10:04:11 2008
New Revision: 620889

URL: http://svn.apache.org/viewvc?rev=620889&view=rev
Log:
Quote all non-printable ASCII characters (not just control characters)

Modified:
    incubator/qpid/trunk/qpid/cpp/src/qpid/log/Statement.cpp
    incubator/qpid/trunk/qpid/cpp/src/tests/logging.cpp

Modified: incubator/qpid/trunk/qpid/cpp/src/qpid/log/Statement.cpp
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/qpid/log/Statement.cpp?rev=620889&r1=620888&r2=620889&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/qpid/log/Statement.cpp (original)
+++ incubator/qpid/trunk/qpid/cpp/src/qpid/log/Statement.cpp Tue Feb 12 10:04:11 2008
@@ -22,6 +22,7 @@
 #include <stdexcept>
 #include <algorithm>
 #include <syslog.h>
+#include <ctype.h>
 
 namespace qpid {
 namespace log {
@@ -29,21 +30,21 @@
 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 @@
 }
 
 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) {

Modified: incubator/qpid/trunk/qpid/cpp/src/tests/logging.cpp
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/tests/logging.cpp?rev=620889&r1=620888&r2=620889&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/tests/logging.cpp (original)
+++ incubator/qpid/trunk/qpid/cpp/src/tests/logging.cpp Tue Feb 12 10:04:11 2008
@@ -367,7 +367,7 @@
     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 @@
     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");