oxen-logging update to handle level/type parsing exceptions

This commit is contained in:
Jason Rhinelander 2022-07-19 13:19:35 -03:00
parent 9bf1d5837a
commit f6019210c3
No known key found for this signature in database
GPG Key ID: C4992CE7A88D4262
5 changed files with 24 additions and 22 deletions

@ -1 +1 @@
Subproject commit 44b6b7834bb0eee37f32bff6b9ae8bbbd4c5eb89
Subproject commit 98a8882c81aa046fbadc0571fcea7bf92ed20154

View File

@ -1099,13 +1099,7 @@ namespace llarp
"logging",
"type",
DefaultLogType,
[this](std::string arg) {
auto type = log::type_from_string(arg);
if (type == log::Type::Unknown)
throw std::invalid_argument{fmt::format("invalid log type: {}", arg)};
m_logType = type;
},
[this](std::string arg) { m_logType = log::type_from_string(arg); },
Comment{
"Log type (format). Valid options are:",
" print - print logs to standard output",
@ -1117,13 +1111,7 @@ namespace llarp
"logging",
"level",
DefaultLogLevel,
[this](std::string arg) {
std::optional<log::Level> level = log::level_from_string(arg);
if (not level)
throw std::invalid_argument{fmt::format("invalid log level value: {}", arg)};
m_logLevel = *level;
},
[this](std::string arg) { m_logLevel = log::level_from_string(arg); },
Comment{
"Minimum log level to print. Logging below this level will be ignored.",
"Valid log levels, in ascending order, are:",

View File

@ -210,7 +210,7 @@ namespace llarp
struct LoggingConfig
{
log::Type m_logType = log::Type::Unknown;
log::Type m_logType = log::Type::Print;
log::Level m_logLevel = log::Level::off;
std::string m_logFile;

View File

@ -17,6 +17,7 @@
#include <mutex>
#include <memory>
#include <chrono>
#include <stdexcept>
#ifdef _WIN32
#define EHOSTDOWN ENETDOWN
@ -453,12 +454,17 @@ extern "C"
int EXPORT
lokinet_log_level(const char* level)
{
if (auto maybe = llarp::log::level_from_string(level))
try
{
last_log_set = *maybe;
llarp::log::reset_level(*maybe);
auto new_level = llarp::log::level_from_string(level);
llarp::log::reset_level(new_level);
last_log_set = new_level;
return 0;
}
catch (std::invalid_argument& e)
{
llarp::LogError(e.what());
}
return -1;
}

View File

@ -1,6 +1,7 @@
#include <catch2/catch.hpp>
#include <util/logging.hpp>
#include <config/config.hpp>
#include <oxen/log/level.hpp>
using TestString = std::string;
@ -35,9 +36,16 @@ std::vector<TestParseLog> testParseLog{// bad cases
TEST_CASE("parseLevel")
{
const auto data = GENERATE(from_range(testParseLog));
const auto maybe = llarp::log::level_from_string(data.input);
CHECK(maybe == data.level);
const auto& [input, expected] = GENERATE(from_range(testParseLog));
if (not expected)
REQUIRE_THROWS_AS(llarp::log::level_from_string(input), std::invalid_argument);
else
{
llarp::log::Level level;
REQUIRE_NOTHROW(level = llarp::log::level_from_string(input));
CHECK(level == *expected);
}
}
TEST_CASE("TestLogLevelToString")