Kylin/Universal/ApplicationSettings.cpp

41 lines
1.3 KiB
C++

#include "ApplicationSettings.h"
#include "BoostLog.h"
#include <boost/property_tree/ini_parser.hpp>
#include <filesystem>
ApplicationSettings::ApplicationSettings(const std::string &path) : m_path(path) {
if (std::filesystem::exists(std::filesystem::path(path))) {
try {
boost::property_tree::read_ini(path, m_ptree);
} catch (const boost::property_tree::ini_parser_error &e) {
LOG(error) << e.what();
}
}
}
void ApplicationSettings::startCheckInterval(boost::asio::io_context &ioContext, uint32_t seconds) {
m_timer = std::make_unique<boost::asio::steady_timer>(ioContext);
m_interval = seconds;
run();
}
void ApplicationSettings::run() {
m_timer->expires_after(std::chrono::seconds(m_interval));
m_timer->async_wait([this](const boost::system::error_code &error) {
if (error) {
LOG(error) << error.message();
return;
}
if (m_needSave) {
try {
std::lock_guard locker(m_mutex);
boost::property_tree::write_ini(m_path, m_ptree);
} catch (const boost::property_tree::ini_parser_error &e) {
LOG(error) << e.what();
}
m_needSave = false;
}
run();
});
}