From f59082d2289b3d8c98fe755fae6b4b5432bcf18e Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Fri, 11 Aug 2023 12:48:40 +0200 Subject: [PATCH] Introduce Logger#object_filter --- lib/base/logger.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ lib/base/logger.hpp | 1 + lib/base/logger.ti | 7 +++++++ 3 files changed, 49 insertions(+) diff --git a/lib/base/logger.cpp b/lib/base/logger.cpp index 38a2c6721b4..b7158fb14e2 100644 --- a/lib/base/logger.cpp +++ b/lib/base/logger.cpp @@ -74,6 +74,47 @@ void Logger::Stop(bool runtimeRemoved) ObjectImpl::Stop(runtimeRemoved); } +void Logger::ValidateObjectFilter(const Lazy& lvalue, const ValidationUtils& utils) +{ + ObjectImpl::ValidateObjectFilter(lvalue, utils); + + auto filter (lvalue()); + + if (filter) { + ObjectLock lock (filter); + + for (auto& kv : filter) { + auto type (Type::GetByName(kv.first)); + + if (!type) { + BOOST_THROW_EXCEPTION( + ValidationError(this, {"object_filter"}, "No such type: '" + kv.first + "'") + ); + } + + if (!dynamic_cast(type.get())) { + BOOST_THROW_EXCEPTION( + ValidationError(this, {"object_filter"}, "Not a config object type: '" + kv.first + "'") + ); + } + + Array::Ptr objects = kv.second; + + if (objects) { + ObjectLock lock (objects); + + for (auto& object : objects) { + if (object.GetType() != ValueString) { + BOOST_THROW_EXCEPTION( + ValidationError(this, {"object_filter", kv.first}, "Must be an array of strings.") + ); + } + } + } + } + } +} + std::set Logger::GetLoggers() { std::unique_lock lock(m_Mutex); diff --git a/lib/base/logger.hpp b/lib/base/logger.hpp index 10e0872ae68..1c0215cc74e 100644 --- a/lib/base/logger.hpp +++ b/lib/base/logger.hpp @@ -92,6 +92,7 @@ class Logger : public ObjectImpl protected: void Start(bool runtimeCreated) override; void Stop(bool runtimeRemoved) override; + void ValidateObjectFilter(const Lazy& lvalue, const ValidationUtils& utils) override; private: static void UpdateMinLogSeverity(); diff --git a/lib/base/logger.ti b/lib/base/logger.ti index 44226cee41a..88544ea39ee 100644 --- a/lib/base/logger.ti +++ b/lib/base/logger.ti @@ -12,6 +12,13 @@ abstract class Logger : ConfigObject [config, virtual] String severity { default {{{ return "information"; }}} }; + [config] Dictionary::Ptr object_filter; +}; + +validator Logger { + Dictionary object_filter { + Array "*"; + }; }; }