Browse Source

Removed named scope mess.

If I really need to have file/line, then I can use:

  BOOST_LOG_TRIVIAL(info) << boost::format("(%1%:%2%) ") %
  __FILE__ % __LINE__ << "session";

Which is easier to read and do then the horrible scope things
that don't really work well at all.
Steve Thielemann 3 years ago
parent
commit
a4d7c244b4
2 changed files with 30 additions and 12 deletions
  1. 22 9
      session.cpp
  2. 8 3
      twproxy.cpp

+ 22 - 9
session.cpp

@@ -1,7 +1,7 @@
 #include <boost/bind.hpp>
 #include <iostream>
 
-#include <boost/log/attributes/named_scope.hpp>
+#include <boost/format.hpp>
 #include <boost/log/core.hpp>
 #include <boost/log/trivial.hpp>
 
@@ -11,6 +11,8 @@
 
 #include <string>
 
+// #include <boost/log/attributes/named_scope.hpp>
+
 bool replace(std::string &str, const std::string &from, const std::string &to) {
   size_t start_pos = str.find(from);
   if (start_pos == std::string::npos)
@@ -32,10 +34,17 @@ bool replace(std::string &str, const char *from, const char *to) {
 }
 
 void ansi_clean(std::string &str) {
-  static std::regex ansi_cleaner("\x1b\[[0-9;]*[A-Zmh]", std::regex_constants::ECMAScript);
+  static std::regex ansi_cleaner("\x1b\[[0-9;]*[A-Zmh]",
+                                 std::regex_constants::ECMAScript);
   str = std::regex_replace(str, ansi_cleaner, "");
 }
 
+void high_ascii(std::string &str) {
+  static std::regex high_cleaner("[\x80-\xff]+",
+                                 std::regex_constants::ECMAScript);
+  str = std::regex_replace(str, high_cleaner, "#");
+}
+
 std::string clean_string(const std::string &source) {
   // BOOST_LOG_NAMED_SCOPE("clean_string");
   std::string clean = source;
@@ -45,6 +54,7 @@ std::string clean_string(const std::string &source) {
   // ANSI too
   ansi_clean(clean);
   // BOOST_LOG_TRIVIAL(error) << "cleaned: " << clean;
+  high_ascii(clean);
 
   replace(clean, "\x1b", "^");
 
@@ -62,8 +72,11 @@ session::session(boost::asio::ip::tcp::socket socket,
 }
 
 void session::start(void) {
-  BOOST_LOG_NAMED_SCOPE("session");
-  BOOST_LOG_TRIVIAL(info) << "session";
+  // BOOST_LOG_NAMED_SCOPE();
+
+  // If I want the file and line number information, here's how to do it:
+  BOOST_LOG_TRIVIAL(info) << boost::format("(%1%:%2%) ") % __FILE__ % __LINE__
+                          << "session";
   auto self(shared_from_this());
   // read_buffer.reserve(1024);
   // do_write("Welcome!\n");
@@ -71,7 +84,7 @@ void session::start(void) {
 }
 
 session::~session() {
-  BOOST_LOG_NAMED_SCOPE("session");
+  // BOOST_LOG_NAMED_SCOPE("session");
   BOOST_LOG_TRIVIAL(info) << "~session destructed";
 }
 
@@ -91,7 +104,7 @@ void session::parse_auth(void) {
 
 void session::on_connect(const boost::system::error_code error) {
   // We've connected to the server!  WOOT WOOT!
-  BOOST_LOG_NAMED_SCOPE("session");
+  // BOOST_LOG_NAMED_SCOPE("session");
 
   if (!error) {
     BOOST_LOG_TRIVIAL(info) << "Connected to " << host;
@@ -125,7 +138,7 @@ void session::on_connect(const boost::system::error_code error) {
 
 void session::dispatch_line(std::string line) {
   // Does this have \n\r still on it?  I don't want them.
-  BOOST_LOG_NAMED_SCOPE("session");
+  // BOOST_LOG_NAMED_SCOPE("session");
   std::string temp = clean_string(line);
   BOOST_LOG_TRIVIAL(info) << "SL: " << temp; // clean_string(line);
   // std::cout << "SL: " << line << std::endl;
@@ -217,7 +230,7 @@ void session::on_resolve(
 
   } else {
     // TO DO:
-    BOOST_LOG_NAMED_SCOPE("session");
+    // BOOST_LOG_NAMED_SCOPE("session");
     BOOST_LOG_TRIVIAL(error) << "Unable to resolve: " << host;
     std::string output = "Unable to resolve: ";
     output += host;
@@ -267,7 +280,7 @@ void session::do_read(void) {
             // std::cout << length << std::endl;
 
             // Proxy Active?
-            BOOST_LOG_NAMED_SCOPE("session");
+            // BOOST_LOG_NAMED_SCOPE("session");
 
             to_server(read_buffer);
             BOOST_LOG_TRIVIAL(info) << "C: " << read_buffer;

+ 8 - 3
twproxy.cpp

@@ -41,10 +41,13 @@ BOOST_LOG_ATTRIBUTE_KEYWORD(scope, "Scope",
 void init_logging(void) {
   // because TimeStamp is missing by default.
   boost::log::add_common_attributes();
+
+  /*
   auto fmtScope = boost::log::expressions::format_named_scope(
       "Scope", boost::log::keywords::format = "%n(%f:%l)",
       boost::log::keywords::iteration = boost::log::expressions::reverse,
       boost::log::keywords::depth = 2);
+  */
 
   boost::log::add_file_log(
       // This gives dated logfile name.
@@ -52,14 +55,16 @@ void init_logging(void) {
       // This appends to the logfile (instead of overwrite)
       boost::log::keywords::open_mode = std::ios_base::out | std::ios_base::app,
       boost::log::keywords::auto_flush = true,
-      
+
       // boost::log::keywords::format = "[%TimeStamp%] %Severity%  : %Message%"
       boost::log::keywords::format =
           (boost::log::expressions::stream
            << boost::log::expressions::format_date_time<
                   boost::posix_time::ptime>("TimeStamp", "%H:%M:%S.%f")
-           << " " << std::setw(8) << boost::log::trivial::severity << " "
-           << fmtScope << " : " << boost::log::expressions::smessage)
+           << " " << std::setw(8) << boost::log::trivial::severity
+           << " "
+           // << fmtScope << " : "
+           << boost::log::expressions::smessage)
 
   );