2026-01-31 11:28:03 +08:00
|
|
|
#include "Reporter.hpp"
|
|
|
|
|
#include <yycc/string/op.hpp>
|
|
|
|
|
#include <cstdarg>
|
2026-03-03 17:11:30 +08:00
|
|
|
#include <stdexcept>
|
2026-01-31 11:28:03 +08:00
|
|
|
|
2026-03-03 17:11:30 +08:00
|
|
|
using BMapInspector::Utils::ReportLevel;
|
2026-01-31 11:28:03 +08:00
|
|
|
namespace strop = yycc::string::op;
|
|
|
|
|
|
2026-01-31 23:38:26 +08:00
|
|
|
namespace BMapInspector::Reporter {
|
2026-01-31 11:28:03 +08:00
|
|
|
|
|
|
|
|
#pragma region Reporter
|
|
|
|
|
|
2026-03-03 17:11:30 +08:00
|
|
|
Reporter::Reporter() : current_rule(std::nullopt), reports() {}
|
2026-01-31 11:28:03 +08:00
|
|
|
|
|
|
|
|
Reporter::~Reporter() {}
|
|
|
|
|
|
2026-03-03 17:11:30 +08:00
|
|
|
void Reporter::EnterRule(const std::u8string_view &rule) {
|
|
|
|
|
if (this->current_rule.has_value()) throw std::logic_error("can not enter rule multiple times");
|
|
|
|
|
else this->current_rule = std::u8string(rule);
|
2026-01-31 11:28:03 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-03 17:11:30 +08:00
|
|
|
void Reporter::LeaveRule() {
|
|
|
|
|
if (this->current_rule.has_value()) this->current_rule = std::nullopt;
|
|
|
|
|
else throw std::logic_error("can not leave rule without any existing rule");
|
2026-01-31 11:28:03 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-03 17:11:30 +08:00
|
|
|
void Reporter::AddReport(ReportLevel level, const std::u8string_view &content) {
|
|
|
|
|
if (this->current_rule.has_value()) {
|
|
|
|
|
this->reports.emplace_back(Report{
|
|
|
|
|
.level = level,
|
|
|
|
|
.rule = std::u8string(this->current_rule.value()),
|
|
|
|
|
.content = std::u8string(content),
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
throw std::logic_error("can not add report without any rule scope");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Reporter::WriteInfo(const std::u8string_view &content) {
|
|
|
|
|
this->AddReport(ReportLevel::Info, content);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Reporter::FormatInfo(const char8_t *fmt, ...) {
|
2026-01-31 11:28:03 +08:00
|
|
|
va_list argptr;
|
|
|
|
|
va_start(argptr, fmt);
|
2026-03-03 17:11:30 +08:00
|
|
|
this->WriteInfo(strop::vprintf(fmt, argptr));
|
2026-01-31 11:28:03 +08:00
|
|
|
va_end(argptr);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-03 17:11:30 +08:00
|
|
|
void Reporter::WriteWarning(const std::u8string_view &content) {
|
|
|
|
|
this->AddReport(ReportLevel::Warning, content);
|
2026-01-31 11:28:03 +08:00
|
|
|
}
|
2026-03-03 17:11:30 +08:00
|
|
|
void Reporter::FormatWarning(const char8_t *fmt, ...) {
|
2026-01-31 11:28:03 +08:00
|
|
|
va_list argptr;
|
|
|
|
|
va_start(argptr, fmt);
|
2026-03-03 17:11:30 +08:00
|
|
|
this->WriteWarning(strop::vprintf(fmt, argptr));
|
2026-01-31 11:28:03 +08:00
|
|
|
va_end(argptr);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-03 17:11:30 +08:00
|
|
|
void Reporter::WriteError(const std::u8string_view &content) {
|
|
|
|
|
this->AddReport(ReportLevel::Error, content);
|
2026-01-31 11:28:03 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-03 17:11:30 +08:00
|
|
|
void Reporter::FormatError(const char8_t *fmt, ...) {
|
2026-01-31 11:28:03 +08:00
|
|
|
va_list argptr;
|
|
|
|
|
va_start(argptr, fmt);
|
2026-03-03 17:11:30 +08:00
|
|
|
this->WriteError(strop::vprintf(fmt, argptr));
|
2026-01-31 11:28:03 +08:00
|
|
|
va_end(argptr);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-31 23:38:26 +08:00
|
|
|
ReporterDigest Reporter::GetDigest() const {
|
|
|
|
|
ReporterDigest digest{.cnt_err = 0, .cnt_warn = 0, .cnt_info = 0};
|
2026-01-31 11:28:03 +08:00
|
|
|
for (const auto &report : this->reports) {
|
|
|
|
|
switch (report.level) {
|
2026-03-03 17:11:30 +08:00
|
|
|
case ReportLevel::Error:
|
2026-01-31 23:38:26 +08:00
|
|
|
++digest.cnt_err;
|
2026-01-31 11:28:03 +08:00
|
|
|
break;
|
2026-03-03 17:11:30 +08:00
|
|
|
case ReportLevel::Warning:
|
2026-01-31 23:38:26 +08:00
|
|
|
++digest.cnt_warn;
|
2026-01-31 11:28:03 +08:00
|
|
|
break;
|
2026-03-03 17:11:30 +08:00
|
|
|
case ReportLevel::Info:
|
2026-01-31 23:38:26 +08:00
|
|
|
++digest.cnt_info;
|
2026-01-31 11:28:03 +08:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-01-31 23:38:26 +08:00
|
|
|
return digest;
|
2026-01-31 11:28:03 +08:00
|
|
|
}
|
|
|
|
|
|
2026-01-31 23:38:26 +08:00
|
|
|
const std::vector<Report> &Reporter::GetReports() const {
|
|
|
|
|
return this->reports;
|
2026-01-31 11:28:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#pragma endregion
|
|
|
|
|
|
2026-01-31 23:38:26 +08:00
|
|
|
} // namespace BMapInspector::Reporter
|