2828
2929#include < boost/algorithm/string/predicate.hpp>
3030#include < boost/date_time/posix_time/posix_time_duration.hpp>
31- #include < boost/filesystem/operations.hpp>
32- #include < boost/filesystem/path.hpp>
3331#include < boost/log/attributes/attribute_value_set.hpp>
3432#include < boost/log/core/core.hpp>
3533#include < boost/log/expressions/filter.hpp>
4846#include < boost/log/utility/setup/console.hpp>
4947#include < boost/log/utility/setup/file.hpp>
5048#include < boost/log/utility/setup/formatter_parser.hpp>
49+ #include < fmt/core.h>
5150
5251#include < algorithm>
5352#include < array>
5453#include < cstddef>
5554#include < cstdint>
55+ #include < filesystem>
5656#include < ios>
5757#include < iostream>
5858#include < optional>
5959#include < ostream>
60- #include < stdexcept>
6160#include < string>
6261#include < string_view>
62+ #include < system_error>
6363#include < unordered_map>
6464#include < utility>
6565
@@ -111,7 +111,7 @@ getSeverityLevel(std::string_view logLevel)
111111 std::unreachable ();
112112}
113113
114- void
114+ std::expected< void , std::string>
115115LogService::init (config::ClioConfigDefinition const & config)
116116{
117117 namespace keywords = boost::log::keywords;
@@ -132,9 +132,15 @@ LogService::init(config::ClioConfigDefinition const& config)
132132
133133 auto const logDir = config.maybeValue <std::string>(" log_directory" );
134134 if (logDir) {
135- boost::filesystem::path dirPath{logDir.value ()};
136- if (!boost::filesystem::exists (dirPath))
137- boost::filesystem::create_directories (dirPath);
135+ std::filesystem::path dirPath{logDir.value ()};
136+ if (not std::filesystem::exists (dirPath)) {
137+ if (std::error_code error; not std::filesystem::create_directories (dirPath, error)) {
138+ return std::unexpected{
139+ fmt::format (" Couldn't create logs directory '{}': {}" , dirPath.string (), error.message ())
140+ };
141+ }
142+ }
143+
138144 auto const rotationPeriod = config.get <uint32_t >(" log_rotation_hour_interval" );
139145
140146 // the below are taken from user in MB, but boost::log::add_file_log needs it to be in bytes
@@ -169,8 +175,9 @@ LogService::init(config::ClioConfigDefinition const& config)
169175 for (auto it = overrides.begin <util::config::ObjectView>(); it != overrides.end <util::config::ObjectView>(); ++it) {
170176 auto const & channelConfig = *it;
171177 auto const name = channelConfig.get <std::string>(" channel" );
172- if (std::count (std::begin (Logger::kCHANNELS ), std::end (Logger::kCHANNELS ), name) == 0 )
173- throw std::runtime_error (" Can't override settings for log channel " + name + " : invalid channel" );
178+ if (std::ranges::count (Logger::kCHANNELS , name) == 0 ) { // TODO: use std::ranges::contains when available
179+ return std::unexpected{fmt::format (" Can't override settings for log channel {}: invalid channel" , name)};
180+ }
174181
175182 minSeverity[name] = getSeverityLevel (channelConfig.get <std::string>(" log_level" ));
176183 }
@@ -189,6 +196,7 @@ LogService::init(config::ClioConfigDefinition const& config)
189196 filter = boost::log::filter{std::move (logFilter)};
190197 boost::log::core::get ()->set_filter (filter);
191198 LOG (LogService::info ()) << " Default log level = " << defaultSeverity;
199+ return {};
192200}
193201
194202Logger::Pump
0 commit comments