From 3c0d01088e8a73443a61ab80b2b8f9f53bcedab5 Mon Sep 17 00:00:00 2001 From: sreyhani Date: Sun, 15 Jan 2023 14:56:04 +0330 Subject: [PATCH] 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 --- lib/base/logger.cpp | 23 ++++++++++++++++------- lib/base/logger.hpp | 7 ++++++- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/lib/base/logger.cpp b/lib/base/logger.cpp index 0be7279b2..714fc7799 100644 --- a/lib/base/logger.cpp +++ b/lib/base/logger.cpp @@ -85,19 +85,28 @@ std::set 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::SetSeverity(value, suppress_events, cookie); - + min_severity.emplace(StringToSeverity(value)); UpdateMinLogSeverity(); } diff --git a/lib/base/logger.hpp b/lib/base/logger.hpp index 10e0872ae..862b3dec1 100644 --- a/lib/base/logger.hpp +++ b/lib/base/logger.hpp @@ -8,6 +8,7 @@ #include "base/logger-ti.hpp" #include #include +#include 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 m_MinLogSeverity; + + void CacheMinSeverity(); + + boost::optional min_severity; }; class Log