refactor(dev): unify compiler error logging
This commit is contained in:
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user