oxen-logging update to handle level/type parsing exceptions

pull/1955/head
Jason Rhinelander 2 years ago
parent 9bf1d5837a
commit f6019210c3
No known key found for this signature in database
GPG Key ID: C4992CE7A88D4262

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

@ -1099,13 +1099,7 @@ namespace llarp
"logging", "logging",
"type", "type",
DefaultLogType, DefaultLogType,
[this](std::string arg) { [this](std::string arg) { m_logType = log::type_from_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;
},
Comment{ Comment{
"Log type (format). Valid options are:", "Log type (format). Valid options are:",
" print - print logs to standard output", " print - print logs to standard output",
@ -1117,13 +1111,7 @@ namespace llarp
"logging", "logging",
"level", "level",
DefaultLogLevel, DefaultLogLevel,
[this](std::string arg) { [this](std::string arg) { m_logLevel = log::level_from_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;
},
Comment{ Comment{
"Minimum log level to print. Logging below this level will be ignored.", "Minimum log level to print. Logging below this level will be ignored.",
"Valid log levels, in ascending order, are:", "Valid log levels, in ascending order, are:",

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

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

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

Loading…
Cancel
Save