* @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.
*
* 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);
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...
{
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();
}
* @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()
{
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&)
* @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&)
* currently known extensions: cpp, cxx, c++, cc, C).
*/
PartLogger::PartLogger( const SourceLocation& loc )
+: keep_unsafe_chars(false)
{
if (loc.Line>0 && ! loc.File.empty())
{
* @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)
{