From c142bf1072eff828588005e3eded72e99b79b0d0 Mon Sep 17 00:00:00 2001 From: Konstantin Date: Wed, 15 Feb 2023 00:35:06 +0300 Subject: [PATCH] vcmi: modernize lib/logging --- lib/logging/CBasicLogConfigurator.cpp | 3 ++- lib/logging/CBasicLogConfigurator.h | 1 + lib/logging/CLogger.cpp | 29 ++++++++------------------- lib/logging/CLogger.h | 9 ++------- 4 files changed, 13 insertions(+), 29 deletions(-) diff --git a/lib/logging/CBasicLogConfigurator.cpp b/lib/logging/CBasicLogConfigurator.cpp index b0d0f78f8..b6a2f0bf5 100644 --- a/lib/logging/CBasicLogConfigurator.cpp +++ b/lib/logging/CBasicLogConfigurator.cpp @@ -37,7 +37,7 @@ void CBasicLogConfigurator::configure() const JsonNode & loggers = loggingNode["loggers"]; if(!loggers.isNull()) { - for(auto & loggerNode : loggers.Vector()) + for(const auto & loggerNode : loggers.Vector()) { // Get logger std::string name = loggerNode["domain"].String(); @@ -148,6 +148,7 @@ void CBasicLogConfigurator::deconfigure() auto l = CLogger::getGlobalLogger(); if(l != nullptr) l->clearTargets(); + appendToLogFile = true; } VCMI_LIB_NAMESPACE_END diff --git a/lib/logging/CBasicLogConfigurator.h b/lib/logging/CBasicLogConfigurator.h index ba96f3d3f..7a62e3bed 100644 --- a/lib/logging/CBasicLogConfigurator.h +++ b/lib/logging/CBasicLogConfigurator.h @@ -35,6 +35,7 @@ public: /// Removes all targets from the global logger. void deconfigure(); + private: // Gets ELogLevel enum from string. (Should be moved to CLogger as a separate function?) // Throws: std::runtime_error diff --git a/lib/logging/CLogger.cpp b/lib/logging/CLogger.cpp index 1264b122d..0e3c0c5ce 100644 --- a/lib/logging/CLogger.cpp +++ b/lib/logging/CLogger.cpp @@ -75,7 +75,7 @@ CLoggerDomain CLoggerDomain::getParent() const if(isGlobalDomain()) return *this; - const size_t pos = name.find_last_of("."); + const size_t pos = name.find_last_of('.'); if(pos != std::string::npos) return CLoggerDomain(name.substr(0, pos)); return CLoggerDomain(DOMAIN_GLOBAL); @@ -189,7 +189,7 @@ void CLogger::callTargets(const LogRecord & record) const { TLockGuard _(mx); for(const CLogger * logger = this; logger != nullptr; logger = logger->parent) - for(auto & target : logger->targets) + for(const auto & target : logger->targets) target->write(record); } @@ -235,7 +235,8 @@ CLogger * CLogManager::getLogger(const CLoggerDomain & domain) std::vector CLogManager::getRegisteredDomains() const { std::vector domains; - for (auto& pair : loggers) + domains.reserve(loggers.size()); + for(const auto & pair : loggers) { domains.push_back(pair.second->getDomain().getName()); } @@ -247,25 +248,11 @@ CLogFormatter::CLogFormatter() { } -CLogFormatter::CLogFormatter(const std::string & pattern) - : pattern(pattern) +CLogFormatter::CLogFormatter(std::string pattern): + pattern(std::move(pattern)) { } -CLogFormatter::CLogFormatter(const CLogFormatter & c) : pattern(c.pattern) { } -CLogFormatter::CLogFormatter(CLogFormatter && m) : pattern(std::move(m.pattern)) { } - -CLogFormatter & CLogFormatter::operator=(const CLogFormatter & c) -{ - pattern = c.pattern; - return *this; -} -CLogFormatter & CLogFormatter::operator=(CLogFormatter && m) -{ - pattern = std::move(m.pattern); - return *this; -} - std::string CLogFormatter::format(const LogRecord & record) const { std::string message = pattern; @@ -437,8 +424,8 @@ void CLogConsoleTarget::setFormatter(const CLogFormatter & formatter) { this->fo const CColorMapping & CLogConsoleTarget::getColorMapping() const { return colorMapping; } void CLogConsoleTarget::setColorMapping(const CColorMapping & colorMapping) { this->colorMapping = colorMapping; } -CLogFileTarget::CLogFileTarget(boost::filesystem::path filePath, bool append) - : file(std::move(filePath), append ? std::ios_base::app : std::ios_base::out) +CLogFileTarget::CLogFileTarget(const boost::filesystem::path & filePath, bool append): + file(filePath, append ? std::ios_base::app : std::ios_base::out) { // formatter.setPattern("%d %l %n [%t] - %m"); formatter.setPattern("%l %n [%t] - %m"); diff --git a/lib/logging/CLogger.h b/lib/logging/CLogger.h index 6ee9e7b03..71a817d81 100644 --- a/lib/logging/CLogger.h +++ b/lib/logging/CLogger.h @@ -134,13 +134,8 @@ class DLL_LINKAGE CLogFormatter { public: CLogFormatter(); - CLogFormatter(const CLogFormatter & copy); - CLogFormatter(CLogFormatter && move); - CLogFormatter(const std::string & pattern); - - CLogFormatter & operator=(const CLogFormatter & copy); - CLogFormatter & operator=(CLogFormatter && move); + CLogFormatter(std::string pattern); void setPattern(const std::string & pattern); void setPattern(std::string && pattern); @@ -216,7 +211,7 @@ class DLL_LINKAGE CLogFileTarget : public ILogTarget public: /// Constructs a CLogFileTarget and opens the file designated by filePath. If the append parameter is true, the file /// will be appended to. Otherwise the file designated by filePath will be truncated before being opened. - explicit CLogFileTarget(boost::filesystem::path filePath, bool append = true); + explicit CLogFileTarget(const boost::filesystem::path & filePath, bool append = true); ~CLogFileTarget(); const CLogFormatter & getFormatter() const;