feat(Log): harden LogState facade, bounded Logger delivery/tickets/quarantine, file sync/reopen, redaction, and separate LogConfig (M-LOG-01..05)
This commit is contained in:
1 parent
18a623aa62
commit
a74a402938
23 files changed
+2656
-1197
No files matched your search
+79
-13
@@ -9,12 +9,17 @@
|
||||
|
||||
namespace Void::Logging
|
||||
{
|
||||
/// Discards everything. Useful as a placeholder or for benchmarks.
|
||||
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(); }
|
||||
|
||||
@@ -23,8 +28,6 @@ namespace Void::Logging
|
||||
void write(const Entry&) override {}
|
||||
};
|
||||
|
||||
/// Forwards every entry to a user callback. The simplest way to splice
|
||||
/// an external logging library into the Logger framework.
|
||||
class CallbackLogger final : public Logger
|
||||
{
|
||||
public:
|
||||
@@ -41,6 +44,11 @@ namespace Void::Logging
|
||||
{
|
||||
}
|
||||
|
||||
explicit CallbackLogger(Callback callback, LoggerOptions options)
|
||||
: Logger(std::move(options)), m_callback(std::move(callback))
|
||||
{
|
||||
}
|
||||
|
||||
~CallbackLogger() noexcept override { flush(); }
|
||||
|
||||
protected:
|
||||
@@ -57,10 +65,6 @@ namespace Void::Logging
|
||||
Callback m_callback;
|
||||
};
|
||||
|
||||
/// Tee: relays every entry to each child logger. Children keep their
|
||||
/// own modes, levels, and formatters. The MultiLogger itself defaults
|
||||
/// to Immediate dispatch — the children already decide whether they
|
||||
/// queue asynchronously, so double-queueing only adds latency.
|
||||
class MultiLogger final : public Logger
|
||||
{
|
||||
public:
|
||||
@@ -72,15 +76,41 @@ namespace Void::Logging
|
||||
{
|
||||
}
|
||||
|
||||
explicit MultiLogger(
|
||||
std::vector<std::shared_ptr<Logger>> children,
|
||||
LoggerOptions options)
|
||||
: Logger(std::move(options)), m_children(std::move(children))
|
||||
{
|
||||
}
|
||||
|
||||
~MultiLogger() noexcept override { flush(); }
|
||||
|
||||
/// Add a child sink. Not thread-safe with concurrent logging —
|
||||
/// assemble the tree before use.
|
||||
void add(std::shared_ptr<Logger> child)
|
||||
{
|
||||
if (child) {
|
||||
m_children.push_back(std::move(child));
|
||||
(void)try_add(std::move(child));
|
||||
}
|
||||
|
||||
[[nodiscard]] Void::Result::Result<void>
|
||||
try_add(std::shared_ptr<Logger> 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
|
||||
@@ -88,7 +118,6 @@ namespace Void::Logging
|
||||
return m_children.size();
|
||||
}
|
||||
|
||||
/// Flush own pending entries, then every child.
|
||||
void flush() override
|
||||
{
|
||||
Logger::flush();
|
||||
@@ -97,6 +126,26 @@ namespace Void::Logging
|
||||
}
|
||||
}
|
||||
|
||||
[[nodiscard]] Void::Result::Result<void> 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<std::int32_t>(
|
||||
LogErrorCode::unsupported_capability)) {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
return any_synced ? Void::Result::Result<void>{}
|
||||
: Logger::sync_files();
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
void write(const Entry& entry) override
|
||||
@@ -108,7 +157,24 @@ namespace Void::Logging
|
||||
|
||||
private:
|
||||
|
||||
[[nodiscard]] bool reaches_self(const Logger& candidate) const
|
||||
{
|
||||
if (&candidate == this) {
|
||||
return true;
|
||||
}
|
||||
const auto* nested = dynamic_cast<const MultiLogger*>(&candidate);
|
||||
if (nested == nullptr) {
|
||||
return false;
|
||||
}
|
||||
for (const auto& grandchild : nested->m_children) {
|
||||
if (grandchild && reaches_self(*grandchild)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
std::vector<std::shared_ptr<Logger>> m_children;
|
||||
};
|
||||
|
||||
} // namespace Void::Logging
|
||||
} // namespace Void::Logging
|
||||
Reference in new issue
Block a user