-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathSwiftLogging.cpp
210 lines (191 loc) · 6.77 KB
/
SwiftLogging.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
210
#include "swift/logging/SwiftLogging.h"
#include <filesystem>
#include <stdlib.h>
#include <optional>
#ifdef _WIN32
#include <process.h>
#else
#include <unistd.h>
#endif
#include "absl/strings/str_cat.h"
#define LEVEL_REGEX_PATTERN "trace|debug|info|warning|error|critical|no_logs"
BINLOG_ADAPT_ENUM(codeql::Log::Level, trace, debug, info, warning, error, critical, no_logs)
namespace codeql {
bool Log::initialized{false};
namespace {
using LevelRule = std::pair<std::regex, Log::Level>;
using LevelRules = std::vector<LevelRule>;
Log::Level getLevelFor(std::string_view name, const LevelRules& rules, Log::Level dflt) {
for (auto it = rules.rbegin(); it != rules.rend(); ++it) {
if (std::regex_match(std::begin(name), std::end(name), it->first)) {
return it->second;
}
}
return dflt;
}
const char* getEnvOr(const char* var, const char* dflt) {
if (const char* ret = getenv(var)) {
return ret;
}
return dflt;
}
std::string_view matchToView(std::csub_match m) {
return {m.first, static_cast<size_t>(m.length())};
}
Log::Level stringToLevel(std::string_view v) {
if (v == "trace") return Log::Level::trace;
if (v == "debug") return Log::Level::debug;
if (v == "info") return Log::Level::info;
if (v == "warning") return Log::Level::warning;
if (v == "error") return Log::Level::error;
if (v == "critical") return Log::Level::critical;
return Log::Level::no_logs;
}
Log::Level matchToLevel(std::csub_match m) {
return stringToLevel(matchToView(m));
}
} // namespace
std::vector<std::string> Log::collectLevelRulesAndReturnProblems(const char* envVar) {
std::vector<std::string> problems;
if (auto levels = getEnvOr(envVar, nullptr)) {
// expect comma-separated <glob pattern>:<log severity>
std::regex comma{","};
std::regex levelAssignment{
R"((?:([*./\w]+)|(?:out:(binary|text|console))):()" LEVEL_REGEX_PATTERN ")"};
std::cregex_token_iterator begin{levels, levels + strlen(levels), comma, -1};
std::cregex_token_iterator end{};
for (auto it = begin; it != end; ++it) {
std::cmatch match;
if (std::regex_match(it->first, it->second, match, levelAssignment)) {
auto level = matchToLevel(match[3]);
if (match[1].matched) {
auto pattern = match[1].str();
// replace all "*" with ".*" and all "." with "\.", turning the glob pattern into a regex
std::string::size_type pos = 0;
while ((pos = pattern.find_first_of("*.", pos)) != std::string::npos) {
pattern.insert(pos, (pattern[pos] == '*') ? "." : "\\");
pos += 2;
}
sourceRules.emplace_back(pattern, level);
} else {
auto out = matchToView(match[2]);
if (out == "binary") {
binary.level = level;
} else if (out == "text") {
text.level = level;
} else if (out == "console") {
console.level = level;
}
}
} else {
problems.emplace_back("Malformed log level rule: " + it->str());
}
}
}
return problems;
}
void Log::configure() {
// as we are configuring logging right now, we collect problems and log them at the end
auto problems = collectLevelRulesAndReturnProblems("CODEQL_EXTRACTOR_SWIFT_LOG_LEVELS");
auto timestamp = std::chrono::system_clock::now().time_since_epoch().count();
auto logBaseName = absl::StrCat(timestamp, "-", getpid());
if (text || binary) {
std::filesystem::path logFile = getEnvOr("CODEQL_EXTRACTOR_SWIFT_LOG_DIR", "extractor-out/log");
logFile /= "swift";
logFile /= programName;
logFile /= logBaseName;
std::error_code ec;
std::filesystem::create_directories(logFile.parent_path(), ec);
if (!ec) {
if (text) {
logFile.replace_extension(".log");
textFile.open(logFile);
if (!textFile) {
problems.emplace_back("Unable to open text log file " + logFile.string());
text.level = Level::no_logs;
}
}
if (binary) {
logFile.replace_extension(".blog");
binary.output.open(logFile, std::fstream::out | std::fstream::binary);
if (!binary.output) {
problems.emplace_back("Unable to open binary log file " + logFile.string());
binary.level = Level::no_logs;
}
}
} else {
problems.emplace_back("Unable to create log directory " + logFile.parent_path().string() +
": " + ec.message());
binary.level = Level::no_logs;
text.level = Level::no_logs;
}
}
if (auto diagDir = getEnvOr("CODEQL_EXTRACTOR_SWIFT_DIAGNOSTIC_DIR", nullptr)) {
std::filesystem::path diagFile = diagDir;
diagFile /= programName;
diagFile /= logBaseName;
diagFile.replace_extension(".jsonl");
std::error_code ec;
std::filesystem::create_directories(diagFile.parent_path(), ec);
if (!ec) {
diagnostics.open(diagFile);
if (!diagnostics) {
problems.emplace_back("Unable to open diagnostics json file " + diagFile.string());
}
} else {
problems.emplace_back("Unable to create diagnostics directory " +
diagFile.parent_path().string() + ": " + ec.message());
}
}
for (const auto& problem : problems) {
LOG_ERROR("{}", problem);
}
LOG_INFO("Logging configured (binary: {}, text: {}, console: {}, diagnostics: {})", binary.level,
text.level, console.level, diagnostics.good());
initialized = true;
flushImpl();
}
void Log::flushImpl() {
session.consume(*this);
if (text) {
textFile.flush();
}
if (binary) {
binary.output.flush();
}
if (diagnostics) {
diagnostics.flush();
}
}
void Log::diagnoseImpl(const Diagnostic& source,
const std::chrono::nanoseconds& elapsed,
std::string_view message) {
using Clock = std::chrono::system_clock;
Clock::time_point time{std::chrono::duration_cast<Clock::duration>(elapsed)};
if (diagnostics) {
diagnostics << source.json(time, message) << '\n';
}
}
Log::LoggerConfiguration Log::getLoggerConfigurationImpl(std::string_view name) {
LoggerConfiguration ret{session, std::string{programName}};
ret.fullyQualifiedName += '/';
ret.fullyQualifiedName += name;
ret.level = std::min({binary.level, text.level, console.level});
ret.level = getLevelFor(ret.fullyQualifiedName, sourceRules, ret.level);
// avoid Logger constructor loop
if (name != "logging") {
LOG_DEBUG("Configuring logger {} with level {}", ret.fullyQualifiedName, ret.level);
}
return ret;
}
Log& Log::write(const char* buffer, std::streamsize size) {
if (text) text.write(buffer, size);
if (binary) binary.write(buffer, size);
if (console) console.write(buffer, size);
return *this;
}
Logger& Log::logger() {
static Logger ret{getLoggerConfigurationImpl("logging")};
return ret;
}
} // namespace codeql