Fix Logger Severity String Parse

parsed severity string is cached as an optional variable. so parsing would be called only once for each logger.

refs: #9534
This commit is contained in:
sreyhani 2023-01-15 14:56:04 +03:30
parent 83dba2f93b
commit 3c0d01088e
2 changed files with 22 additions and 8 deletions

View file

@ -85,19 +85,28 @@ std::set<Logger::Ptr> Logger::GetLoggers()
*
* @returns The minimum severity.
*/
LogSeverity Logger::GetMinSeverity() const
LogSeverity Logger::GetMinSeverity()
{
if (min_severity == boost::none) {
CacheMinSeverity();
}
return *min_severity;
}
/**
* Retrieves and caches the minimum severity for this logger.
*/
void Logger::CacheMinSeverity()
{
String severity = GetSeverity();
if (severity.IsEmpty())
return LogInformation;
min_severity.emplace(LogInformation);
else {
LogSeverity ls = LogInformation;
try {
ls = Logger::StringToSeverity(severity);
} catch (const std::exception&) { /* use the default level */ }
return ls;
} catch (const std::exception &) { /* use the default level */ }
min_severity.emplace(ls);
}
}
@ -202,7 +211,7 @@ bool Logger::IsTimestampEnabled()
void Logger::SetSeverity(const String& value, bool suppress_events, const Value& cookie)
{
ObjectImpl<Logger>::SetSeverity(value, suppress_events, cookie);
min_severity.emplace(StringToSeverity(value));
UpdateMinLogSeverity();
}

View file

@ -8,6 +8,7 @@
#include "base/logger-ti.hpp"
#include <set>
#include <sstream>
#include <boost/optional.hpp>
namespace icinga
{
@ -54,7 +55,7 @@ public:
static String SeverityToString(LogSeverity severity);
static LogSeverity StringToSeverity(const String& severity);
LogSeverity GetMinSeverity() const;
LogSeverity GetMinSeverity();
/**
* Processes the log entry and writes it to the log that is
@ -104,6 +105,10 @@ private:
static LogSeverity m_ConsoleLogSeverity;
static std::mutex m_UpdateMinLogSeverityMutex;
static Atomic<LogSeverity> m_MinLogSeverity;
void CacheMinSeverity();
boost::optional<LogSeverity> min_severity;
};
class Log