aboutsummaryrefslogtreecommitdiff
path: root/src/zencore/logging/logger.cpp
diff options
context:
space:
mode:
authorLiam Mitchell <[email protected]>2026-03-09 19:06:36 -0700
committerLiam Mitchell <[email protected]>2026-03-09 19:06:36 -0700
commitd1abc50ee9d4fb72efc646e17decafea741caa34 (patch)
treee4288e00f2f7ca0391b83d986efcb69d3ba66a83 /src/zencore/logging/logger.cpp
parentAllow requests with invalid content-types unless specified in command line or... (diff)
parentupdated chunk–block analyser (#818) (diff)
downloadzen-d1abc50ee9d4fb72efc646e17decafea741caa34.tar.xz
zen-d1abc50ee9d4fb72efc646e17decafea741caa34.zip
Merge branch 'main' into lm/restrict-content-type
Diffstat (limited to 'src/zencore/logging/logger.cpp')
-rw-r--r--src/zencore/logging/logger.cpp142
1 files changed, 142 insertions, 0 deletions
diff --git a/src/zencore/logging/logger.cpp b/src/zencore/logging/logger.cpp
new file mode 100644
index 000000000..dd1675bb1
--- /dev/null
+++ b/src/zencore/logging/logger.cpp
@@ -0,0 +1,142 @@
+// Copyright Epic Games, Inc. All Rights Reserved.
+
+#include <zencore/logging/logger.h>
+#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;
+};
+
+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());
+}
+
+Logger::~Logger() = default;
+
+void
+Logger::Log(const LogPoint& Point, fmt::format_args Args)
+{
+ if (!ShouldLog(Point.Level))
+ {
+ return;
+ }
+
+ fmt::basic_memory_buffer<char, 250> Buffer;
+ fmt::vformat_to(fmt::appender(Buffer), Point.FormatString, Args);
+
+ LogMessage LogMsg(Point, m_Impl->m_Name, std::string_view(Buffer.data(), Buffer.size()));
+ LogMsg.SetThreadId(GetCurrentThreadId());
+ SinkIt(LogMsg);
+ FlushIfNeeded(Point.Level);
+}
+
+void
+Logger::SinkIt(const LogMessage& Msg)
+{
+ for (auto& CurrentSink : m_Impl->m_Sinks)
+ {
+ if (CurrentSink->ShouldLog(Msg.GetLevel()))
+ {
+ try
+ {
+ CurrentSink->Log(Msg);
+ }
+ catch (const std::exception& Ex)
+ {
+ if (m_Impl->m_ErrorHandler)
+ {
+ m_Impl->m_ErrorHandler->HandleError(Ex.what());
+ }
+ }
+ }
+ }
+}
+
+void
+Logger::FlushIfNeeded(LogLevel InLevel)
+{
+ if (InLevel >= m_FlushLevel.load(std::memory_order_relaxed))
+ {
+ Flush();
+ }
+}
+
+void
+Logger::Flush()
+{
+ for (auto& CurrentSink : m_Impl->m_Sinks)
+ {
+ try
+ {
+ CurrentSink->Flush();
+ }
+ catch (const std::exception& Ex)
+ {
+ if (m_Impl->m_ErrorHandler)
+ {
+ m_Impl->m_ErrorHandler->HandleError(Ex.what());
+ }
+ }
+ }
+}
+
+void
+Logger::SetSinks(std::vector<SinkPtr> InSinks)
+{
+ m_Impl->m_Sinks = std::move(InSinks);
+}
+
+void
+Logger::AddSink(SinkPtr InSink)
+{
+ m_Impl->m_Sinks.push_back(std::move(InSink));
+}
+
+void
+Logger::SetErrorHandler(ErrorHandler* Handler)
+{
+ m_Impl->m_ErrorHandler = Handler;
+}
+
+void
+Logger::SetFormatter(std::unique_ptr<Formatter> InFormatter)
+{
+ for (auto& CurrentSink : m_Impl->m_Sinks)
+ {
+ CurrentSink->SetFormatter(InFormatter->Clone());
+ }
+}
+
+std::string_view
+Logger::Name() const
+{
+ return m_Impl->m_Name;
+}
+
+Ref<Logger>
+Logger::Clone(std::string_view NewName) const
+{
+ Ref<Logger> Cloned(new Logger(NewName, m_Impl->m_Sinks));
+ Cloned->SetLevel(m_Level.load(std::memory_order_relaxed));
+ Cloned->SetFlushLevel(m_FlushLevel.load(std::memory_order_relaxed));
+ Cloned->SetErrorHandler(m_Impl->m_ErrorHandler);
+ return Cloned;
+}
+
+} // namespace zen::logging