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/07 15:25:38 UTC

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

Author: aconway
Date: Thu Feb  7 06:25:32 2008
New Revision: 619424

URL: http://svn.apache.org/viewvc?rev=619424&view=rev
Log:

Quote unprintable control characters in log output.

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=619424&r1=619423&r2=619424&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/qpid/log/Statement.cpp (original)
+++ incubator/qpid/trunk/qpid/cpp/src/qpid/log/Statement.cpp Thu Feb  7 06:25:32 2008
@@ -18,14 +18,42 @@
 
 #include "Statement.h"
 #include "Logger.h"
+#include <boost/bind.hpp>
 #include <stdexcept>
+#include <algorithm>
 #include <syslog.h>
 
 namespace qpid {
 namespace log {
 
+namespace {
+using namespace std;
+
+struct IsControl { bool operator()(unsigned char c) { return c < 32; } };
+
+bool isClean(const std::string& str) {
+    return std::find_if(str.begin(), str.end(), IsControl()) == str.end();
+}
+
+std::string quote(const std::string& str) {
+    IsControl isControl;
+    size_t n = std::count_if(str.begin(), str.end(), isControl);
+    std::string ret;
+    ret.reserve(str.size()+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);
+        }
+        else ret.push_back(*i);
+    }
+    return ret;
+}
+
+}
+
 void Statement::log(const std::string& message) {
-    Logger::instance().log(*this,message);
+    Logger::instance().log(*this, isClean(message) ? message : 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=619424&r1=619423&r2=619424&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/tests/logging.cpp (original)
+++ incubator/qpid/trunk/qpid/cpp/src/tests/logging.cpp Thu Feb  7 06:25:32 2008
@@ -367,4 +367,24 @@
     unlink("logging.tmp");
 }
 
+BOOST_AUTO_TEST_CASE(testQuoteControlChars) {
+    Logger& l=Logger::instance();
+    l.clear();
+    Options opts;
+    opts.outputs.clear();
+    opts.outputs.push_back("logging.tmp");
+    opts.time=false;
+    l.configure(opts, "test");
+    char s[] = "null\0tab\tspace newline\nret\r";
+    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^@";
+    BOOST_CHECK_EQUAL(expect, line);
+    log.close();
+    unlink("logging.tmp");
+}
+
 QPID_AUTO_TEST_SUITE_END()