Ver código fonte

Relaxed Mutex of Logger. Attempt to optimize format function.

DjWarmonger 10 anos atrás
pai
commit
cdad9f88b7
2 arquivos alterados com 15 adições e 12 exclusões
  1. 9 9
      lib/logging/CLogger.cpp
  2. 6 3
      lib/logging/CLogger.h

+ 9 - 9
lib/logging/CLogger.cpp

@@ -230,13 +230,10 @@ std::string CLogFormatter::format(const LogRecord & record) const
 {
 	std::string message = pattern;
 
-	// Format date
-	dateStream.str(std::string());
-	dateStream.clear();
-	dateStream << record.timeStamp;
-	boost::algorithm::replace_first(message, "%d", dateStream.str());
+	//Format date
+	boost::algorithm::replace_first(message, "%d", boost::posix_time::to_simple_string (record.timeStamp));
 
-	// Format log level
+	//Format log level 
 	std::string level;
 	switch(record.level)
 	{
@@ -258,11 +255,13 @@ std::string CLogFormatter::format(const LogRecord & record) const
 	}
 	boost::algorithm::replace_first(message, "%l", level);
 
-	// Format name, thread id and message
+	//Format name, thread id and 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, "%t", record.threadId);
 	boost::algorithm::replace_first(message, "%m", record.message);
 
+	//return boost::to_string (boost::format("%d %d %d[%d] - %d") % dateStream.str() % level % record.domain.getName() % record.threadId % record.message);
+
 	return message;
 }
 
@@ -365,8 +364,9 @@ CLogFileTarget::CLogFileTarget(boost::filesystem::path filePath, bool append /*=
 
 void CLogFileTarget::write(const LogRecord & record)
 {
+	std::string message = formatter.format(record); //formatting is slow, do it outside the lock
 	TLockGuard _(mx);
-	file << formatter.format(record) << std::endl;
+	file << message << std::endl;
 }
 
 const CLogFormatter & CLogFileTarget::getFormatter() const { return formatter; }

+ 6 - 3
lib/logging/CLogger.h

@@ -188,14 +188,17 @@ private:
 struct DLL_LINKAGE LogRecord
 {
 	LogRecord(const CLoggerDomain & domain, ELogLevel::ELogLevel level, const std::string & message)
-		: domain(domain), level(level), message(message), timeStamp(boost::posix_time::microsec_clock::local_time()),
-		  threadId(boost::this_thread::get_id()) { }
+		: domain(domain),
+		level(level),
+		message(message),
+		timeStamp(boost::posix_time::microsec_clock::local_time()),
+		threadId(boost::lexical_cast<std::string>(boost::this_thread::get_id())) { }
 
 	CLoggerDomain domain;
 	ELogLevel::ELogLevel level;
 	std::string message;
 	boost::posix_time::ptime timeStamp;
-	boost::thread::id threadId;
+	std::string threadId;
 };
 
 /// The class CLogFormatter formats log records.