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

Exceptions for map.at OOB #5

Merged
merged 1 commit into from
May 20, 2024
Merged
Changes from all 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
42 changes: 40 additions & 2 deletions src/KDSpdSetup/setup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
*/
#include "setup.h"

#include <iostream>

using std::literals::string_literals::operator""s;

namespace KDSPDSetup::setup {

void handleMultipleFileSink(toml::string &&typeStr, toml::table &&sinkTable, spdlog::sink_ptr &sinkPtr, bool const &truncate)
Expand Down Expand Up @@ -43,6 +47,17 @@ void setupSink(toml::table &&sinkTable)
auto const name = sinkTable.at("name").as_string();
auto typeStr = sinkTable.at("type").as_string();

bool ok = false;
for (auto &typeList : { details::fileStrs, details::rotateStrs, details::dailyStrs, details::nullStrs, details::stdStrs, details::linuxStrs, details::winStrs }) {
if (details::inTypelist(typeStr, typeList)) {
ok = true;
break;
}
}

if (!ok)
throw std::out_of_range("KDSPDSetup: sink type "s + typeStr.str + " is not valid"s);

toml::string level = "";
if (sinkTable.contains("level")) {
level = sinkTable.at("level").as_string();
Expand Down Expand Up @@ -72,6 +87,9 @@ void setupSink(toml::table &&sinkTable)
}

if (level != "") {
if (!details::levelMap.contains(level))
throw std::out_of_range("KDSPDSetup: level "s + level.str + " not found"s);

sinkPtr->set_level(details::levelMap.at(level));
}

Expand Down Expand Up @@ -137,6 +155,9 @@ void registerAsynchronousLogger(toml::table const &loggerTable, toml::string con
static std::shared_ptr<spdlog::details::thread_pool> threadPoolPtr;

if (threadPool != "") {
if (!details::SPDMaps::threadPoolMap().contains(threadPool))
throw std::out_of_range("KDSPDSetup: threadpool "s + threadPool.str + " not found"s);

auto const threadPoolPair = details::SPDMaps::threadPoolMap().at(threadPool);
auto const queueSize = threadPoolPair.first;
auto const numThreads = threadPoolPair.second;
Expand All @@ -148,6 +169,9 @@ void registerAsynchronousLogger(toml::table const &loggerTable, toml::string con
auto const overflowPolicy =
(loggerTable.contains("overflow_policy")) ? loggerTable.at("overflow_policy").as_string() : "block";

if (!details::overflowMap.contains(overflowPolicy))
throw std::out_of_range("KDSPDSetup: overflow policy "s + overflowPolicy.str + " not found"s);

auto logger = std::make_shared<spdlog::async_logger>(name, sinkList.begin(), sinkList.end(), std::move(threadPoolPtr),
details::overflowMap.at(overflowPolicy));

Expand All @@ -168,6 +192,10 @@ void registerSynchronousLogger(toml::table const &loggerTable, toml::string cons

if (loggerTable.contains("level")) {
auto const level = loggerTable.at("level").as_string();

if (!details::levelMap.contains(level))
throw std::out_of_range("KDSPDSetup: level "s + level.str + " not found"s);

logger->set_level(details::levelMap.at(level));
}

Expand All @@ -187,11 +215,21 @@ void setupLogger(toml::table const &loggerTable)
for (auto &&sink : sinks) {
if (details::SPDMaps::sinkMap().contains(sink.as_string())) {
sinkList.emplace_back(details::SPDMaps::sinkMap().at(sink.as_string()));
} else {
// ignore sink instead of throwing, but notify that this happens
std::cerr << "KDSPDSetup: setting up logger " << name
<< " - skipped sink " << sink.as_string().str << " as it was not found";
}
}

auto const pattern =
(loggerTable.contains("pattern")) ? details::SPDMaps::patternMap().at(loggerTable.at("pattern").as_string()) : "";
std::string pattern = "";

if (loggerTable.contains("pattern")) {
if (!details::SPDMaps::patternMap().contains(loggerTable.at("pattern").as_string()))
throw std::out_of_range("KDSPDSetup: pattern "s + loggerTable.at("pattern").as_string().str + " not found"s);

pattern = details::SPDMaps::patternMap().at(loggerTable.at("pattern").as_string());
}

auto const type = (loggerTable.contains("type")) ? loggerTable.at("type").as_string() : "";

Expand Down