Escaped argument for external warning log.
authorBjoern Sikora <bjoern.sikora@intra2net.com>
Fri, 25 Jun 2010 09:27:49 +0000 (11:27 +0200)
committerBjoern Sikora <bjoern.sikora@intra2net.com>
Fri, 25 Jun 2010 09:27:49 +0000 (11:27 +0200)
src/logger.cpp
src/logger.hpp

index 7b4fd58..3dc2486 100644 (file)
@@ -82,6 +82,28 @@ void Logger::log_notice(const string& msg) const
 
 
 /**
+* Escape shell arguments.
+* @param input The input to escape.
+* @return The escaped string ready to use for the shell.
+*/
+string Logger::escape_shellarg(const string &input)
+{
+    string output = "'";
+    string::const_iterator it, it_end = input.end();
+    for (it = input.begin(); it != it_end; it++)
+    {
+        if ( (*it) == '\'')
+            output += "'\\'";
+
+        output += *it;
+    }
+
+    output += "'";
+    return output;
+}
+
+
+/**
  * Decides if Logging through syslog if enabled or through std.
  * @param msg The message to log.
  */
@@ -99,6 +121,8 @@ void Logger::log_warning(const string& msg, int level)
         if (!message.empty() && message[message.length()-1] == '\n')
             message.erase(message.length()-1);  /*lint !e534 */
 
+        message = escape_shellarg(message);
+
         string external = ExternalWarningLog;
         external.append(" ");
         external.append("\"");
index d5ae694..d3c82ec 100644 (file)
@@ -42,6 +42,8 @@ public:
 
     void clear_external_send_messages();
 
+    std::string escape_shellarg(const std::string &input);
+
     void log_notice(const std::string& msg) const;
 
     void log_warning(const std::string& msg, int loglevel);