Reflection and filesystem

Signed-off-by: faumaray <faumaray@gmail.com>
This commit is contained in:
faumaray committed 2026-05-03 18:09:46 +03:00
1 parent f0825fbd1a
commit 42268c6c35
45 files changed
+2732 -262

No files matched your search

+3
View File
@@ -0,0 +1,3 @@
void_add_module(FileSystem
INTERNAL_DEPS Result
)
+72
View File
@@ -0,0 +1,72 @@
#include <Void/Logging/ConsoleLogger.hpp>
#ifdef VOID_HAS_STACKTRACE
#include <Void/Stacktrace/Color.hpp>
#endif
#include <format>
#include <iostream>
#include <ostream>
#include <string>
#include <string_view>
namespace Void::Logging
{
namespace
{
#ifdef VOID_HAS_STACKTRACE
std::string_view color_for(Level level)
{
using namespace Void::Stacktrace::Color;
switch (level) {
case Level::Trace:
return dim;
case Level::Debug:
return cyan;
case Level::Info:
return bold_cyan;
case Level::Warning:
return bold_yellow;
case Level::Error:
return bold_red;
case Level::Fatal:
return bold_magenta;
default:
return reset;
}
}
#endif
std::string format_entry(const Entry& entry)
{
auto level_str = std::string{to_string(entry.level)};
#ifdef VOID_HAS_STACKTRACE
using namespace Void::Stacktrace::Color;
return std::format("[{}] {}\n",
apply(color_for(entry.level), level_str),
entry.message);
#else
return std::format("[{}] {}\n", level_str, entry.message);
#endif
}
} // namespace
ConsoleLogger::ConsoleLogger(std::ostream& out,
Mode mode,
std::size_t capacity)
: Logger(mode, capacity), m_out(out)
{
}
ConsoleLogger::ConsoleLogger() : ConsoleLogger(std::cerr) {}
ConsoleLogger::~ConsoleLogger() noexcept { flush(); }
void ConsoleLogger::write(const Entry& entry)
{
m_out << format_entry(entry);
m_out.flush();
}
} // namespace Void::Logging
+50
View File
@@ -0,0 +1,50 @@
#include <Void/Logging/FileLogger.hpp>
#include <format>
#include <stdexcept>
namespace Void::Logging
{
FileLogger::FileLogger(const std::filesystem::path& path,
Mode mode,
std::size_t capacity)
: Logger(mode, capacity), m_file(path, std::ios::app)
{
if (!m_file.is_open()) {
throw std::runtime_error(
std::format("FileLogger: cannot open '{}'", path.string()));
}
}
Void::Result::Result<std::unique_ptr<FileLogger>, Void::Result::Error>
FileLogger::open(const std::filesystem::path& path,
Mode mode,
std::size_t capacity)
{
try {
return Void::Result::ok(
std::make_unique<FileLogger>(path, mode, capacity));
} catch (const std::exception& e) {
return Void::Result::fail(std::string{e.what()});
}
}
FileLogger::~FileLogger() noexcept { flush(); }
void FileLogger::write(const Entry& entry)
{
m_file << std::format(
"[{}] {}\n", to_string(entry.level), entry.message);
m_file.flush();
}
void FileLogger::on_flush(std::span<const Entry> entries)
{
for (const auto& e : entries) {
m_file << std::format("[{}] {}\n", to_string(e.level), e.message);
}
m_file.flush();
}
} // namespace Void::Logging
+4
View File
@@ -0,0 +1,4 @@
void_add_module(Logging
INTERNAL_DEPS Result
OPTIONAL_DEPS Stacktrace
)
+1
View File
@@ -0,0 +1 @@
void_add_module(Result)
+99 -51
View File
@@ -1,64 +1,112 @@
#include <Void/Stacktrace/Stacktrace.hpp>
#include <Void/Stacktrace/Color.hpp>
#include <fstream>
#include <sstream>
#include <format>
#include <algorithm>
#include <filesystem>
#include <format>
#include <fstream>
#include <sstream>
#define BOOST_STACKTRACE_USE_BACKTRACE
#include <boost/stacktrace.hpp>
#include <boost/core/demangle.hpp>
#include <boost/stacktrace.hpp>
namespace Void::Stacktrace {
namespace Void::Stacktrace
{
std::vector<StackFrame> capture_stacktrace(unsigned skip_frames, unsigned max_frames) {
std::vector<StackFrame> result;
try {
boost::stacktrace::stacktrace bt(skip_frames + 1, max_frames);
for (auto it = bt.begin(); it != bt.end(); ++it) {
StackFrame frame;
std::string name = it->name();
if (!name.empty()) frame.function_name = boost::core::demangle(name.c_str());
frame.source_file = it->source_file(); frame.source_line = it->source_line();
if (!frame.source_file.empty() && frame.source_line > 0) frame.source_snippet = read_source_snippet(frame.source_file, frame.source_line, 2);
result.push_back(std::move(frame));
}
} catch (...) {}
return result;
}
std::vector<StackFrame> capture_stacktrace(unsigned skip_frames,
unsigned max_frames)
{
std::vector<StackFrame> result;
try {
boost::stacktrace::stacktrace bt(skip_frames + 1, max_frames);
for (auto it = bt.begin(); it != bt.end(); ++it) {
StackFrame frame;
std::string name = it->name();
if (!name.empty()) {
frame.function_name = boost::core::demangle(name.c_str());
}
frame.source_file = it->source_file();
frame.source_line = it->source_line();
if (!frame.source_file.empty() && frame.source_line > 0) {
frame.source_snippet =
read_source_snippet(frame.source_file,
frame.source_line, 2)
.value_or(std::string{});
}
result.push_back(std::move(frame));
}
} catch (...) {
}
return result;
}
std::string read_source_snippet(const std::string& file, int line, int context_lines) {
std::ifstream ifs{file}; if (!ifs.is_open()) return {};
std::ostringstream result; std::string current_line; int line_num = 1;
int start = std::max(1, line - context_lines); int end = line + context_lines;
while (std::getline(ifs, current_line)) {
if (line_num >= start && line_num <= end) {
std::string prefix = (line_num == line) ? " >>> " : " ";
result << std::format("{:>4}{}{}\n", line_num, prefix, current_line);
}
if (line_num > end) break; ++line_num;
}
return result.str();
}
Void::Result::Result<std::string, Void::Result::Error>
read_source_snippet(const std::string& file, int line,
int context_lines)
{
std::ifstream ifs{file};
if (!ifs.is_open()) {
return Void::Result::fail(
std::format("read_source_snippet: cannot open '{}'", file));
}
std::ostringstream result;
std::string current_line;
int line_num = 1;
int start = std::max(1, line - context_lines);
int end = line + context_lines;
while (std::getline(ifs, current_line)) {
if (line_num >= start && line_num <= end) {
std::string prefix = (line_num == line) ? " >>> " : " ";
result << std::format("{:>4}{}{}\n", line_num, prefix,
current_line);
}
if (line_num > end) {
break;
}
++line_num;
}
return Void::Result::ok(result.str());
}
std::string format_stacktrace(const std::vector<StackFrame>& frames) {
if (frames.empty()) return " <no stacktrace available>\n";
std::ostringstream oss;
for (size_t i = 0; i < frames.size(); ++i) {
const auto& frame = frames[i];
oss << std::format(" #{:02} ", i);
if (!frame.function_name.empty()) oss << (Color::g_color_enabled ? Color::apply(Color::bold_cyan, frame.function_name) : frame.function_name);
else oss << "<unknown>";
if (!frame.source_file.empty() && frame.source_line > 0) {
std::filesystem::path p{frame.source_file};
oss << "\n at " << (Color::g_color_enabled ? Color::apply(Color::dim, p.filename().string()) : p.filename().string()) << ":" << frame.source_line;
}
oss << "\n";
if (!frame.source_snippet.empty() && i < 5) oss << " " << (Color::g_color_enabled ? Color::apply(Color::dim, frame.source_snippet) : frame.source_snippet);
}
return oss.str();
}
std::string format_stacktrace(const std::vector<StackFrame>& frames)
{
if (frames.empty()) {
return " <no stacktrace available>\n";
}
std::ostringstream oss;
for (size_t i = 0; i < frames.size(); ++i) {
const auto& frame = frames[i];
oss << std::format(" #{:02} ", i);
if (!frame.function_name.empty()) {
oss << (Color::g_color_enabled
? Color::apply(Color::bold_cyan,
frame.function_name)
: frame.function_name);
} else {
oss << "<unknown>";
}
if (!frame.source_file.empty() && frame.source_line > 0) {
std::filesystem::path p{frame.source_file};
oss << "\n at "
<< (Color::g_color_enabled
? Color::apply(Color::dim, p.filename().string())
: p.filename().string())
<< ":" << frame.source_line;
}
oss << "\n";
if (!frame.source_snippet.empty() && i < 5) {
oss << " "
<< (Color::g_color_enabled
? Color::apply(Color::dim, frame.source_snippet)
: frame.source_snippet);
}
}
return oss.str();
}
void print_stacktrace(std::ostream& os, const std::vector<StackFrame>& frames) { os << format_stacktrace(frames); }
void print_stacktrace(std::ostream& os,
const std::vector<StackFrame>& frames)
{
os << format_stacktrace(frames);
}
} // namespace Void::Stacktrace
+1
View File
@@ -1,4 +1,5 @@
void_add_module(Stacktrace
INTERNAL_DEPS Result
EXTERNAL_DEPS Boost::stacktrace_backtrace
DEPS_WIN32 dbghelp
DEPS_UNIX dl
+67 -1
View File
@@ -1,5 +1,7 @@
#include <Void/Testing/Runner.hpp>
#include "Void/Testing/Config.hpp"
#include <Void/Logging/FileLogger.hpp>
#include <Void/Logging/Level.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/ConsoleReporter.hpp>
#include <Void/Stacktrace/Color.hpp>
@@ -10,8 +12,10 @@
#include <algorithm>
#include <csignal>
#include <cstdlib>
#include <format>
#include <fstream>
#include <iostream>
#include <memory>
#include <random>
#include <regex>
#include <unordered_map>
@@ -26,7 +30,7 @@ namespace Void::Testing
std::string arg = argv[i];
if ((arg == "--filter" || arg == "--suite" || arg == "--reporter" ||
arg == "--output" || arg == "--tag" || arg == "--seed" ||
arg == "--repeat") &&
arg == "--repeat" || arg == "--log-file") &&
i + 1 < argc) {
std::string val = argv[++i];
if (arg == "--filter") {
@@ -39,6 +43,8 @@ namespace Void::Testing
config.output_file = val;
} else if (arg == "--tag") {
config.tag_filter = val;
} else if (arg == "--log-file") {
config.log_file = val;
} else if (arg == "--seed") {
config.shuffle = true;
config.random_seed = static_cast<unsigned>(std::stoul(val));
@@ -250,6 +256,10 @@ namespace Void::Testing
<< "\n"
<< " " << apply(green, "--output <file> ")
<< apply(dim, "Write reporter output to a file") << "\n"
<< " " << apply(green, "--log-file <file> ")
<< apply(dim,
"Mirror test events to <file> via Void::Logging")
<< "\n"
<< " " << apply(green, "--verbose ")
<< apply(dim, "Enable verbose output") << "\n"
<< " " << apply(green, "--no-color ")
@@ -274,6 +284,17 @@ namespace Void::Testing
auto tests = filter_tests(all_tests, config);
auto reporter = create_reporter(config);
std::unique_ptr<Void::Logging::FileLogger> log_sink;
if (!config.log_file.empty()) {
try {
log_sink = std::make_unique<Void::Logging::FileLogger>(
config.log_file);
} catch (const std::exception& e) {
std::cerr << "warning: --log-file failed: " << e.what()
<< "\n";
}
}
if (config.list_only) {
for (const auto& t : tests) {
std::cout << t.suite << "." << t.name
@@ -284,9 +305,17 @@ namespace Void::Testing
RunResult run_result;
reporter->on_run_start(config, static_cast<int>(tests.size()));
if (log_sink) {
log_sink->info(std::format("run start: {} test(s) discovered",
tests.size()));
}
if (config.shuffle) {
std::shuffle(tests.begin(), tests.end(),
std::mt19937{config.random_seed});
if (log_sink) {
log_sink->info(std::format("shuffle enabled (seed = {})",
config.random_seed));
}
}
std::vector<SuiteResult> suite_results;
@@ -309,10 +338,41 @@ namespace Void::Testing
return t.suite ==
suite.name;
}));
if (log_sink) {
log_sink->info(
std::format("suite start: {}", suite.name));
}
}
reporter->on_test_start(test);
auto result = execute_test(test);
reporter->on_test_end(result);
if (log_sink) {
const auto qualified =
std::format("{}.{}", result.suite, result.name);
switch (result.state) {
case TestState::Passed:
log_sink->info(
std::format("test pass: {}", qualified));
break;
case TestState::Failed:
log_sink->error(std::format(
"test fail: {} — {}", qualified,
result.crash_info.empty() ? "assertion failed"
: result.crash_info));
break;
case TestState::Skipped:
log_sink->warning(std::format(
"test skip: {} ({})", qualified,
result.skip_reason.empty() ? "no reason"
: result.skip_reason));
break;
case TestState::Crashed:
log_sink->fatal(std::format(
"test crash: {} — {}", qualified,
result.crash_info));
break;
}
}
if (rep == config.repeat - 1) {
suite.tests.push_back(std::move(result));
}
@@ -343,6 +403,12 @@ namespace Void::Testing
}
run_result.total_duration = end_time - start_time;
reporter->on_run_end(run_result);
if (log_sink) {
log_sink->info(std::format(
"run end: {} total / {} passed / {} failed / {} skipped / {} crashed",
run_result.total, run_result.passed, run_result.failed,
run_result.skipped, run_result.crashed));
}
return run_result;
}
+1 -1
View File
@@ -1 +1 @@
void_add_module(Testing INTERNAL_DEPS Stacktrace)
void_add_module(Testing INTERNAL_DEPS Stacktrace Logging Result)
+1 -1
View File
@@ -1 +1 @@
void_add_module(TestingMock INTERNAL_DEPS Testing)
void_add_module(TestingMock INTERNAL_DEPS Testing Result)