Skip to content

Commit

Permalink
Cache actual Logger#object_filter objects in Logger#m_ObjectFilterCache
Browse files Browse the repository at this point in the history
  • Loading branch information
Al2Klimov committed Aug 15, 2023
1 parent 9693663 commit 27413d5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
44 changes: 30 additions & 14 deletions lib/base/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ void Logger::Start(bool runtimeCreated)
void Logger::SetObjectFilter(const Dictionary::Ptr& value, bool suppress_events, const Value& cookie)
{
ObjectImpl<Logger>::SetObjectFilter(value, suppress_events, cookie);
CheckObjectFilter();
UpdateCheckObjectFilterCache();
}

void Logger::OnAllConfigLoaded()
{
ObjectImpl<Logger>::OnAllConfigLoaded();
m_CalledOnAllConfigLoaded.store(true);
CheckObjectFilter();
UpdateCheckObjectFilterCache();
}

void Logger::Stop(bool runtimeRemoved)
Expand Down Expand Up @@ -297,7 +297,7 @@ void Logger::UpdateMinLogSeverity()
m_MinLogSeverity.store(result);
}

void Logger::CheckObjectFilter()
void Logger::UpdateCheckObjectFilterCache()
{
if (!m_CalledOnAllConfigLoaded.load()) {
return;
Expand All @@ -306,27 +306,43 @@ void Logger::CheckObjectFilter()
auto filter (GetObjectFilter());

if (!filter) {
ObjectLock lock (this);
m_ObjectFilterCache.clear();
return;
}

ObjectLock lock (filter);
std::vector<ConfigObject*> allObjects;

for (auto& kv : filter) {
auto type (Type::GetByName(kv.first));
auto ctype (dynamic_cast<ConfigType*>(type.get()));
Array::Ptr objects = kv.second;
{
ObjectLock lock (filter);

for (auto& kv : filter) {
auto type (Type::GetByName(kv.first));
auto ctype (dynamic_cast<ConfigType*>(type.get()));
Array::Ptr objects = kv.second;

if (ctype && objects) {
ObjectLock lock (objects);
if (ctype && objects) {
ObjectLock lock (objects);

for (String name : objects) {
auto object (ctype->GetObject(name));

for (String object : objects) {
if (!ctype->GetObject(object)) {
Log(LogWarning, GetReflectionType()->GetName())
<< "Missing " << kv.first << " '" << object << "' in object filter of '" << GetName() << "'.";
if (object) {
allObjects.emplace_back(object.get());
} else {
Log(LogWarning, GetReflectionType()->GetName())
<< "Missing " << kv.first << " '" << name << "' in name filter of '" << GetName() << "'.";
}
}
}
}
}

std::sort(allObjects.begin(), allObjects.end());

ObjectLock lock (this);

m_ObjectFilterCache.swap(allObjects);
}

Log::Log(LogSeverity severity, String facility, const String& message)
Expand Down
4 changes: 3 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 <vector>

namespace icinga
{
Expand Down Expand Up @@ -99,7 +100,7 @@ class Logger : public ObjectImpl<Logger>
private:
static void UpdateMinLogSeverity();

void CheckObjectFilter();
void UpdateCheckObjectFilterCache();

static std::mutex m_Mutex;
static std::set<Logger::Ptr> m_Loggers;
Expand All @@ -111,6 +112,7 @@ class Logger : public ObjectImpl<Logger>
static Atomic<LogSeverity> m_MinLogSeverity;

Atomic<bool> m_CalledOnAllConfigLoaded {false};
std::vector<ConfigObject*> m_ObjectFilterCache;
};

class Log
Expand Down

0 comments on commit 27413d5

Please sign in to comment.