-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.cpp
209 lines (167 loc) · 6.44 KB
/
log.cpp
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
#include "log.h"
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <mutex>
#include <optional>
#include <string>
#include <windows.h>
namespace dechamps_cpplog {
namespace {
template <auto functionPointer> struct FunctionPointerFunctor {
template <typename... Args> auto operator()(Args&&... args) {
return functionPointer(std::forward<Args>(args)...);
}
};
using Library = std::unique_ptr<std::decay_t<decltype(*HMODULE())>, FunctionPointerFunctor<FreeLibrary>>;
// On systems that support GetSystemTimePreciseAsFileTime() (i.e. Windows 8 and greater), use that.
// Otherwise, fall back to GetSystemTimeAsFileTime().
// See https://github.com/dechamps/FlexASIO/issues/15
decltype(GetSystemTimeAsFileTime)* GetSystemTimeAsFileTimeFunction() {
static const auto function = [] {
static const Library library(LoadLibraryA("kernel32.dll"));
if (library != nullptr) {
const auto function = GetProcAddress(library.get(), "GetSystemTimePreciseAsFileTime");
if (function != nullptr) return reinterpret_cast<decltype(GetSystemTimePreciseAsFileTime)*>(function);
}
return GetSystemTimeAsFileTime;
}();
return function;
}
int64_t FileTimeToTenthsOfUs(const FILETIME filetime) {
ULARGE_INTEGER integer;
integer.LowPart = filetime.dwLowDateTime;
integer.HighPart = filetime.dwHighDateTime;
return integer.QuadPart;
}
std::string FormatSystemTimeISO8601(const SYSTEMTIME& systemtime) {
std::stringstream stream;
stream.fill('0');
stream << std::setw(2) << systemtime.wYear << "-" << std::setw(2) << systemtime.wMonth << "-" << std::setw(2) << systemtime.wDay;
stream << "T";
stream << std::setw(2) << systemtime.wHour << ":" << std::setw(2) << systemtime.wMinute << ":" << std::setw(2) << systemtime.wSecond;
return stream.str();
}
std::pair<TIME_ZONE_INFORMATION, LONG> GetTimezoneAndBias() {
TIME_ZONE_INFORMATION localTimezone;
// Note: contrary to what MSDN seems to indicate, TIME_ZONE_INFORMATION::Bias does *not* include the StandardBias/DaylightBias.
switch (GetTimeZoneInformation(&localTimezone)) {
case TIME_ZONE_ID_UNKNOWN:
return { localTimezone, localTimezone.Bias };
case TIME_ZONE_ID_STANDARD:
return { localTimezone, localTimezone.Bias + localTimezone.StandardBias };
case TIME_ZONE_ID_DAYLIGHT:
return { localTimezone, localTimezone.Bias + localTimezone.DaylightBias };
default:
abort();
}
}
std::string FormatFiletimeISO8601(const FILETIME filetime) {
SYSTEMTIME systemtimeUTC;
if (!FileTimeToSystemTime(&filetime, &systemtimeUTC)) abort();
systemtimeUTC.wMilliseconds = 0;
const auto localTimezoneAndBias = GetTimezoneAndBias();
SYSTEMTIME systemtimeLocal;
if (!SystemTimeToTzSpecificLocalTime(&localTimezoneAndBias.first, &systemtimeUTC, &systemtimeLocal)) abort();
std::stringstream stream;
stream.fill('0');
stream << FormatSystemTimeISO8601(systemtimeLocal);
FILETIME truncatedFiletime;
if (!SystemTimeToFileTime(&systemtimeUTC, &truncatedFiletime)) abort();
stream << "." << std::setw(7) << FileTimeToTenthsOfUs(filetime) - FileTimeToTenthsOfUs(truncatedFiletime);
stream << ((localTimezoneAndBias.second >= 0) ? "-" : "+");
const auto absoluteBias = std::abs(localTimezoneAndBias.second);
stream << std::setw(2) << absoluteBias / 60 << ":" << std::setw(2) << absoluteBias % 60;
return stream.str();
}
std::string GetModuleName() {
std::string moduleName(MAX_PATH, 0);
moduleName.resize(GetModuleFileNameA(NULL, moduleName.data(), DWORD(moduleName.size())));
return moduleName;
}
}
PreambleLogSink::PreambleLogSink(LogSink& backend) : backend(backend) {
Logger(this) << "Log time source: " << ((GetSystemTimeAsFileTimeFunction() == GetSystemTimeAsFileTime) ? "GetSystemTimeAsFileTime" : "GetSystemTimePreciseAsFileTime");
Logger(this) << "Host process: " << GetModuleName();
}
FileLogSink::FileLogSink(std::filesystem::path path, Options options) : path(std::move(path)), options(std::move(options)), stream(this->path, std::ios::app | std::ios::out) {
Logger(this) << "Logfile opened: " << this->path;
CheckSize();
}
FileLogSink::~FileLogSink() {
Logger(this) << "Closing logfile";
}
void FileLogSink::Write(const std::string_view str) {
if (!stream.is_open()) return;
stream_sink.Write(str);
const auto size = str.size();
if (size >= bytesRemainingUntilSizeCheck) CheckSize();
else bytesRemainingUntilSizeCheck -= size;
}
void FileLogSink::CheckSize() {
bytesRemainingUntilSizeCheck = (std::numeric_limits<uintmax_t>::max)();
const auto sizeBytes = std::filesystem::file_size(path);
const auto maxSizeBytes = options.maxSizeBytes;
Logger(this) << "Current log file size is " << sizeBytes << " bytes (maximum allowed: " << maxSizeBytes << " bytes)";
if (sizeBytes < maxSizeBytes) {
bytesRemainingUntilSizeCheck = options.sizeCheckPeriodBytes;
return;
}
Logger(this) << "Closing logfile as the maximum size is exceeded";
stream.close();
}
void ThreadSafeLogSink::Write(const std::string_view str) {
std::scoped_lock lock(mutex);
backend.Write(str);
}
void AsyncLogSink::Write(const std::string_view str) {
std::string strCopy(str);
bool notify;
{
std::scoped_lock lock(mutex);
if (shutdown) abort();
notify = queue.empty();
queue.emplace_back(std::move(strCopy));
}
if (notify) stateChanged.notify_all();
}
void AsyncLogSink::RunThread() {
for (;;) {
std::vector<std::string> localQueue;
localQueue.reserve(32);
bool localShutdown;
{
std::unique_lock lock(mutex);
stateChanged.wait(lock, [&] { return !queue.empty() || shutdown; });
localQueue.swap(queue);
localShutdown = shutdown;
}
for (const auto& str : localQueue) backend.Write(str);
if (localShutdown) break;
}
}
AsyncLogSink::~AsyncLogSink() {
{
std::scoped_lock lock(mutex);
shutdown = true;
}
stateChanged.notify_all();
thread.join();
}
Logger::Logger(LogSink* sink, const Options& options) {
if (sink == nullptr) return;
enabledState.emplace(*sink);
auto& stream = enabledState->stream;
if (options.prependTime) {
FILETIME now = { 0 };
GetSystemTimeAsFileTimeFunction()(&now);
stream << FormatFiletimeISO8601(now) << " ";
}
if (options.prependProcessId) stream << GetCurrentProcessId() << " ";
if (options.prependThreadId) stream << GetCurrentThreadId() << " ";
}
Logger::~Logger() {
if (!enabledState.has_value()) return;
enabledState->sink.Write(enabledState->stream.str());
}
}