Make log not throw, but warn on invalid format

This commit is contained in:
altalk23 2023-02-26 17:25:11 +03:00
parent 724a9d342b
commit 6aba7cfdec
2 changed files with 29 additions and 11 deletions
loader
include/Geode/loader
src/loader

View file

@ -141,7 +141,10 @@ namespace geode {
Mod* getSender() const;
Severity getSeverity() const;
[[deprecated("Will be removed in next version")]]
void addFormat(std::string_view formatStr, std::span<ComponentTrait*> comps);
Result<> addFormatNew(std::string_view formatStr, std::span<ComponentTrait*> comps);
};
class GEODE_DLL Logger {
@ -172,7 +175,12 @@ namespace geode {
Log l(m, sev);
std::array<ComponentTrait*, sizeof...(Args)> comps = { static_cast<ComponentTrait*>(new ComponentBase(args))... };
l.addFormat(formatStr, comps);
auto res = l.addFormatNew(formatStr, comps);
if (res.isErr()) {
internalLog(Severity::Warning, getMod(), "Error parsing log format \"{}\": {}", formatStr, res.unwrapErr());
return;
}
Logger::push(std::move(l));
}

View file

@ -132,13 +132,20 @@ Severity Log::getSeverity() const {
}
void Log::addFormat(std::string_view formatStr, std::span<ComponentTrait*> components) {
auto res = this->addFormatNew(formatStr, components);
if (res.isErr()) {
throw std::runtime_error(res.unwrapErr());
}
}
Result<> Log::addFormatNew(std::string_view formatStr, std::span<ComponentTrait*> components) {
size_t compIndex = 0;
std::string current;
for (size_t i = 0; i < formatStr.size(); ++i) {
if (formatStr[i] == '{') {
if (i == formatStr.size() - 1)
throw std::runtime_error("Unescaped { at the end of format string");
if (i == formatStr.size() - 1) {
return Err("Unescaped { at the end of format string");
}
auto const next = formatStr[i + 1];
if (next == '{') {
current.push_back('{');
@ -146,8 +153,9 @@ void Log::addFormat(std::string_view formatStr, std::span<ComponentTrait*> compo
continue;
}
if (next == '}') {
if (compIndex >= components.size())
throw std::runtime_error("Not enough arguments for format string");
if (compIndex >= components.size()) {
return Err("Not enough arguments for format string");
}
m_components.push_back(new ComponentBase(current));
m_components.push_back(components[compIndex++]);
@ -156,17 +164,18 @@ void Log::addFormat(std::string_view formatStr, std::span<ComponentTrait*> compo
++i;
continue;
}
throw std::runtime_error("You put something in between {} silly head");
return Err("You put something in between {} silly head");
}
if (formatStr[i] == '}') {
if (i == formatStr.size() - 1)
throw std::runtime_error("Unescaped } at the end of format string");
if (i == formatStr.size() - 1) {
return Err("Unescaped } at the end of format string");
}
if (formatStr[i + 1] == '}') {
current.push_back('}');
++i;
continue;
}
throw std::runtime_error("You have an unescaped }");
return Err("You have an unescaped }");
}
current.push_back(formatStr[i]);
@ -176,9 +185,10 @@ void Log::addFormat(std::string_view formatStr, std::span<ComponentTrait*> compo
m_components.push_back(new ComponentBase(current));
if (compIndex != components.size()) {
throw std::runtime_error("You have left over arguments.. silly head");
// show_silly_error(formatStr);
return Err("You have left over arguments.. silly head");
}
return Ok();
}
// Logger