#pragma once #include #include #include #include #include namespace Void::Logging { inline constexpr std::size_t kDefaultMaxFanoutChildren = 8; class NullLogger final : public Logger { public: explicit NullLogger(Mode mode = Mode::Immediate) : Logger(mode) {} explicit NullLogger(LoggerOptions options) : Logger(std::move(options)) { } ~NullLogger() noexcept override { flush(); } protected: void write(const Entry&) override {} }; class CallbackLogger final : public Logger { public: using Callback = std::function; explicit CallbackLogger( Callback callback, Mode mode = Mode::Async, std::size_t capacity = 512, OverflowPolicy policy = OverflowPolicy::Block) : Logger(mode, capacity, policy), m_callback(std::move(callback)) { } explicit CallbackLogger(Callback callback, LoggerOptions options) : Logger(std::move(options)), m_callback(std::move(callback)) { } ~CallbackLogger() noexcept override { flush(); } protected: void write(const Entry& entry) override { if (m_callback) { m_callback(entry); } } private: Callback m_callback; }; class MultiLogger final : public Logger { public: explicit MultiLogger( std::vector> children = {}, Mode mode = Mode::Immediate) : Logger(mode), m_children(std::move(children)) { } explicit MultiLogger( std::vector> children, LoggerOptions options) : Logger(std::move(options)), m_children(std::move(children)) { } ~MultiLogger() noexcept override { flush(); } void add(std::shared_ptr child) { (void)try_add(std::move(child)); } [[nodiscard]] Void::Result::Result try_add(std::shared_ptr child, std::size_t max_children = kDefaultMaxFanoutChildren) { if (!child) { return Void::Result::fail(make_log_error( LogErrorCode::invalid_argument, "MultiLogger::try_add: null child")); } if (m_children.size() >= max_children) { return Void::Result::fail(make_log_error( LogErrorCode::queue_full, "MultiLogger child fan-out limit exceeded")); } if (reaches_self(*child)) { return Void::Result::fail(make_log_error( LogErrorCode::cycle_detected, "MultiLogger fan-out cycle detected")); } m_children.push_back(std::move(child)); return {}; } [[nodiscard]] std::size_t child_count() const noexcept { return m_children.size(); } void flush() override { Logger::flush(); for (const auto& child : m_children) { child->flush(); } } [[nodiscard]] Void::Result::Result sync_files() override { if (m_children.empty()) { return Logger::sync_files(); } bool any_synced = false; for (const auto& child : m_children) { auto res = child->sync_files(); if (res) { any_synced = true; } else if (res.error().code().value != static_cast( LogErrorCode::unsupported_capability)) { return res; } } return any_synced ? Void::Result::Result{} : Logger::sync_files(); } protected: void write(const Entry& entry) override { for (const auto& child : m_children) { child->relay(entry); } } private: [[nodiscard]] bool reaches_self(const Logger& candidate) const { if (&candidate == this) { return true; } const auto* nested = dynamic_cast(&candidate); if (nested == nullptr) { return false; } for (const auto& grandchild : nested->m_children) { if (grandchild && reaches_self(*grandchild)) { return true; } } return false; } std::vector> m_children; }; } // namespace Void::Logging