Skip to content

Fix Logger Severity String Parse #9629

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions lib/base/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... in case of an exception?

}
}

Expand Down Expand Up @@ -202,7 +211,7 @@ bool Logger::IsTimestampEnabled()
void Logger::SetSeverity(const String& value, bool suppress_events, const Value& cookie)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add function description as well ?

{
ObjectImpl<Logger>::SetSeverity(value, suppress_events, cookie);

min_severity.emplace(StringToSeverity(value));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens...

UpdateMinLogSeverity();
}

Expand Down
7 changes: 6 additions & 1 deletion lib/base/logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "base/logger-ti.hpp"
#include <set>
#include <sstream>
#include <boost/optional.hpp>

namespace icinga
{
Expand Down Expand Up @@ -54,7 +55,7 @@ class Logger : public ObjectImpl<Logger>
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
Expand Down Expand Up @@ -104,6 +105,10 @@ class Logger : public ObjectImpl<Logger>
static LogSeverity m_ConsoleLogSeverity;
static std::mutex m_UpdateMinLogSeverityMutex;
static Atomic<LogSeverity> m_MinLogSeverity;

void CacheMinSeverity();

boost::optional<LogSeverity> min_severity;
};

class Log
Expand Down