// // Created by zhaohe on 19-5-30. // #pragma once #include #include #include #include #include #include #include #include #include #include #include // #include "zwtimecpp/core/core.hpp" // #include "zwtimecpp/core/exception/base_exception.hpp" // #include "zwtimecpp/core/exception/base_exception.hpp" // #include "zwtimecpp/core/exception/null_expection.hpp" // #include "zwtimecpp/core/logger/logger_factory.hpp" // #include "zwtimecpp/core/utils/data.hpp" #include "components/fileutils.hpp" #include "components/marco/data.hpp" #include "nlohmann/json.hpp" #include "spdlogfactory/logger_factory.hpp" #include "components/zexception/zexception.hpp" // #include "zwtimecpp/core/utils/zmath.hpp" // ConfigurationService的实现 /*----------------------------------------------------------------*/ #define ConfigServiceENABLE_GET_AND_SET2(type, propertyName, defaultValue) \ private: \ type propertyName = defaultValue; \ \ public: \ virtual const type &get_##propertyName() const { \ lock_guard lock(lock_); \ return propertyName; \ } \ virtual void set_##propertyName(const type &propertyName) { \ lock_guard lock(lock_); \ this->propertyName = propertyName; \ flushConfigToFile(); \ } #define configTemplateGET_CONFIG2(type, propertyName, defaultValue) \ try { \ j3.at(#propertyName).get_to(propertyName); \ } catch (const std::exception &e) { \ onParseConfigFail(#propertyName, e); \ } #define configTemplateSAVE_CONFIG2(type, propertyName, defaultValue) j[#propertyName] = propertyName; #define configTemplateDEFILE_CONFIG_SERVICE2(name, configList, configfilePath, onParseFiale) \ namespace iflytop { \ using namespace std; \ using namespace core; \ using namespace nlohmann; \ class name { \ ENABLE_LOGGER(name) \ string configFile; \ \ mutable recursive_mutex lock_; \ bool forTest = false; \ \ public: \ name() {} \ \ public: \ void initialize(string configFile = configfilePath) { \ logger->debug("initialize"); \ \ this->configFile = configFile; \ json j3; \ \ string config; \ if (!FileUtils().exist(configFile)) { \ logger->error("can't find configfile ,create default config:{}", configFile); \ auto defaultConfigFile = FileUtils().openTrunc(configFile); \ defaultConfigFile->write("{}", 2); \ } \ config = FileUtils().readFileAsString(configFile); \ if (config.empty()) { \ flushConfigToFile(); \ config = FileUtils().readFileAsString(configFile); \ } \ \ try { \ j3 = json::parse(config); \ } catch (const std::exception &e) { \ throw zexception("Parse config fail......" + string(e.what())); \ } \ configList(configTemplateGET_CONFIG2); \ flushConfigToFile(); \ } \ configList(ConfigServiceENABLE_GET_AND_SET2); \ \ public: \ private: \ void flushConfigToFile() { \ json j; \ configList(configTemplateSAVE_CONFIG2); \ bool flushSuccess = FileUtils().writeToFile(configFile, j.dump(2)); \ if (!flushSuccess) { \ logger->error("flush config to logfile fail"); \ } \ }; \ static shared_ptr &instance() { \ static shared_ptr value; \ return value; \ } \ void onParseConfigFail(string file_name, const std::exception &e) { onParseFiale; }; \ }; \ }