refactor(dev): unify compiler error logging

This commit is contained in:
Lane0218
2026-03-11 21:25:07 +08:00
parent f9fde30d12
commit 9070775187
5 changed files with 81 additions and 28 deletions

View File

@@ -5,6 +5,48 @@
#include "utils/Log.h"
#include <ostream>
#include <string>
namespace {
bool IsCLIError(const std::string_view msg) {
return HasErrorPrefix(msg, "cli");
}
} // namespace
void LogInfo(const std::string_view msg, std::ostream& os) {
os << "[info] " << msg << "\n";
}
void LogError(const std::string_view msg, std::ostream& os) {
os << "[error] " << msg << "\n";
}
std::string FormatError(const std::string_view stage,
const std::string_view msg) {
return "[" + std::string(stage) + "] " + std::string(msg);
}
std::string FormatErrorAt(const std::string_view stage, const std::size_t line,
const std::size_t column,
const std::string_view msg) {
return "[" + std::string(stage) + "] @" + std::to_string(line) + ":" +
std::to_string(column) + " - " + std::string(msg);
}
bool HasErrorPrefix(const std::string_view msg, const std::string_view stage) {
const std::string prefix = "[" + std::string(stage) + "]";
return msg.rfind(prefix, 0) == 0;
}
void PrintException(std::ostream& os, const std::exception& ex) {
LogError(ex.what(), os);
if (IsCLIError(ex.what())) {
os << "\n";
PrintHelp(os);
}
}
void PrintHelp(std::ostream& os) {
os << "SysY Compiler (课程实验最小可运行示例)\n"