From 455f2cd6c49cba021f5d741fcd46b98a1ba1c5d0 Mon Sep 17 00:00:00 2001 From: pixl Date: Sat, 29 Apr 2023 21:23:43 -0400 Subject: [PATCH] Fix entire config being invalidated by bad value --- src/logid/CMakeLists.txt | 1 + src/logid/config/types.h | 17 ++++++++++++----- src/logid/config/util.cpp | 26 ++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 5 deletions(-) create mode 100644 src/logid/config/util.cpp diff --git a/src/logid/CMakeLists.txt b/src/logid/CMakeLists.txt index a2fcd9b..23f291b 100644 --- a/src/logid/CMakeLists.txt +++ b/src/logid/CMakeLists.txt @@ -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 diff --git a/src/logid/config/types.h b/src/logid/config/types.h index 69c2fc5..4beb345 100644 --- a/src/logid/config/types.h +++ b/src/logid/config/types.h @@ -28,11 +28,11 @@ #include #include -/// 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 struct config_io { @@ -406,10 +406,17 @@ namespace logid::config { struct config_io> { static inline std::optional get(const libconfig::Setting& parent, const std::string& name) { - if (parent.exists(name)) - return config_io::get(parent.lookup(name)); - else + if (parent.exists(name)) { + auto& setting = parent.lookup(name); + try { + return config_io::get(setting); + } catch (libconfig::SettingException& e) { + logError(setting, e); + return {}; + } + } else { return {}; + } } static inline void set(libconfig::Setting& parent, diff --git a/src/logid/config/util.cpp b/src/logid/config/util.cpp new file mode 100644 index 0000000..dce4682 --- /dev/null +++ b/src/logid/config/util.cpp @@ -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 . + * + */ + +#include +#include + +using namespace logid; + +void config::logError(const libconfig::Setting& setting, std::exception& e) { + logPrintf(WARN, "Error at line %d: %s", setting.getSourceLine(), e.what()); +} \ No newline at end of file