aboutsummaryrefslogtreecommitdiff
path: root/src/zencore
diff options
context:
space:
mode:
authorStefan Boberg <[email protected]>2026-03-23 12:53:58 +0100
committerGitHub Enterprise <[email protected]>2026-03-23 12:53:58 +0100
commite12feda1272922f6ad3567dd27509f0ec53d3a7b (patch)
tree8d238687c643a6790f9f307db3008a0e400caf42 /src/zencore
parentDocumentation updates (#882) (diff)
downloadzen-e12feda1272922f6ad3567dd27509f0ec53d3a7b.tar.xz
zen-e12feda1272922f6ad3567dd27509f0ec53d3a7b.zip
Logger simplification (#883)
- **`Logger` now holds a single `SinkPtr`** instead of a `std::vector<SinkPtr>`. The `SetSinks`/`AddSink` API is replaced with a single `SetSink`. This removes complexity from `Logger` itself and makes `Clone()` cheaper (no vector copy). - **New `BroadcastSink`** (`zencore/logging/broadcastsink.h`) acts as a thread-safe, shared indirection point that fans out to a dynamic list of child sinks. Adding or removing a child sink via `AddSink`/`RemoveSink` is immediately visible to every `Logger` that holds a reference to it — including cloned loggers — without requiring each logger to be updated individually. - **`GetDefaultBroadcastSink()`** (exposed from `zenutil/logging.h`) gives server-layer code access to the shared broadcast sink so it can register optional sinks (OTel, TCP log stream) after logging is initialized, without going through `Default()->AddSink()`. ### Motivation Previously, dynamically adding sinks post-initialization mutated the default logger's internal sink vector directly. This was fragile: cloned loggers (created before `AddSink` was called) would not pick up the new sinks. `BroadcastSink` fixes this by making the sink list a shared, mutable object that all loggers sharing the same broadcast instance observe uniformly.
Diffstat (limited to 'src/zencore')
-rw-r--r--src/zencore/include/zencore/logging/broadcastsink.h86
-rw-r--r--src/zencore/include/zencore/logging/logger.h4
-rw-r--r--src/zencore/include/zencore/logging/logmsg.h12
-rw-r--r--src/zencore/logging.cpp4
-rw-r--r--src/zencore/logging/logger.cpp54
-rw-r--r--src/zencore/sentryintegration.cpp2
6 files changed, 111 insertions, 51 deletions
diff --git a/src/zencore/include/zencore/logging/broadcastsink.h b/src/zencore/include/zencore/logging/broadcastsink.h
new file mode 100644
index 000000000..c2709d87c
--- /dev/null
+++ b/src/zencore/include/zencore/logging/broadcastsink.h
@@ -0,0 +1,86 @@
+// Copyright Epic Games, Inc. All Rights Reserved.
+
+#pragma once
+
+#include <zencore/logging/sink.h>
+#include <zencore/thread.h>
+
+#include <algorithm>
+#include <vector>
+
+namespace zen::logging {
+
+/// A sink that broadcasts log messages to a dynamic list of child sinks.
+///
+/// BroadcastSink acts as a shared indirection point: multiple Loggers can
+/// reference the same BroadcastSink instance, and adding or removing a child
+/// sink is immediately visible to all of them. This is the recommended way
+/// to manage "default" sinks that should be active on most loggers.
+///
+/// Each child sink owns its own Formatter — BroadcastSink::SetFormatter() is
+/// intentionally a no-op so that per-sink formatting is not accidentally
+/// overwritten by registry-wide formatter changes.
+class BroadcastSink : public Sink
+{
+public:
+ BroadcastSink() = default;
+ explicit BroadcastSink(std::vector<SinkPtr> InSinks) : m_Sinks(std::move(InSinks)) {}
+
+ void Log(const LogMessage& Msg) override
+ {
+ RwLock::SharedLockScope Lock(m_Lock);
+ for (auto& Child : m_Sinks)
+ {
+ if (Child->ShouldLog(Msg.GetLevel()))
+ {
+ try
+ {
+ Child->Log(Msg);
+ }
+ catch (const std::exception&)
+ {
+ // logging here would be sketchy since we could cause a recursive loop
+ // if we wanted to surface this error, we would need to have some sort
+ // of internal error handling mechanism that doesn't rely on sinks
+ }
+ }
+ }
+ }
+
+ void Flush() override
+ {
+ RwLock::SharedLockScope Lock(m_Lock);
+ for (auto& Child : m_Sinks)
+ {
+ try
+ {
+ Child->Flush();
+ }
+ catch (const std::exception&)
+ {
+ // must not log here (see above)
+ }
+ }
+ }
+
+ /// No-op — child sinks manage their own formatters.
+ void SetFormatter(std::unique_ptr<Formatter> /*InFormatter*/) override {}
+
+ void AddSink(SinkPtr InSink)
+ {
+ RwLock::ExclusiveLockScope Lock(m_Lock);
+ m_Sinks.push_back(std::move(InSink));
+ }
+
+ void RemoveSink(const SinkPtr& InSink)
+ {
+ RwLock::ExclusiveLockScope Lock(m_Lock);
+ m_Sinks.erase(std::remove(m_Sinks.begin(), m_Sinks.end(), InSink), m_Sinks.end());
+ }
+
+private:
+ RwLock m_Lock;
+ std::vector<SinkPtr> m_Sinks;
+};
+
+} // namespace zen::logging
diff --git a/src/zencore/include/zencore/logging/logger.h b/src/zencore/include/zencore/logging/logger.h
index c94bc58fa..1706a4455 100644
--- a/src/zencore/include/zencore/logging/logger.h
+++ b/src/zencore/include/zencore/logging/logger.h
@@ -21,7 +21,6 @@ class Logger final : public LoggerBase
{
public:
Logger(std::string_view InName, SinkPtr InSink);
- Logger(std::string_view InName, std::span<const SinkPtr> InSinks);
~Logger();
Logger(const Logger&) = delete;
@@ -31,8 +30,7 @@ public:
std::string_view Name() const;
- void SetSinks(std::vector<SinkPtr> InSinks);
- void AddSink(SinkPtr InSink);
+ void SetSink(SinkPtr InSink);
void SetFormatter(std::unique_ptr<Formatter> InFormatter);
diff --git a/src/zencore/include/zencore/logging/logmsg.h b/src/zencore/include/zencore/logging/logmsg.h
index a1acb503b..4a777c71e 100644
--- a/src/zencore/include/zencore/logging/logmsg.h
+++ b/src/zencore/include/zencore/logging/logmsg.h
@@ -41,22 +41,14 @@ struct LogMessage
void SetSource(const SourceLocation& InSource) { m_Source = InSource; }
private:
- static constexpr LogPoint s_DefaultPoints[LogLevelCount] = {
- {{}, Trace, {}},
- {{}, Debug, {}},
- {{}, Info, {}},
- {{}, Warn, {}},
- {{}, Err, {}},
- {{}, Critical, {}},
- {{}, Off, {}},
- };
+ static constexpr LogPoint s_DefaultPoint{{}, Off, {}};
std::string_view m_LoggerName;
LogLevel m_Level = Off;
std::chrono::system_clock::time_point m_Time;
SourceLocation m_Source;
std::string_view m_Payload;
- const LogPoint* m_Point = &s_DefaultPoints[Off];
+ const LogPoint* m_Point = &s_DefaultPoint;
int m_ThreadId = 0;
};
diff --git a/src/zencore/logging.cpp b/src/zencore/logging.cpp
index 3206e380b..828bea6ed 100644
--- a/src/zencore/logging.cpp
+++ b/src/zencore/logging.cpp
@@ -345,7 +345,7 @@ SuppressConsoleLog()
}
SinkPtr NullSinkPtr(new NullSink());
- ConLogger = Ref<Logger>(new Logger("console", std::vector<SinkPtr>{NullSinkPtr}));
+ ConLogger = Ref<Logger>(new Logger("console", NullSinkPtr));
Registry::Instance().Register(ConLogger);
}
@@ -391,7 +391,7 @@ ConsoleLog()
{
SinkPtr ConsoleSink(new AnsiColorStdoutSink());
ConsoleSink->SetFormatter(std::make_unique<ConsoleFormatter>());
- ConLogger = Ref<Logger>(new Logger("console", std::vector<SinkPtr>{ConsoleSink}));
+ ConLogger = Ref<Logger>(new Logger("console", ConsoleSink));
Registry::Instance().Register(ConLogger);
}
});
diff --git a/src/zencore/logging/logger.cpp b/src/zencore/logging/logger.cpp
index dd1675bb1..ff1db3edc 100644
--- a/src/zencore/logging/logger.cpp
+++ b/src/zencore/logging/logger.cpp
@@ -4,27 +4,20 @@
#include <zencore/thread.h>
#include <string>
-#include <vector>
namespace zen::logging {
struct Logger::Impl
{
- std::string m_Name;
- std::vector<SinkPtr> m_Sinks;
- ErrorHandler* m_ErrorHandler = nullptr;
+ std::string m_Name;
+ SinkPtr m_Sink;
+ ErrorHandler* m_ErrorHandler = nullptr;
};
Logger::Logger(std::string_view InName, SinkPtr InSink) : m_Impl(std::make_unique<Impl>())
{
m_Impl->m_Name = InName;
- m_Impl->m_Sinks.push_back(std::move(InSink));
-}
-
-Logger::Logger(std::string_view InName, std::span<const SinkPtr> InSinks) : m_Impl(std::make_unique<Impl>())
-{
- m_Impl->m_Name = InName;
- m_Impl->m_Sinks.assign(InSinks.begin(), InSinks.end());
+ m_Impl->m_Sink = std::move(InSink);
}
Logger::~Logger() = default;
@@ -49,20 +42,17 @@ Logger::Log(const LogPoint& Point, fmt::format_args Args)
void
Logger::SinkIt(const LogMessage& Msg)
{
- for (auto& CurrentSink : m_Impl->m_Sinks)
+ if (m_Impl->m_Sink && m_Impl->m_Sink->ShouldLog(Msg.GetLevel()))
{
- if (CurrentSink->ShouldLog(Msg.GetLevel()))
+ try
{
- try
- {
- CurrentSink->Log(Msg);
- }
- catch (const std::exception& Ex)
+ m_Impl->m_Sink->Log(Msg);
+ }
+ catch (const std::exception& Ex)
+ {
+ if (m_Impl->m_ErrorHandler)
{
- if (m_Impl->m_ErrorHandler)
- {
- m_Impl->m_ErrorHandler->HandleError(Ex.what());
- }
+ m_Impl->m_ErrorHandler->HandleError(Ex.what());
}
}
}
@@ -80,11 +70,11 @@ Logger::FlushIfNeeded(LogLevel InLevel)
void
Logger::Flush()
{
- for (auto& CurrentSink : m_Impl->m_Sinks)
+ if (m_Impl->m_Sink)
{
try
{
- CurrentSink->Flush();
+ m_Impl->m_Sink->Flush();
}
catch (const std::exception& Ex)
{
@@ -97,15 +87,9 @@ Logger::Flush()
}
void
-Logger::SetSinks(std::vector<SinkPtr> InSinks)
-{
- m_Impl->m_Sinks = std::move(InSinks);
-}
-
-void
-Logger::AddSink(SinkPtr InSink)
+Logger::SetSink(SinkPtr InSink)
{
- m_Impl->m_Sinks.push_back(std::move(InSink));
+ m_Impl->m_Sink = std::move(InSink);
}
void
@@ -117,9 +101,9 @@ Logger::SetErrorHandler(ErrorHandler* Handler)
void
Logger::SetFormatter(std::unique_ptr<Formatter> InFormatter)
{
- for (auto& CurrentSink : m_Impl->m_Sinks)
+ if (m_Impl->m_Sink)
{
- CurrentSink->SetFormatter(InFormatter->Clone());
+ m_Impl->m_Sink->SetFormatter(std::move(InFormatter));
}
}
@@ -132,7 +116,7 @@ Logger::Name() const
Ref<Logger>
Logger::Clone(std::string_view NewName) const
{
- Ref<Logger> Cloned(new Logger(NewName, m_Impl->m_Sinks));
+ Ref<Logger> Cloned(new Logger(NewName, m_Impl->m_Sink));
Cloned->SetLevel(m_Level.load(std::memory_order_relaxed));
Cloned->SetFlushLevel(m_FlushLevel.load(std::memory_order_relaxed));
Cloned->SetErrorHandler(m_Impl->m_ErrorHandler);
diff --git a/src/zencore/sentryintegration.cpp b/src/zencore/sentryintegration.cpp
index c9c843dd6..8491bef64 100644
--- a/src/zencore/sentryintegration.cpp
+++ b/src/zencore/sentryintegration.cpp
@@ -341,7 +341,7 @@ SentryIntegration::Initialize(const Config& Conf, const std::string& CommandLine
sentry_set_user(SentryUserObject);
logging::SinkPtr SentrySink(new sentry::SentrySink());
- m_SentryLogger = Ref<logging::Logger>(new logging::Logger("sentry", std::vector<logging::SinkPtr>{SentrySink}));
+ m_SentryLogger = Ref<logging::Logger>(new logging::Logger("sentry", SentrySink));
logging::Registry::Instance().Register(m_SentryLogger);
logging::SetErrorLog("sentry");