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
134
135
136
137
138
139
140
141
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
|