You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
7.9 KiB

5 months ago
  1. //
  2. // Created by zhaohe on 19-5-30.
  3. //
  4. #pragma once
  5. #include <exception>
  6. #include <fstream>
  7. #include <iostream>
  8. #include <list>
  9. #include <map>
  10. #include <memory>
  11. #include <mutex>
  12. #include <set>
  13. #include <sstream>
  14. #include <string>
  15. #include <vector>
  16. // #include "zwtimecpp/core/core.hpp"
  17. // #include "zwtimecpp/core/exception/base_exception.hpp"
  18. // #include "zwtimecpp/core/exception/base_exception.hpp"
  19. // #include "zwtimecpp/core/exception/null_expection.hpp"
  20. // #include "zwtimecpp/core/logger/logger_factory.hpp"
  21. // #include "zwtimecpp/core/utils/data.hpp"
  22. #include "components/fileutils.hpp"
  23. #include "components/marco/data.hpp"
  24. #include "nlohmann/json.hpp"
  25. #include "spdlogfactory/logger_factory.hpp"
  26. #include "components/zexception/zexception.hpp"
  27. // #include "zwtimecpp/core/utils/zmath.hpp"
  28. // ConfigurationService的实现
  29. /*----------------------------------------------------------------*/
  30. #define ConfigServiceENABLE_GET_AND_SET2(type, propertyName, defaultValue) \
  31. private: \
  32. type propertyName = defaultValue; \
  33. \
  34. public: \
  35. virtual const type &get_##propertyName() const { \
  36. lock_guard<recursive_mutex> lock(lock_); \
  37. return propertyName; \
  38. } \
  39. virtual void set_##propertyName(const type &propertyName) { \
  40. lock_guard<recursive_mutex> lock(lock_); \
  41. this->propertyName = propertyName; \
  42. flushConfigToFile(); \
  43. }
  44. #define configTemplateGET_CONFIG2(type, propertyName, defaultValue) \
  45. try { \
  46. j3.at(#propertyName).get_to(propertyName); \
  47. } catch (const std::exception &e) { \
  48. onParseConfigFail(#propertyName, e); \
  49. }
  50. #define configTemplateSAVE_CONFIG2(type, propertyName, defaultValue) j[#propertyName] = propertyName;
  51. #define configTemplateDEFILE_CONFIG_SERVICE2(name, configList, configfilePath, onParseFiale) \
  52. namespace iflytop { \
  53. using namespace std; \
  54. using namespace core; \
  55. using namespace nlohmann; \
  56. class name { \
  57. ENABLE_LOGGER(name) \
  58. string configFile; \
  59. \
  60. mutable recursive_mutex lock_; \
  61. bool forTest = false; \
  62. \
  63. public: \
  64. name() {} \
  65. \
  66. public: \
  67. void initialize(string configFile = configfilePath) { \
  68. logger->debug("initialize"); \
  69. \
  70. this->configFile = configFile; \
  71. json j3; \
  72. \
  73. string config; \
  74. if (!FileUtils().exist(configFile)) { \
  75. logger->error("can't find configfile ,create default config:{}", configFile); \
  76. auto defaultConfigFile = FileUtils().openTrunc(configFile); \
  77. defaultConfigFile->write("{}", 2); \
  78. } \
  79. config = FileUtils().readFileAsString(configFile); \
  80. if (config.empty()) { \
  81. flushConfigToFile(); \
  82. config = FileUtils().readFileAsString(configFile); \
  83. } \
  84. \
  85. try { \
  86. j3 = json::parse(config); \
  87. } catch (const std::exception &e) { \
  88. throw zexception("Parse config fail......" + string(e.what())); \
  89. } \
  90. configList(configTemplateGET_CONFIG2); \
  91. flushConfigToFile(); \
  92. } \
  93. configList(ConfigServiceENABLE_GET_AND_SET2); \
  94. \
  95. public: \
  96. private: \
  97. void flushConfigToFile() { \
  98. json j; \
  99. configList(configTemplateSAVE_CONFIG2); \
  100. bool flushSuccess = FileUtils().writeToFile(configFile, j.dump(2)); \
  101. if (!flushSuccess) { \
  102. logger->error("flush config to logfile fail"); \
  103. } \
  104. }; \
  105. static shared_ptr<name> &instance() { \
  106. static shared_ptr<name> value; \
  107. return value; \
  108. } \
  109. void onParseConfigFail(string file_name, const std::exception &e) { onParseFiale; }; \
  110. }; \
  111. }