refactor: build from conventions, adopt ConsoleLogger, and colorize TTY output
VoidBuildSystem CI / build-and-test-gcc16 (push) Failing after 4s
VoidBuildSystem CI / signing-openssl-gcc16 (push) Failing after 10s

This commit is contained in:
faumaray committed 2026-12-04 23:42:50 +03:00
1 parent ea6ac852e7
commit 83ec08390f
23 files changed
+172 -151

No files matched your search

+72 -32
View File
@@ -35,7 +35,11 @@
#include <Void/Configuration/Argparse.hpp>
#include <Void/Configuration/Env.hpp>
#include <Void/Data/StrictJson.hpp>
#include <Void/Log/ConsoleLogger.hpp>
#include <Void/Log/Formatter.hpp>
#include <Void/Log/Log.hpp>
#include <Void/Log/LoggingAdapter.hpp>
#include <unistd.h>
#include <Void/System/SignalWatcher.hpp>
#include <Void/System/TempDir.hpp>
#include <Void/Text/Semver.hpp>
@@ -68,6 +72,60 @@ namespace {
using namespace Void::Build;
// ANSI colors are enabled only for a terminal and are disabled by NO_COLOR or --no-color.
bool g_no_color = false;
bool color_enabled(int fd = 2) noexcept {
if (g_no_color || ::getenv("NO_COLOR") != nullptr) {
return false;
}
return ::isatty(fd) == 1;
}
std::string colorize_help(std::string_view text) {
if (!color_enabled(1)) {
return std::string{text};
}
constexpr std::string_view kReset = "\x1b[0m";
constexpr std::string_view kBold = "\x1b[1m";
constexpr std::string_view kCyan = "\x1b[36m";
std::string out;
out.reserve(text.size() + 64);
std::size_t pos = 0;
while (pos <= text.size()) {
const auto nl = text.find('\n', pos);
const auto end = nl == std::string_view::npos ? text.size() : nl;
const std::string_view line = text.substr(pos, end - pos);
const bool section_header =
!line.empty() && line.back() == ':' && line.find_first_not_of(" \t") == 0;
const bool usage = line.starts_with("Usage:");
const bool indented_entry = line.starts_with(" ") && !section_header &&
line.find_first_not_of(' ') != std::string_view::npos &&
line.find(" ") != std::string_view::npos;
if (section_header || usage) {
out += kBold;
out += line;
out += kReset;
} else if (indented_entry) {
const auto token_start = line.find_first_not_of(' ');
const auto token_end = line.find(' ', token_start);
out += line.substr(0, token_start);
out += kCyan;
out += line.substr(token_start, token_end - token_start);
out += kReset;
out += line.substr(token_end);
} else {
out += line;
}
if (nl == std::string_view::npos) {
break;
}
out += '\n';
pos = nl + 1;
}
return out;
}
class ConsoleLogGuard {
public:
explicit ConsoleLogGuard(Void::Log::Level level) {
@@ -76,46 +134,27 @@ class ConsoleLogGuard {
// Suppress internal VoidLibrary FileSystem open-miss warnings on cold cache probes.
Void::Log::set_module_level("FileSystem", Void::Log::Level::Error);
}
reg_id_ =
Void::Log::register_sink([mutex = std::make_shared<std::mutex>()](
Void::Log::Level lvl, std::string_view module,
std::string_view message, const std::source_location &) {
std::lock_guard lock{*mutex};
switch (lvl) {
case Void::Log::Level::Trace:
std::cerr << "[vbs][TRACE][" << module << "] " << message << '\n' << std::flush;
break;
case Void::Log::Level::Debug:
std::cerr << "[vbs][DEBUG][" << module << "] " << message << '\n' << std::flush;
break;
case Void::Log::Level::Info:
std::cerr << "[vbs][" << module << "] " << message << '\n' << std::flush;
break;
case Void::Log::Level::Warning:
std::cerr << "[vbs][WARN][" << module << "] " << message << '\n' << std::flush;
break;
case Void::Log::Level::Error:
std::cerr << "[vbs][ERROR][" << module << "] " << message << '\n' << std::flush;
break;
case Void::Log::Level::Fatal:
std::cerr << "[vbs][FATAL][" << module << "] " << message << '\n' << std::flush;
break;
case Void::Log::Level::Disabled:
break;
}
});
logger_ = std::make_unique<Void::Logging::ConsoleLogger>();
if (color_enabled()) {
auto formatter = std::make_shared<Void::Logging::PatternFormatter>("[{level:,auto}] {message}");
formatter->set_color_policy(Void::Logging::ColorPolicy::defaults());
logger_->set_formatter(std::move(formatter));
}
reg_id_ = Void::Log::register_sink(Void::Log::make_sink(*logger_));
}
~ConsoleLogGuard() {
if (reg_id_ != 0) {
(void)Void::Log::detach_and_drain(reg_id_);
}
logger_.reset();
}
ConsoleLogGuard(const ConsoleLogGuard &) = delete;
ConsoleLogGuard &operator=(const ConsoleLogGuard &) = delete;
private:
std::unique_ptr<Void::Logging::ConsoleLogger> logger_;
Void::Log::RegistrationId reg_id_{0};
};
@@ -179,7 +218,7 @@ SnapshotInputs collect_snapshot_inputs(const Graph::ProjectDescription &desc,
}
};
for (const char *d : {"src", "source", "include", "tests"}) {
for (const char *d : {"src", "sdk", "source", "include", "tests"}) {
add_dir_if_in_project(root / d);
}
@@ -2666,7 +2705,8 @@ int main(int argc, char **argv) {
auto add_log_flags = [&](Void::Argparse::Parser &p, bool allow_short_v = true) {
p.flag("--verbose", allow_short_v ? "-v" : "", "Enable verbose debug logs", verbose)
.flag("--quiet", "-q", "Suppress progress logs (warnings and errors only)", quiet);
.flag("--quiet", "-q", "Suppress progress logs (warnings and errors only)", quiet)
.flag("--no-color", "", "Disable ANSI colors", g_no_color);
};
auto configure_build_or_test = [&](Void::Argparse::Parser &p) {
@@ -3218,7 +3258,7 @@ int main(int argc, char **argv) {
const auto parsed = root.parse_detailed(argc, argv);
if (parsed.is_help()) {
std::cout << parsed.message;
std::cout << colorize_help(parsed.message);
return 0;
}
if (parsed.is_error()) {
@@ -3234,7 +3274,7 @@ int main(int argc, char **argv) {
return 0;
}
if (parsed.matched_command_path.empty()) {
std::cout << root.help();
std::cout << colorize_help(root.help());
return 0;
}