From cdad9f88b7e9aed5035d8409eacd1ad62a2bf44d Mon Sep 17 00:00:00 2001 From: DjWarmonger Date: Sun, 30 Aug 2015 19:26:19 +0200 Subject: [PATCH] Relaxed Mutex of Logger. Attempt to optimize format function. --- lib/logging/CLogger.cpp | 18 +++++++++--------- lib/logging/CLogger.h | 9 ++++++--- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/lib/logging/CLogger.cpp b/lib/logging/CLogger.cpp index 89856449f..d93506622 100644 --- a/lib/logging/CLogger.cpp +++ b/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(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; } diff --git a/lib/logging/CLogger.h b/lib/logging/CLogger.h index d5ec400d3..e26529390 100644 --- a/lib/logging/CLogger.h +++ b/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(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.