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.
148 lines
4.0 KiB
148 lines
4.0 KiB
#include "disinfection_logs_manager.hpp"
|
|
|
|
#include <dirent.h>
|
|
#include <errno.h>
|
|
|
|
#include "iflytop/core/components/fileutils.hpp"
|
|
#include "configs/project_setting.hpp"
|
|
using namespace iflytop;
|
|
using namespace core;
|
|
using namespace std;
|
|
|
|
#define LOG_STORGE_PATH "./disinfection_logs/"
|
|
|
|
|
|
DisinfectionLogger::DisinfectionLogger() {}
|
|
DisinfectionLogger::~DisinfectionLogger() {
|
|
if (m_logfile.is_open()) {
|
|
m_logfile.close();
|
|
}
|
|
}
|
|
|
|
void DisinfectionLogger::initialize(string log_file_name) { //
|
|
logger->info("create loggers:{}", log_file_name);
|
|
m_logfile.open(log_file_name, ios::out | ios::binary | ios::trunc);
|
|
if (!m_logfile.is_open()) {
|
|
logger->error("create loggers:{} failed", log_file_name);
|
|
}
|
|
}
|
|
void DisinfectionLogger::write(string log) {
|
|
m_logfile.write(log.c_str(), log.size());
|
|
if (m_logfile.fail()) {
|
|
logger->error("write log failed:{}", strerror(errno));
|
|
}
|
|
m_logfile.flush();
|
|
}
|
|
|
|
DisinfectionLogsManager::DisinfectionLogsManager(/* args */) {}
|
|
DisinfectionLogsManager::~DisinfectionLogsManager() {}
|
|
|
|
shared_ptr<DisinfectionLogger> DisinfectionLogsManager::createNewLogger(string log_file_name) {
|
|
system(fmt::format("mkdir -p {}", LOG_STORGE_PATH).c_str());
|
|
shared_ptr<DisinfectionLogger> dslogger = make_shared<DisinfectionLogger>();
|
|
dslogger->initialize(fmt::format("{}{}.csv", LOG_STORGE_PATH, log_file_name));
|
|
|
|
vector<string> files;
|
|
list_dir_csvfile(LOG_STORGE_PATH, files);
|
|
|
|
if (files.size() > MAX_DISINFECTIONLOGGER_FILE_NUM) {
|
|
logger->info("delete old loggers:{}", files.back());
|
|
deleteReport(files.back());
|
|
}
|
|
|
|
return dslogger;
|
|
}
|
|
|
|
static void split(const string& s, vector<string>& sv, const char delim = ' ') {
|
|
sv.clear();
|
|
istringstream iss(s);
|
|
string temp;
|
|
|
|
while (getline(iss, temp, delim)) {
|
|
sv.push_back(temp);
|
|
}
|
|
return;
|
|
}
|
|
void DisinfectionLogsManager::list_dir_csvfile(string path, vector<string>& sv) {
|
|
sv.clear();
|
|
#if 0
|
|
DIR* dir;
|
|
struct dirent* ptr;
|
|
if ((dir = opendir(path.c_str())) == NULL) {
|
|
logger->error("Open dir {} error...", path);
|
|
return;
|
|
}
|
|
while ((ptr = readdir(dir)) != NULL) {
|
|
if (ptr->d_name[0] == '.') continue;
|
|
if (ptr->d_type == 8) {
|
|
string filename = ptr->d_name;
|
|
if (filename.find(".csv") != string::npos) {
|
|
sv.push_back(filename);
|
|
}
|
|
}
|
|
}
|
|
closedir(dir);
|
|
#endif
|
|
|
|
/**
|
|
* @brief 读取文件夹下所有文件,并按照时间排序
|
|
*/
|
|
|
|
DIR* dir;
|
|
struct dirent* ptr;
|
|
if ((dir = opendir(path.c_str())) == NULL) {
|
|
logger->error("Open dir {} error...", path);
|
|
return;
|
|
}
|
|
|
|
vector<string> files;
|
|
while ((ptr = readdir(dir)) != NULL) {
|
|
if (ptr->d_name[0] == '.') continue;
|
|
if (ptr->d_type == 8) {
|
|
string filename = ptr->d_name;
|
|
if (filename.find(".csv") != string::npos) {
|
|
files.push_back(filename);
|
|
}
|
|
}
|
|
}
|
|
|
|
sort(files.begin(), files.end(), [](string a, string b) { return a > b; });
|
|
|
|
for (auto& file : files) {
|
|
sv.push_back(file);
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
nlohmann::json DisinfectionLogsManager::getlogger(string log_file_name) {
|
|
string content = FileUtils().readFileAsString(fmt::format("{}{}.csv", LOG_STORGE_PATH, log_file_name));
|
|
nlohmann::json csvcontent;
|
|
vector<string> lines;
|
|
split(content, lines, '\n');
|
|
|
|
for (auto& line : lines) {
|
|
csvcontent["content"].push_back(line);
|
|
}
|
|
return csvcontent;
|
|
}
|
|
nlohmann::json DisinfectionLogsManager::getLoggerList() {
|
|
//
|
|
// 1. get all files in LOG_STORGE_PATH
|
|
vector<string> files;
|
|
list_dir_csvfile(LOG_STORGE_PATH, files);
|
|
nlohmann::json loggerlist;
|
|
for (auto& file : files) {
|
|
// 获取文件名去掉.csv
|
|
file = file.substr(0, file.find(".csv"));
|
|
loggerlist.push_back(file);
|
|
}
|
|
return loggerlist;
|
|
}
|
|
|
|
void DisinfectionLogsManager::deleteReport(string log_file_name) { system(fmt::format("rm -f {}{}.csv", LOG_STORGE_PATH, log_file_name).c_str()); }
|
|
void DisinfectionLogsManager::deleteReports(vector<string> log_file_names) {
|
|
for (auto& log_file_name : log_file_names) {
|
|
deleteReport(log_file_name);
|
|
}
|
|
}
|