Skip to content

Commit

Permalink
Log#~Log(): skip messages based on Logger#m_ObjectFilterCache & Confi…
Browse files Browse the repository at this point in the history
…gObject#m_AllParentsAffectingLogging

If Logger#object_filter is set, but doesn't intersect with
ConfigObject#m_AllParentsAffectingLogging, drop the message.
  • Loading branch information
Al2Klimov committed Aug 15, 2023
1 parent 0cecad6 commit 8c3bfd0
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 5 deletions.
14 changes: 13 additions & 1 deletion lib/base/configobject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -427,10 +427,22 @@ void ConfigObject::OnAllConfigLoaded()

toDo.pop_back();

if (m_AllParentsAffectingLogging.emplace(current.get()).second) {
if (m_AllParentsAffectingLogging.Data.emplace(current.get()).second) {
current->GetParentsAffectingLogging(toDo);
}
} while (!toDo.empty());

m_AllParentsAffectingLogging.Frozen.store(true);
}

const std::set<ConfigObject*>& ConfigObject::GetAllParentsAffectingLogging() const
{
if (m_AllParentsAffectingLogging.Frozen.load(std::memory_order_relaxed)) {
return m_AllParentsAffectingLogging.Data;
}

static const std::set<ConfigObject*> fallback;
return fallback;
}

void ConfigObject::CreateChildObjects(const Type::Ptr& childType)
Expand Down
8 changes: 7 additions & 1 deletion lib/base/configobject.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#define CONFIGOBJECT_H

#include "base/i2-base.hpp"
#include "base/atomic.hpp"
#include "base/configobject-ti.hpp"
#include "base/object.hpp"
#include "base/type.hpp"
Expand Down Expand Up @@ -61,6 +62,7 @@ class ConfigObject : public ObjectImpl<ConfigObject>
virtual void OnStateLoaded();

Dictionary::Ptr GetSourceLocation() const override;
const std::set<ConfigObject*>& GetAllParentsAffectingLogging() const;

template<typename T>
static intrusive_ptr<T> GetObject(const String& name)
Expand All @@ -82,7 +84,11 @@ class ConfigObject : public ObjectImpl<ConfigObject>

private:
ConfigObject::Ptr m_Zone;
std::set<ConfigObject*> m_AllParentsAffectingLogging;

struct {
std::set<ConfigObject*> Data;
Atomic<bool> Frozen {false};
} m_AllParentsAffectingLogging;

static void RestoreObject(const String& message, int attributeTypes);
};
Expand Down
30 changes: 27 additions & 3 deletions lib/base/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
#endif /* _WIN32 */
#include <algorithm>
#include <iostream>
#include <iterator>
#include <utility>
#include <vector>

using namespace icinga;

Expand Down Expand Up @@ -387,11 +389,33 @@ Log::~Log()
for (const Logger::Ptr& logger : Logger::GetLoggers()) {
ObjectLock llock(logger);

if (!logger->IsActive())
if (!logger->IsActive()) {
continue;
}

if (entry.Severity < logger->GetMinSeverity()) {
continue;
}

auto filter (logger->GetObjectFilter());

if (logger->GetObjectFilter()) {
if (!m_Involved) {
continue;
}

auto& allowed (logger->GetObjectFilterCache());
auto& indirect (m_Involved->GetAllParentsAffectingLogging());
std::vector<ConfigObject*> intersection;

std::set_intersection(allowed.begin(), allowed.end(), indirect.begin(), indirect.end(), std::back_inserter(intersection));

if (intersection.empty()) {
continue;
}
}

if (entry.Severity >= logger->GetMinSeverity())
logger->ProcessLogEntry(entry);
logger->ProcessLogEntry(entry);

#ifdef I2_DEBUG /* I2_DEBUG */
/* Always flush, don't depend on the timer. Enable this for development sprints on Linux/macOS only. Windows crashes. */
Expand Down
5 changes: 5 additions & 0 deletions lib/base/logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ class Logger : public ObjectImpl<Logger>
void SetObjectFilter(const Dictionary::Ptr& value, bool suppress_events = false, const Value& cookie = Empty) override;
void OnAllConfigLoaded() override;

inline const std::vector<ConfigObject*>& GetObjectFilterCache() const
{
return m_ObjectFilterCache;
}

protected:
void Start(bool runtimeCreated) override;
void Stop(bool runtimeRemoved) override;
Expand Down

0 comments on commit 8c3bfd0

Please sign in to comment.