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.

98 lines
2.8 KiB

1 year ago
  1. #include "disinfection_logs_manager.hpp"
  2. #include <dirent.h>
  3. #include <errno.h>
  4. #include "iflytop/core/components/fileutils.hpp"
  5. using namespace iflytop;
  6. using namespace core;
  7. using namespace std;
  8. #define LOG_STORGE_PATH "./disinfection_logs/"
  9. DisinfectionLogger::DisinfectionLogger() {}
  10. DisinfectionLogger::~DisinfectionLogger() {
  11. if (m_logfile.is_open()) {
  12. m_logfile.close();
  13. }
  14. }
  15. void DisinfectionLogger::initialize(string log_file_name) { //
  16. logger->info("create loggers:{}", log_file_name);
  17. m_logfile.open(log_file_name, ios::out | ios::binary | ios::trunc);
  18. if (!m_logfile.is_open()) {
  19. logger->error("create loggers:{} failed", log_file_name);
  20. }
  21. }
  22. void DisinfectionLogger::write(string log) {
  23. m_logfile.write(log.c_str(), log.size());
  24. if (m_logfile.fail()) {
  25. logger->error("write log failed:{}", strerror(errno));
  26. }
  27. m_logfile.flush();
  28. }
  29. DisinfectionLogsManager::DisinfectionLogsManager(/* args */) {}
  30. DisinfectionLogsManager::~DisinfectionLogsManager() {}
  31. shared_ptr<DisinfectionLogger> DisinfectionLogsManager::createNewLogger(string log_file_name) {
  32. system(fmt::format("mkdir -p {}", LOG_STORGE_PATH).c_str());
  33. shared_ptr<DisinfectionLogger> logger = make_shared<DisinfectionLogger>();
  34. logger->initialize(fmt::format("{}{}.csv", LOG_STORGE_PATH, log_file_name));
  35. return logger;
  36. }
  37. static void split(const string& s, vector<string>& sv, const char delim = ' ') {
  38. sv.clear();
  39. istringstream iss(s);
  40. string temp;
  41. while (getline(iss, temp, delim)) {
  42. sv.push_back(temp);
  43. }
  44. return;
  45. }
  46. void DisinfectionLogsManager::list_dir_csvfile(string path, vector<string>& sv) {
  47. sv.clear();
  48. DIR* dir;
  49. struct dirent* ptr;
  50. if ((dir = opendir(path.c_str())) == NULL) {
  51. logger->error("Open dir {} error...", path);
  52. return;
  53. }
  54. while ((ptr = readdir(dir)) != NULL) {
  55. if (ptr->d_name[0] == '.') continue;
  56. if (ptr->d_type == 8) {
  57. string filename = ptr->d_name;
  58. if (filename.find(".csv") != string::npos) {
  59. sv.push_back(filename);
  60. }
  61. }
  62. }
  63. closedir(dir);
  64. return;
  65. }
  66. nlohmann::json DisinfectionLogsManager::getlogger(string log_file_name) {
  67. string content = FileUtils().readFileAsString(fmt::format("{}{}.csv", LOG_STORGE_PATH, log_file_name));
  68. nlohmann::json csvcontent;
  69. vector<string> lines;
  70. split(content, lines, '\n');
  71. for (auto& line : lines) {
  72. csvcontent["content"].push_back(line);
  73. }
  74. return csvcontent;
  75. }
  76. nlohmann::json DisinfectionLogsManager::getLoggerList() {
  77. //
  78. // 1. get all files in LOG_STORGE_PATH
  79. vector<string> files;
  80. list_dir_csvfile(LOG_STORGE_PATH, files);
  81. nlohmann::json loggerlist;
  82. for (auto& file : files) {
  83. // 获取文件名去掉.csv
  84. file = file.substr(0, file.find(".csv"));
  85. loggerlist.push_back(file);
  86. }
  87. return loggerlist;
  88. }