Kylin/Universal/ApplicationSettings.cpp

37 lines
1.1 KiB
C++
Raw Normal View History

2024-01-06 00:06:35 +08:00
#include "ApplicationSettings.h"
#include "BoostLog.h"
#include <boost/property_tree/ini_parser.hpp>
#include <filesystem>
ApplicationSettings::ApplicationSettings(boost::asio::io_context &ioContext, const std::string &path)
: m_ioContext(ioContext), m_timer(ioContext), 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();
}
}
run();
}
void ApplicationSettings::run() {
m_timer.expires_after(std::chrono::seconds(1));
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();
});
}