1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
// Copyright Epic Games, Inc. All Rights Reserved.
#include <zencore/logging/logger.h>
#include <zencore/logging/tracelog.h>
#include <zencore/thread.h>
#include <string>
namespace zen::logging {
struct Logger::Impl
{
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_Sink = std::move(InSink);
}
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);
#if ZEN_WITH_TRACE
// Forward the typed argument pack to the trace system while we still have
// it - sinks are invoked below with the already-rendered payload.
TraceLogTyped(*this, Point, Args);
#endif
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)
{
if (m_Impl->m_Sink && m_Impl->m_Sink->ShouldLog(Msg.GetLevel()))
{
try
{
m_Impl->m_Sink->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()
{
if (m_Impl->m_Sink)
{
try
{
m_Impl->m_Sink->Flush();
}
catch (const std::exception& Ex)
{
if (m_Impl->m_ErrorHandler)
{
m_Impl->m_ErrorHandler->HandleError(Ex.what());
}
}
}
}
void
Logger::SetSink(SinkPtr InSink)
{
m_Impl->m_Sink = std::move(InSink);
}
void
Logger::SetErrorHandler(ErrorHandler* Handler)
{
m_Impl->m_ErrorHandler = Handler;
}
void
Logger::SetFormatter(std::unique_ptr<Formatter> InFormatter)
{
if (m_Impl->m_Sink)
{
m_Impl->m_Sink->SetFormatter(std::move(InFormatter));
}
}
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_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);
return Cloned;
}
} // namespace zen::logging
|