Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Better errors on logger init failure #1857

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/main/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,21 @@ try {
if (not app::parseConfig(run.configPath))
return EXIT_FAILURE;

util::LogService::init(gClioConfig);
if (auto const initSuccess = util::LogService::init(gClioConfig); not initSuccess) {
std::cerr << initSuccess.error() << std::endl;
return EXIT_FAILURE;
}
app::ClioApplication clio{gClioConfig};
return clio.run(run.useNgWebServer);
},
[](app::CliArgs::Action::Migrate const& migrate) {
if (not app::parseConfig(migrate.configPath))
return EXIT_FAILURE;

util::LogService::init(gClioConfig);
if (auto const initSuccess = util::LogService::init(gClioConfig); not initSuccess) {
std::cerr << initSuccess.error() << std::endl;
return EXIT_FAILURE;
}
app::MigratorApplication migrator{gClioConfig, migrate.subCmd};
return migrator.run();
}
Expand Down
23 changes: 15 additions & 8 deletions src/util/log/Logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@

#include <boost/algorithm/string/predicate.hpp>
#include <boost/date_time/posix_time/posix_time_duration.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/log/attributes/attribute_value_set.hpp>
#include <boost/log/core/core.hpp>
#include <boost/log/expressions/filter.hpp>
Expand All @@ -48,18 +46,20 @@
#include <boost/log/utility/setup/console.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/formatter_parser.hpp>
#include <fmt/core.h>

#include <algorithm>
#include <array>
#include <cstddef>
#include <cstdint>
#include <filesystem>
#include <ios>
#include <iostream>
#include <optional>
#include <ostream>
#include <stdexcept>
#include <string>
#include <string_view>
#include <system_error>
#include <unordered_map>
#include <utility>

Expand Down Expand Up @@ -111,7 +111,7 @@ getSeverityLevel(std::string_view logLevel)
std::unreachable();
}

void
std::expected<void, std::string>
LogService::init(config::ClioConfigDefinition const& config)
{
namespace keywords = boost::log::keywords;
Expand All @@ -132,9 +132,15 @@ LogService::init(config::ClioConfigDefinition const& config)

auto const logDir = config.maybeValue<std::string>("log_directory");
if (logDir) {
boost::filesystem::path dirPath{logDir.value()};
if (!boost::filesystem::exists(dirPath))
boost::filesystem::create_directories(dirPath);
std::filesystem::path dirPath{logDir.value()};
if (not std::filesystem::exists(dirPath)) {
if (std::error_code error; not std::filesystem::create_directories(dirPath, error)) {
return std::unexpected{
fmt::format("Couldn't create logs directory '{}': {}", dirPath.string(), error.message())
};
}
}

auto const rotationPeriod = config.get<uint32_t>("log_rotation_hour_interval");

// the below are taken from user in MB, but boost::log::add_file_log needs it to be in bytes
Expand Down Expand Up @@ -170,7 +176,7 @@ LogService::init(config::ClioConfigDefinition const& config)
auto const& channelConfig = *it;
auto const name = channelConfig.get<std::string>("channel");
if (std::count(std::begin(Logger::kCHANNELS), std::end(Logger::kCHANNELS), name) == 0)
throw std::runtime_error("Can't override settings for log channel " + name + ": invalid channel");
return std::unexpected{fmt::format("Can't override settings for log channel {}: invalid channel", name)};

minSeverity[name] = getSeverityLevel(channelConfig.get<std::string>("log_level"));
}
Expand All @@ -189,6 +195,7 @@ LogService::init(config::ClioConfigDefinition const& config)
filter = boost::log::filter{std::move(logFilter)};
boost::log::core::get()->set_filter(filter);
LOG(LogService::info()) << "Default log level = " << defaultSeverity;
return {};
}

Logger::Pump
Expand Down
3 changes: 2 additions & 1 deletion src/util/log/Logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@

#include <array>
#include <cstddef>
#include <expected>
#include <optional>
#include <ostream>
#include <string>
Expand Down Expand Up @@ -279,7 +280,7 @@ class LogService {
*
* @param config The configuration to use
*/
static void
[[nodiscard]] static std::expected<void, std::string>
init(config::ClioConfigDefinition const& config);

/**
Expand Down
28 changes: 26 additions & 2 deletions tests/unit/LoggerTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@
#include "util/newconfig/ConfigValue.hpp"
#include "util/newconfig/Types.hpp"

#include <boost/json/array.hpp>
#include <boost/json/object.hpp>
#include <boost/json/parse.hpp>
#include <fmt/core.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>

#include <cstddef>
Expand Down Expand Up @@ -113,7 +115,7 @@ TEST_F(LoggerInitTest, DefaultLogLevel)
ASSERT_FALSE(parsingErrors.has_value());
std::string const logString = "some log";

LogService::init(config_);
EXPECT_TRUE(LogService::init(config_));
for (auto const& channel : Logger::kCHANNELS) {
Logger const log{channel};
log.trace() << logString;
Expand Down Expand Up @@ -151,7 +153,7 @@ TEST_F(LoggerInitTest, ChannelLogLevel)
ASSERT_FALSE(parsingErrors.has_value());
std::string const logString = "some log";

LogService::init(config_);
EXPECT_TRUE(LogService::init(config_));
for (auto const& channel : Logger::kCHANNELS) {
Logger const log{channel};
log.trace() << logString;
Expand All @@ -175,6 +177,28 @@ TEST_F(LoggerInitTest, ChannelLogLevel)
}
}

TEST_F(LoggerInitTest, InitReturnsErrorIfCouldNotCreateLogDirectory)
{
auto const parsingErrors = config_.parse(ConfigFileJson{boost::json::object{{"log_directory", "/root"}}});
kuznetsss marked this conversation as resolved.
Show resolved Hide resolved
ASSERT_FALSE(parsingErrors.has_value());

auto const result = LogService::init(config_);
EXPECT_FALSE(result);
EXPECT_THAT(result.error(), testing::HasSubstr("Couldn't create logs directory"));
}

TEST_F(LoggerInitTest, InitReturnsErrorIfProvidedInvalidChannel)
{
auto const parsingErrors = config_.parse(ConfigFileJson{
boost::json::object{{"log_channels", boost::json::array{boost::json::object{{"channel", "SomeChannel"}}}}}
});
ASSERT_FALSE(parsingErrors.has_value());

auto const result = LogService::init(config_);
EXPECT_FALSE(result);
EXPECT_EQ(result.error(), "Can't override settings for log channel SomeChannel: invalid channel");
}

TEST_F(LoggerInitTest, LogSizeAndHourRotationCannotBeZero)
{
std::vector<std::string_view> const keys{
Expand Down
Loading