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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
|
// Copyright Epic Games, Inc. All Rights Reserved.
#include <zenutil/logging/fullformatter.h>
#include <zencore/intmath.h>
#include <zencore/logging/helpers.h>
#include <zencore/logging/memorybuffer.h>
#include <zencore/memory/llm.h>
#include <zencore/thread.h>
#include <zencore/zencore.h>
#include <atomic>
#include <chrono>
#include <string>
namespace zen::logging {
struct FullFormatter::Impl
{
Impl(std::string_view LogId, std::chrono::time_point<std::chrono::system_clock> Epoch)
: m_Epoch(Epoch)
, m_LogId(LogId)
, m_LinePrefix(128, ' ')
, m_UseFullDate(false)
{
}
explicit Impl(std::string_view LogId) : m_LogId(LogId), m_LinePrefix(128, ' '), m_UseFullDate(true) {}
std::chrono::time_point<std::chrono::system_clock> m_Epoch;
std::tm m_CachedLocalTm{};
std::chrono::seconds m_LastLogSecs{std::chrono::seconds(87654321)};
std::atomic<std::chrono::seconds> m_CacheTimestamp{std::chrono::seconds(87654321)};
MemoryBuffer m_CachedDatetime;
std::string m_LogId;
std::string m_LinePrefix;
bool m_UseFullDate = true;
RwLock m_TimestampLock;
};
FullFormatter::FullFormatter(std::string_view LogId, std::chrono::time_point<std::chrono::system_clock> Epoch)
: m_Impl(std::make_unique<Impl>(LogId, Epoch))
{
}
FullFormatter::FullFormatter(std::string_view LogId) : m_Impl(std::make_unique<Impl>(LogId))
{
}
FullFormatter::~FullFormatter() = default;
std::unique_ptr<Formatter>
FullFormatter::Clone() const
{
ZEN_MEMSCOPE(ELLMTag::Logging);
std::unique_ptr<FullFormatter> Copy;
if (m_Impl->m_UseFullDate)
{
Copy = std::make_unique<FullFormatter>(m_Impl->m_LogId);
}
else
{
Copy = std::make_unique<FullFormatter>(m_Impl->m_LogId, m_Impl->m_Epoch);
}
Copy->SetColorEnabled(IsColorEnabled());
return Copy;
}
void
FullFormatter::Format(const LogMessage& Msg, MemoryBuffer& OutBuffer)
{
ZEN_MEMSCOPE(ELLMTag::Logging);
// Note that the sink is responsible for ensuring there is only ever a
// single caller in here
using namespace std::literals;
std::chrono::seconds TimestampSeconds;
std::chrono::milliseconds Millis;
if (m_Impl->m_UseFullDate)
{
TimestampSeconds = std::chrono::duration_cast<std::chrono::seconds>(Msg.GetTime().time_since_epoch());
if (TimestampSeconds != m_Impl->m_LastLogSecs)
{
RwLock::ExclusiveLockScope _(m_Impl->m_TimestampLock);
m_Impl->m_LastLogSecs = TimestampSeconds;
m_Impl->m_CachedLocalTm = helpers::SafeLocaltime(LogClock::to_time_t(Msg.GetTime()));
m_Impl->m_CachedDatetime.clear();
m_Impl->m_CachedDatetime.push_back('[');
helpers::Pad2(m_Impl->m_CachedLocalTm.tm_year % 100, m_Impl->m_CachedDatetime);
m_Impl->m_CachedDatetime.push_back('-');
helpers::Pad2(m_Impl->m_CachedLocalTm.tm_mon + 1, m_Impl->m_CachedDatetime);
m_Impl->m_CachedDatetime.push_back('-');
helpers::Pad2(m_Impl->m_CachedLocalTm.tm_mday, m_Impl->m_CachedDatetime);
m_Impl->m_CachedDatetime.push_back(' ');
helpers::Pad2(m_Impl->m_CachedLocalTm.tm_hour, m_Impl->m_CachedDatetime);
m_Impl->m_CachedDatetime.push_back(':');
helpers::Pad2(m_Impl->m_CachedLocalTm.tm_min, m_Impl->m_CachedDatetime);
m_Impl->m_CachedDatetime.push_back(':');
helpers::Pad2(m_Impl->m_CachedLocalTm.tm_sec, m_Impl->m_CachedDatetime);
m_Impl->m_CachedDatetime.push_back('.');
}
Millis = helpers::TimeFraction<std::chrono::milliseconds>(Msg.GetTime());
}
else
{
auto ElapsedTime = Msg.GetTime() - m_Impl->m_Epoch;
TimestampSeconds = std::chrono::duration_cast<std::chrono::seconds>(ElapsedTime);
if (m_Impl->m_CacheTimestamp.load() != TimestampSeconds)
{
RwLock::ExclusiveLockScope _(m_Impl->m_TimestampLock);
m_Impl->m_CacheTimestamp = TimestampSeconds;
int Count = int(TimestampSeconds.count());
const int LogSecs = Count % 60;
Count /= 60;
const int LogMins = Count % 60;
Count /= 60;
const int LogHours = Count;
m_Impl->m_CachedDatetime.clear();
m_Impl->m_CachedDatetime.push_back('[');
helpers::Pad2(LogHours, m_Impl->m_CachedDatetime);
m_Impl->m_CachedDatetime.push_back(':');
helpers::Pad2(LogMins, m_Impl->m_CachedDatetime);
m_Impl->m_CachedDatetime.push_back(':');
helpers::Pad2(LogSecs, m_Impl->m_CachedDatetime);
m_Impl->m_CachedDatetime.push_back('.');
}
Millis = std::chrono::duration_cast<std::chrono::milliseconds>(ElapsedTime - TimestampSeconds);
}
{
RwLock::SharedLockScope _(m_Impl->m_TimestampLock);
OutBuffer.append(m_Impl->m_CachedDatetime.begin(), m_Impl->m_CachedDatetime.end());
}
helpers::Pad3(static_cast<uint32_t>(Millis.count()), OutBuffer);
OutBuffer.push_back(']');
OutBuffer.push_back(' ');
if (!m_Impl->m_LogId.empty())
{
OutBuffer.push_back('[');
helpers::AppendStringView(m_Impl->m_LogId, OutBuffer);
OutBuffer.push_back(']');
OutBuffer.push_back(' ');
}
// append logger name if exists
if (Msg.GetLoggerName().size() > 0)
{
OutBuffer.push_back('[');
helpers::AppendStringView(Msg.GetLoggerName(), OutBuffer);
OutBuffer.push_back(']');
OutBuffer.push_back(' ');
}
OutBuffer.push_back('[');
if (IsColorEnabled())
{
helpers::AppendAnsiColor(Msg.GetLevel(), OutBuffer);
}
helpers::AppendStringView(helpers::LevelToShortString(Msg.GetLevel()), OutBuffer);
if (IsColorEnabled())
{
helpers::AppendAnsiReset(OutBuffer);
}
OutBuffer.push_back(']');
OutBuffer.push_back(' ');
// add source location if present
if (Msg.GetSource())
{
OutBuffer.push_back('[');
const char* Filename = helpers::ShortFilename(Msg.GetSource().Filename);
helpers::AppendStringView(Filename, OutBuffer);
OutBuffer.push_back(':');
helpers::AppendInt(Msg.GetSource().Line, OutBuffer);
OutBuffer.push_back(']');
OutBuffer.push_back(' ');
}
// Handle newlines in single log call by prefixing each additional line to make
// subsequent lines align with the first line in the message
size_t AnsiBytes = IsColorEnabled() ? (helpers::AnsiColorForLevel(Msg.GetLevel()).size() + helpers::kAnsiReset.size()) : 0;
const size_t LinePrefixCount = Min<size_t>(OutBuffer.size() - AnsiBytes, m_Impl->m_LinePrefix.size());
auto MsgPayload = Msg.GetPayload();
auto ItLineBegin = MsgPayload.begin();
auto ItMessageEnd = MsgPayload.end();
bool IsFirstline = true;
{
auto ItLineEnd = ItLineBegin;
auto EmitLine = [&] {
if (IsFirstline)
{
IsFirstline = false;
}
else
{
helpers::AppendStringView(std::string_view(m_Impl->m_LinePrefix.data(), LinePrefixCount), OutBuffer);
}
helpers::AppendStringView(std::string_view(&*ItLineBegin, ItLineEnd - ItLineBegin), OutBuffer);
};
while (ItLineEnd != ItMessageEnd)
{
if (*ItLineEnd++ == '\n')
{
EmitLine();
ItLineBegin = ItLineEnd;
}
}
if (ItLineBegin != ItMessageEnd)
{
EmitLine();
helpers::AppendStringView("\n"sv, OutBuffer);
}
}
}
} // namespace zen::logging
|