Skip to content

Commit 84c0170

Browse files
committed
Swift: add configuration of diagnostics logs
1 parent ca94b20 commit 84c0170

File tree

4 files changed

+20
-10
lines changed

4 files changed

+20
-10
lines changed

swift/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ A log file is produced for each run under `CODEQL_EXTRACTOR_SWIFT_LOG_DIR` (the
5252
You can use the environment variable `CODEQL_EXTRACTOR_SWIFT_LOG_LEVELS` to configure levels for
5353
loggers and outputs. This must have the form of a comma separated `spec:min_level` list, where
5454
`spec` is either a glob pattern (made up of alphanumeric, `/`, `*` and `.` characters) for
55-
matching logger names or one of `out:bin`, `out:text` or `out:console`, and `min_level` is one
55+
matching logger names or one of `out:binary`, `out:text`, `out:console` or `out:diagnostics`, and `min_level` is one
5656
of `trace`, `debug`, `info`, `warning`, `error`, `critical` or `no_logs` to turn logs completely off.
5757

5858
Current output default levels are no binary logs, `info` logs or higher in the text file and `warning` logs or higher on

swift/logging/SwiftDiagnostics.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ class SwiftDiagnosticsDumper {
6767
return output.good();
6868
}
6969

70+
void flush() { output.flush(); }
71+
7072
// write out binlog entries as corresponding JSON diagnostics entries. Expects all entries to have
7173
// a category equal to an id of a previously created SwiftDiagnosticSource.
7274
void write(const char* buffer, std::size_t bufferSize);

swift/logging/SwiftLogging.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <filesystem>
44
#include <stdlib.h>
55
#include <optional>
6+
#include <unistd.h>
67

78
#define LEVEL_REGEX_PATTERN "trace|debug|info|warning|error|critical|no_logs"
89

@@ -57,8 +58,8 @@ std::vector<std::string> Log::collectLevelRulesAndReturnProblems(const char* env
5758
if (auto levels = getEnvOr(envVar, nullptr)) {
5859
// expect comma-separated <glob pattern>:<log severity>
5960
std::regex comma{","};
60-
std::regex levelAssignment{R"((?:([*./\w]+)|(?:out:(bin|text|console))):()" LEVEL_REGEX_PATTERN
61-
")"};
61+
std::regex levelAssignment{
62+
R"((?:([*./\w]+)|(?:out:(binary|text|console|diagnostics))):()" LEVEL_REGEX_PATTERN ")"};
6263
std::cregex_token_iterator begin{levels, levels + strlen(levels), comma, -1};
6364
std::cregex_token_iterator end{};
6465
for (auto it = begin; it != end; ++it) {
@@ -76,12 +77,14 @@ std::vector<std::string> Log::collectLevelRulesAndReturnProblems(const char* env
7677
sourceRules.emplace_back(pattern, level);
7778
} else {
7879
auto out = matchToView(match[2]);
79-
if (out == "bin") {
80+
if (out == "binary") {
8081
binary.level = level;
8182
} else if (out == "text") {
8283
text.level = level;
8384
} else if (out == "console") {
8485
console.level = level;
86+
} else if (out == "diagnostics") {
87+
diagnostics.level = level;
8588
}
8689
}
8790
} else {
@@ -95,12 +98,14 @@ std::vector<std::string> Log::collectLevelRulesAndReturnProblems(const char* env
9598
void Log::configure() {
9699
// as we are configuring logging right now, we collect problems and log them at the end
97100
auto problems = collectLevelRulesAndReturnProblems("CODEQL_EXTRACTOR_SWIFT_LOG_LEVELS");
98-
auto now = std::to_string(std::chrono::system_clock::now().time_since_epoch().count());
101+
auto logBaseName = std::to_string(std::chrono::system_clock::now().time_since_epoch().count());
102+
logBaseName += '-';
103+
logBaseName += std::to_string(getpid());
99104
if (text || binary) {
100105
std::filesystem::path logFile = getEnvOr("CODEQL_EXTRACTOR_SWIFT_LOG_DIR", "extractor-out/log");
101106
logFile /= "swift";
102107
logFile /= programName;
103-
logFile /= now;
108+
logFile /= logBaseName;
104109
std::error_code ec;
105110
std::filesystem::create_directories(logFile.parent_path(), ec);
106111
if (!ec) {
@@ -130,7 +135,7 @@ void Log::configure() {
130135
std::filesystem::path diagFile =
131136
getEnvOr("CODEQL_EXTRACTOR_SWIFT_DIAGNOSTIC_DIR", "extractor-out/diagnostics");
132137
diagFile /= programName;
133-
diagFile /= now;
138+
diagFile /= logBaseName;
134139
diagFile.replace_extension(".jsonl");
135140
std::error_code ec;
136141
std::filesystem::create_directories(diagFile.parent_path(), ec);
@@ -149,8 +154,8 @@ void Log::configure() {
149154
for (const auto& problem : problems) {
150155
LOG_ERROR("{}", problem);
151156
}
152-
LOG_INFO("Logging configured (binary: {}, text: {}, console: {})", binary.level, text.level,
153-
console.level);
157+
LOG_INFO("Logging configured (binary: {}, text: {}, console: {}, diagnostics: {})", binary.level,
158+
text.level, console.level, diagnostics.level);
154159
initialized = true;
155160
flushImpl();
156161
}
@@ -163,6 +168,9 @@ void Log::flushImpl() {
163168
if (binary) {
164169
binary.output.flush();
165170
}
171+
if (diagnostics) {
172+
diagnostics.output.flush();
173+
}
166174
}
167175

168176
Log::LoggerConfiguration Log::getLoggerConfigurationImpl(std::string_view name) {

swift/logging/SwiftLogging.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ extern const std::string_view programName;
9999
// * using environment variable `CODEQL_EXTRACTOR_SWIFT_LOG_LEVELS` to configure levels for
100100
// loggers and outputs. This must have the form of a comma separated `spec:level` list, where
101101
// `spec` is either a glob pattern (made up of alphanumeric, `/`, `*` and `.` characters) for
102-
// matching logger names or one of `out:bin`, `out:text` or `out:console`.
102+
// matching logger names or one of `out:binary`, `out:text`, `out:console` or `out:diagnostics`.
103103
// Output default levels can be seen in the corresponding initializers below. By default, all
104104
// loggers are configured with the lowest output level
105105
class Log {

0 commit comments

Comments
 (0)