From 48e7b7d805135c440470f17c9831d00ec6e93cb5 Mon Sep 17 00:00:00 2001 From: beegee1 Date: Tue, 1 Apr 2014 19:46:47 +0000 Subject: [PATCH] - Some performance improvements for logging --- lib/logging/CLogger.cpp | 29 ++++++++++++++++++++--------- lib/logging/CLogger.h | 4 ++++ 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/lib/logging/CLogger.cpp b/lib/logging/CLogger.cpp index b3a6f0617..53a203982 100644 --- a/lib/logging/CLogger.cpp +++ b/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(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(record.threadId)); + boost::algorithm::replace_first(message, "%m", record.message); return message; } diff --git a/lib/logging/CLogger.h b/lib/logging/CLogger.h index 9689751c5..efa0acb13 100644 --- a/lib/logging/CLogger.h +++ b/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