1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-24 08:32:34 +02:00

vcmi: modernize lib/logging

This commit is contained in:
Konstantin 2023-02-15 00:35:06 +03:00
parent c8afef3408
commit c142bf1072
4 changed files with 13 additions and 29 deletions

View File

@ -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

View File

@ -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

View File

@ -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<std::string> CLogManager::getRegisteredDomains() const
{
std::vector<std::string> 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");

View File

@ -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;