automatically sanitize all messages logged through I2n::Logger, provide an option...
authorGerd von Egidy <gerd.von.egidy@intra2net.com>
Wed, 16 Dec 2015 10:43:41 +0000 (11:43 +0100)
committerGerd von Egidy <gerd.von.egidy@intra2net.com>
Wed, 16 Dec 2015 10:43:41 +0000 (11:43 +0100)
src/logfunc.cpp
src/logfunc.hpp

index 3b80119..f21d504 100644 (file)
@@ -196,6 +196,7 @@ std::string get_level_tag( int level )
  * @brief the "real" log function which logs a message at a given level.
  * @param level the log level to log the message at.
  * @param msg the message.
+ * @param keep_unsafe_chars don't replace characters that are considered unsafe to log, e.g. control characters.
  *
  * Write the message to every enabled log channel.
  *
@@ -205,7 +206,7 @@ std::string get_level_tag( int level )
  * and process information (like syslog does). The message is splitted at line ends and
  * consecutive lines are indented.
  */
-void log_msg( int level, const std::string& msg)
+void log_msg( int level, const std::string& msg, bool keep_unsafe_chars)
 {
     boost::recursive_mutex::scoped_lock lock(LoggerLock);
 
@@ -217,7 +218,13 @@ void log_msg( int level, const std::string& msg)
 
     if (g_syslog_opened)
     {
-        ::syslog( get_syslog_level(level), "%s", msg.c_str());
+        std::string sane_msg;
+        if (keep_unsafe_chars)
+            sane_msg=msg;
+        else
+            sane_msg=sanitize_for_logging(msg);
+
+        ::syslog( get_syslog_level(level), "%s", sane_msg.c_str());
     }
     // file(/stream) logging:
     if (g_stderr_log or g_log_stream_ptr) // add more log "enabled" expressions here...
@@ -257,7 +264,11 @@ void log_msg( int level, const std::string& msg)
                     {
                         ostr << indent_string;
                     }
-                    ostr << *it << std::endl;
+
+                    if (keep_unsafe_chars)
+                        ostr << *it << std::endl;
+                    else
+                        ostr << sanitize_for_logging(*it) << std::endl;
                 }
                 new_msg= ostr.str();
             }
@@ -279,13 +290,15 @@ void log_msg( int level, const std::string& msg)
  * @param level the log level.
  * @param part the part(/module) name(/id)
  * @param msg the log message.
+ * @param keep_unsafe_chars don't replace characters that are considered unsafe to log, e.g. control characters.
  *
  * basically calls @a log(), but prepends the part (if not empty) in square brackets to the message.
  */
 void log_part_msg(
     int level,
     const std::string& part,
-    const std::string& msg)
+    const std::string& msg,
+    bool keep_unsafe_chars)
 {
     // Note: Locking is done in log_msg()
 
@@ -293,11 +306,11 @@ void log_part_msg(
     {
         std::ostringstream ostr;
         ostr << "[" << part << "] " << msg;
-        log_msg(level, ostr.str());
+        log_msg(level, ostr.str(), keep_unsafe_chars);
     }
     else
     {
-        log_msg(level, msg);
+        log_msg(level, msg, keep_unsafe_chars);
     }
 } // eo log_part_msg(int,const std::string&,const std::string&)
 
@@ -438,7 +451,7 @@ PartLogger::LogHelper::~LogHelper()
  * @param part name of the part (module name) using the logger instance.
  */
 PartLogger::PartLogger(const std::string& part)
-: Part(part)
+: Part(part), keep_unsafe_chars(false)
 {
 } // eo PartLogger::PartLogger(const std.:string&)
 
@@ -452,6 +465,7 @@ PartLogger::PartLogger(const std::string& part)
  * currently known extensions: cpp, cxx, c++, cc, C).
  */
 PartLogger::PartLogger( const SourceLocation& loc )
+: keep_unsafe_chars(false)
 {
     if (loc.Line>0 && ! loc.File.empty())
     {
@@ -479,65 +493,73 @@ PartLogger::~PartLogger()
  * @param level the log level.
  * @param msg the log message.
  */
-void PartLogger::log(int level, const std::string &msg)
+void PartLogger::log(int level, const std::string &msg, bool keep_unsafe_chars)
 {
     boost::recursive_mutex::scoped_lock lock(LoggerLock);
 
     if (level <= g_max_level)
     {
-        log_part_msg(level, Part, msg);
+        log_part_msg(level, Part, msg, keep_unsafe_chars);
     }
 } // eo PartLogger::log(int,const std::string);
 
 
 void PartLogger::fatal(const std::string& msg)
 {
-    log(LOG_EMERG,msg);
+    log(LOG_EMERG,msg,keep_unsafe_chars);
 } // eo PartLogger::fatal(const std::string&)
 
 
 void PartLogger::alert(const std::string& msg)
 {
-    log(LOG_ALERT,msg);
+    log(LOG_ALERT,msg,keep_unsafe_chars);
 } // eo PartLogger::alert(const std::string&)
 
 
 void PartLogger::critical(const std::string& msg)
 {
-    log(LOG_CRIT,msg);
+    log(LOG_CRIT,msg,keep_unsafe_chars);
 } // eo PartLogger::critical(const std::string&)
 
 
 void PartLogger::error(const std::string& msg)
 {
-    log(LOG_ERR, msg);
+    log(LOG_ERR, msg,keep_unsafe_chars);
 } // eo PartLogger::error(const std::string&)
 
 
 void PartLogger::warning(const std::string& msg)
 {
-    log(LOG_WARNING, msg);
+    log(LOG_WARNING, msg,keep_unsafe_chars);
 } // eo PartLogger::warning(const std::string&)
 
 
 void PartLogger::notice(const std::string& msg)
 {
-    log(LOG_NOTICE, msg);
+    log(LOG_NOTICE, msg,keep_unsafe_chars);
 } // eo PartLogger::notice(const std::string&)
 
 
 void PartLogger::info(const std::string& msg)
 {
-    log(LOG_INFO, msg);
+    log(LOG_INFO, msg,keep_unsafe_chars);
 } // eo PartLogger::info(const std::string&)
 
 
 void PartLogger::debug(const std::string& msg)
 {
-    log(LOG_DEBUG, msg);
+    log(LOG_DEBUG, msg,keep_unsafe_chars);
 } // eo PartLogger::debug(const std::string&)
 
+void PartLogger::set_keep_unsafe_chars(bool _keep_unsafe_chars)
+{
+    keep_unsafe_chars=_keep_unsafe_chars;
+}
 
+bool PartLogger::get_keep_unsafe_chars()
+{
+    return keep_unsafe_chars;
+}
 
 PartLogger::LogHelper PartLogger::fatal(const SourceLocation& loc)
 {
index 1cd03da..34d1134 100644 (file)
@@ -153,7 +153,7 @@ class PartLogger
         PartLogger( const SourceLocation& loc );
         virtual ~PartLogger();
 
-        void log(int level, const std::string &msg);
+        void log(int level, const std::string &msg, bool keep_unsafe_chars=false);
 
         void fatal(const std::string& msg);
         void alert(const std::string& msg);
@@ -173,9 +173,13 @@ class PartLogger
         LogHelper info(const SourceLocation& loc= SourceLocation());
         LogHelper debug(const SourceLocation& loc= SourceLocation());
 
+        void set_keep_unsafe_chars(bool _keep_unsafe_chars);
+        bool get_keep_unsafe_chars();
+
     protected:
 
         std::string Part;
+        bool keep_unsafe_chars;
 
 }; // eo class PartLogger