|
| 1 | +#include "swift/extractor/infra/log/SwiftLogging.h" |
| 2 | + |
| 3 | +#include <filesystem> |
| 4 | +#include <stdlib.h> |
| 5 | +#include <optional> |
| 6 | + |
| 7 | +#define LEVEL_REGEX_PATTERN "trace|debug|info|warning|error|critical|no_logs" |
| 8 | + |
| 9 | +BINLOG_ADAPT_ENUM(codeql::Log::Level, trace, debug, info, warning, error, critical, no_logs) |
| 10 | + |
| 11 | +namespace codeql { |
| 12 | + |
| 13 | +namespace { |
| 14 | +using LevelRule = std::pair<std::regex, Log::Level>; |
| 15 | +using LevelRules = std::vector<LevelRule>; |
| 16 | + |
| 17 | +Log::Level getLevelFor(std::string_view name, const LevelRules& rules, Log::Level dflt) { |
| 18 | + for (auto it = rules.rbegin(); it != rules.rend(); ++it) { |
| 19 | + if (std::regex_match(std::begin(name), std::end(name), it->first)) { |
| 20 | + return it->second; |
| 21 | + } |
| 22 | + } |
| 23 | + return dflt; |
| 24 | +} |
| 25 | + |
| 26 | +const char* getEnvOr(const char* var, const char* dflt) { |
| 27 | + if (const char* ret = getenv(var)) { |
| 28 | + return ret; |
| 29 | + } |
| 30 | + return dflt; |
| 31 | +} |
| 32 | + |
| 33 | +std::string_view matchToView(std::csub_match m) { |
| 34 | + return {m.first, static_cast<size_t>(m.length())}; |
| 35 | +} |
| 36 | + |
| 37 | +Log::Level stringToLevel(std::string_view v) { |
| 38 | + if (v == "trace") return Log::Level::trace; |
| 39 | + if (v == "debug") return Log::Level::debug; |
| 40 | + if (v == "info") return Log::Level::info; |
| 41 | + if (v == "warning") return Log::Level::warning; |
| 42 | + if (v == "error") return Log::Level::error; |
| 43 | + if (v == "critical") return Log::Level::critical; |
| 44 | + return Log::Level::no_logs; |
| 45 | +} |
| 46 | + |
| 47 | +Log::Level matchToLevel(std::csub_match m) { |
| 48 | + return stringToLevel(matchToView(m)); |
| 49 | +} |
| 50 | + |
| 51 | +} // namespace |
| 52 | + |
| 53 | +std::vector<std::string> Log::collectSeverityRulesAndReturnProblems(const char* envVar) { |
| 54 | + std::vector<std::string> problems; |
| 55 | + if (auto levels = getEnvOr(envVar, nullptr)) { |
| 56 | + // expect comma-separated <glob pattern>:<log severity> |
| 57 | + std::regex comma{","}; |
| 58 | + std::regex levelAssignment{R"((?:([*./\w]+)|(?:out:(bin|text|console))):()" LEVEL_REGEX_PATTERN |
| 59 | + ")"}; |
| 60 | + std::cregex_token_iterator begin{levels, levels + strlen(levels), comma, -1}; |
| 61 | + std::cregex_token_iterator end{}; |
| 62 | + for (auto it = begin; it != end; ++it) { |
| 63 | + std::cmatch match; |
| 64 | + if (std::regex_match(it->first, it->second, match, levelAssignment)) { |
| 65 | + auto level = matchToLevel(match[3]); |
| 66 | + if (match[1].matched) { |
| 67 | + auto pattern = match[1].str(); |
| 68 | + // replace all "*" with ".*" and all "." with "\.", turning the glob pattern into a regex |
| 69 | + std::string::size_type pos = 0; |
| 70 | + while ((pos = pattern.find_first_of("*.", pos)) != std::string::npos) { |
| 71 | + pattern.insert(pos, (pattern[pos] == '*') ? "." : "\\"); |
| 72 | + pos += 2; |
| 73 | + } |
| 74 | + sourceRules.emplace_back(pattern, level); |
| 75 | + } else { |
| 76 | + auto out = matchToView(match[2]); |
| 77 | + if (out == "bin") { |
| 78 | + binary.level = level; |
| 79 | + } else if (out == "text") { |
| 80 | + text.level = level; |
| 81 | + } else if (out == "console") { |
| 82 | + console.level = level; |
| 83 | + } |
| 84 | + } |
| 85 | + } else { |
| 86 | + problems.emplace_back("Malformed log level rule: " + it->str()); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + return problems; |
| 91 | +} |
| 92 | + |
| 93 | +void Log::configure() { |
| 94 | + // as we are configuring logging right now, we collect problems and log them at the end |
| 95 | + auto problems = collectSeverityRulesAndReturnProblems("CODEQL_EXTRACTOR_SWIFT_LOG_LEVELS"); |
| 96 | + if (text || binary) { |
| 97 | + std::filesystem::path logFile = getEnvOr("CODEQL_EXTRACTOR_SWIFT_LOG_DIR", "."); |
| 98 | + logFile /= logRootName; |
| 99 | + logFile /= std::to_string(std::chrono::system_clock::now().time_since_epoch().count()); |
| 100 | + std::error_code ec; |
| 101 | + std::filesystem::create_directories(logFile.parent_path(), ec); |
| 102 | + if (!ec) { |
| 103 | + if (text) { |
| 104 | + logFile.replace_extension(".log"); |
| 105 | + textFile.open(logFile); |
| 106 | + if (!textFile) { |
| 107 | + problems.emplace_back("Unable to open text log file " + logFile.string()); |
| 108 | + text.level = Level::no_logs; |
| 109 | + } |
| 110 | + } |
| 111 | + if (binary) { |
| 112 | + logFile.replace_extension(".blog"); |
| 113 | + binary.output.open(logFile, std::fstream::out | std::fstream::binary); |
| 114 | + if (!binary.output) { |
| 115 | + problems.emplace_back("Unable to open binary log file " + logFile.string()); |
| 116 | + binary.level = Level::no_logs; |
| 117 | + } |
| 118 | + } |
| 119 | + } else { |
| 120 | + problems.emplace_back("Unable to create log directory " + logFile.parent_path().string() + |
| 121 | + ": " + ec.message()); |
| 122 | + binary.level = Level::no_logs; |
| 123 | + text.level = Level::no_logs; |
| 124 | + } |
| 125 | + } |
| 126 | + for (const auto& problem : problems) { |
| 127 | + LOG_ERROR("{}", problem); |
| 128 | + } |
| 129 | + LOG_INFO("Logging configured (binary: {}, text: {}, console: {})", binary.level, text.level, |
| 130 | + console.level); |
| 131 | + flushImpl(); |
| 132 | +} |
| 133 | + |
| 134 | +void Log::flushImpl() { |
| 135 | + session.consume(*this); |
| 136 | +} |
| 137 | + |
| 138 | +Log::LoggerConfiguration Log::getLoggerConfigurationImpl(std::string_view name) { |
| 139 | + LoggerConfiguration ret{session, std::string{logRootName}}; |
| 140 | + ret.fullyQualifiedName += '/'; |
| 141 | + ret.fullyQualifiedName += name; |
| 142 | + ret.level = std::min({binary.level, text.level, console.level}); |
| 143 | + ret.level = getLevelFor(ret.fullyQualifiedName, sourceRules, ret.level); |
| 144 | + // avoid Logger constructor loop |
| 145 | + if (name != "logging") { |
| 146 | + LOG_DEBUG("Configuring logger {} with level {}", ret.fullyQualifiedName, ret.level); |
| 147 | + } |
| 148 | + return ret; |
| 149 | +} |
| 150 | + |
| 151 | +Log& Log::write(const char* buffer, std::streamsize size) { |
| 152 | + if (text) text.write(buffer, size); |
| 153 | + if (binary) binary.write(buffer, size); |
| 154 | + if (console) console.write(buffer, size); |
| 155 | + return *this; |
| 156 | +} |
| 157 | + |
| 158 | +Logger& Log::logger() { |
| 159 | + static Logger ret{getLoggerConfigurationImpl("logging")}; |
| 160 | + return ret; |
| 161 | +} |
| 162 | + |
| 163 | +} // namespace codeql |
0 commit comments