Fix entire config being invalidated by bad value

This commit is contained in:
pixl 2023-04-29 21:23:43 -04:00
parent fed7e0cacc
commit 455f2cd6c4
No known key found for this signature in database
GPG Key ID: 1866C148CD593B6E
3 changed files with 39 additions and 5 deletions

View File

@ -12,6 +12,7 @@ find_package(PkgConfig REQUIRED)
add_executable(logid
logid.cpp
util/log.cpp
config/util.cpp
InputDevice.cpp
DeviceManager.cpp
Device.cpp

View File

@ -28,11 +28,11 @@
#include <list>
#include <set>
/// TODO: A single element failing should not cause the container to be invalid.
// Containers are chosen specifically so that no iterator is invalidated.
namespace logid::config {
void logError(const libconfig::Setting& setting, std::exception& e);
namespace {
template<typename T>
struct config_io {
@ -406,10 +406,17 @@ namespace logid::config {
struct config_io<std::optional<T>> {
static inline std::optional<T> get(const libconfig::Setting& parent,
const std::string& name) {
if (parent.exists(name))
return config_io<T>::get(parent.lookup(name));
else
if (parent.exists(name)) {
auto& setting = parent.lookup(name);
try {
return config_io<T>::get(setting);
} catch (libconfig::SettingException& e) {
logError(setting, e);
return {};
}
} else {
return {};
}
}
static inline void set(libconfig::Setting& parent,

26
src/logid/config/util.cpp Normal file
View File

@ -0,0 +1,26 @@
/*
* Copyright 2019-2023 PixlOne
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <config/types.h>
#include <util/log.h>
using namespace logid;
void config::logError(const libconfig::Setting& setting, std::exception& e) {
logPrintf(WARN, "Error at line %d: %s", setting.getSourceLine(), e.what());
}