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
9598void 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
168176Log::LoggerConfiguration Log::getLoggerConfigurationImpl (std::string_view name) {
0 commit comments