1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-07-01 00:45:26 +02:00

- Some performance improvements for logging

This commit is contained in:
beegee1
2014-04-01 19:46:47 +00:00
parent 30b79588db
commit 48e7b7d805
2 changed files with 24 additions and 9 deletions

View File

@ -272,7 +272,8 @@ CLogger * CLogManager::getLogger(const CLoggerDomain & domain)
CLogFormatter::CLogFormatter() : pattern("%m") CLogFormatter::CLogFormatter() : pattern("%m")
{ {
boost::posix_time::time_facet * facet = new boost::posix_time::time_facet("%H:%M:%S");
dateStream.imbue(std::locale(dateStream.getloc(), facet));
} }
CLogFormatter::CLogFormatter(const std::string & pattern) CLogFormatter::CLogFormatter(const std::string & pattern)
@ -280,16 +281,26 @@ CLogFormatter::CLogFormatter(const std::string & pattern)
setPattern(pattern); setPattern(pattern);
} }
CLogFormatter::CLogFormatter(const CLogFormatter & other)
{
*this = other;
}
CLogFormatter & CLogFormatter::operator=(const CLogFormatter & other)
{
pattern = other.pattern;
return *this;
}
std::string CLogFormatter::format(const LogRecord & record) const std::string CLogFormatter::format(const LogRecord & record) const
{ {
std::string message = pattern; std::string message = pattern;
// Format date // Format date
std::stringstream dateStream; dateStream.str(std::string());
boost::posix_time::time_facet * facet = new boost::posix_time::time_facet("%H:%M:%S"); dateStream.clear();
dateStream.imbue(std::locale(dateStream.getloc(), facet));
dateStream << record.timeStamp; dateStream << record.timeStamp;
boost::algorithm::replace_all(message, "%d", dateStream.str()); boost::algorithm::replace_first(message, "%d", dateStream.str());
// Format log level // Format log level
std::string level; std::string level;
@ -311,12 +322,12 @@ std::string CLogFormatter::format(const LogRecord & record) const
level = "ERROR"; level = "ERROR";
break; break;
} }
boost::algorithm::replace_all(message, "%l", level); boost::algorithm::replace_first(message, "%l", level);
// Format name, thread id and message // Format name, thread id and message
boost::algorithm::replace_all(message, "%n", record.domain.getName()); boost::algorithm::replace_first(message, "%n", record.domain.getName());
boost::algorithm::replace_all(message, "%t", boost::lexical_cast<std::string>(record.threadId)); boost::algorithm::replace_first(message, "%t", boost::lexical_cast<std::string>(record.threadId));
boost::algorithm::replace_all(message, "%m", record.message); boost::algorithm::replace_first(message, "%m", record.message);
return message; return message;
} }

View File

@ -210,6 +210,9 @@ public:
CLogFormatter(); CLogFormatter();
CLogFormatter(const std::string & pattern); CLogFormatter(const std::string & pattern);
CLogFormatter(const CLogFormatter & other);
CLogFormatter & operator=(const CLogFormatter & other);
void setPattern(const std::string & pattern); void setPattern(const std::string & pattern);
const std::string & getPattern() const; const std::string & getPattern() const;
@ -217,6 +220,7 @@ public:
private: private:
std::string pattern; std::string pattern;
mutable std::stringstream dateStream;
}; };
/// The interface ILogTarget is used by all log target implementations. It holds /// The interface ILogTarget is used by all log target implementations. It holds