// Copyright Epic Games, Inc. All Rights Reserved. #include namespace zen::logging { Logger::Logger(std::string InName, SinkPtr InSink) : m_Name(std::move(InName)) { m_Sinks.push_back(std::move(InSink)); } Logger::Logger(std::string InName, std::vector InSinks) : m_Name(std::move(InName)), m_Sinks(std::move(InSinks)) { } void Logger::Log(LogLevel InLevel, std::string_view Msg) { if (!ShouldLog(InLevel)) { return; } LogMessage LogMsg(m_Name, InLevel, Msg); SinkIt(LogMsg); FlushIfNeeded(InLevel); } void Logger::Log(const SourceLocation& Loc, LogLevel InLevel, std::string_view Msg) { if (!ShouldLog(InLevel)) { return; } LogMessage LogMsg(Loc, m_Name, InLevel, Msg); SinkIt(LogMsg); FlushIfNeeded(InLevel); } void Logger::SinkIt(const LogMessage& Msg) { for (auto& CurrentSink : m_Sinks) { if (CurrentSink->ShouldLog(Msg.Level)) { try { CurrentSink->Log(Msg); } catch (const std::exception&) { // Silently eat errors in sinks } } } } void Logger::FlushIfNeeded(LogLevel InLevel) { if (InLevel >= m_FlushLevel.load(std::memory_order_relaxed)) { Flush(); } } void Logger::Flush() { for (auto& CurrentSink : m_Sinks) { try { CurrentSink->Flush(); } catch (const std::exception&) { } } } void Logger::SetFormatter(std::unique_ptr InFormatter) { for (auto& CurrentSink : m_Sinks) { CurrentSink->SetFormatter(InFormatter->Clone()); } } std::shared_ptr Logger::Clone(std::string NewName) const { auto Cloned = std::make_shared(std::move(NewName), m_Sinks); Cloned->SetLevel(m_Level.load(std::memory_order_relaxed)); Cloned->SetFlushLevel(m_FlushLevel.load(std::memory_order_relaxed)); return Cloned; } } // namespace zen::logging