Pārlūkot izejas kodu

- Some performance improvements for logging

beegee1 11 gadi atpakaļ
vecāks
revīzija
48e7b7d805
2 mainītis faili ar 24 papildinājumiem un 9 dzēšanām
  1. 20 9
      lib/logging/CLogger.cpp
  2. 4 0
      lib/logging/CLogger.h

+ 20 - 9
lib/logging/CLogger.cpp

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

+ 4 - 0
lib/logging/CLogger.h

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