From 5e538de1ae4056f5a11e34d64cf1c4017deccdcf Mon Sep 17 00:00:00 2001 From: Ivan Palenov Date: Thu, 13 Jun 2024 12:11:43 +0300 Subject: [PATCH] DEVOPS-10175 return logging --- src/RabbitMQClient.cpp | 10 +++--- src/addin/biterp/Logger.hpp | 71 +++++++++++++++++++------------------ 2 files changed, 41 insertions(+), 40 deletions(-) diff --git a/src/RabbitMQClient.cpp b/src/RabbitMQClient.cpp index 86efc04..b7fd5ba 100644 --- a/src/RabbitMQClient.cpp +++ b/src/RabbitMQClient.cpp @@ -310,8 +310,8 @@ void RabbitMQClient::basicConsumeImpl(Biterp::CallContext& ctx) { }) .onCancelled([this](const std::string &consumer){ LOGI("Consumer cancelled " + consumer); - // std::lock_guard lock(_mutex); - // consumers.erase(std::remove_if(consumers.begin(), consumers.end(), [&consumer](std::string& s){return s == consumer;})); + std::lock_guard lock(_mutex); + consumers.erase(std::remove_if(consumers.begin(), consumers.end(), [&consumer](std::string& s){return s == consumer;})); }) .onError([this, &result](const char* message) { @@ -344,9 +344,9 @@ void RabbitMQClient::basicConsumeMessageImpl(Biterp::CallContext& ctx) { { std::unique_lock lock(_mutex); if (messageQueue.empty()){ - // if (!consumerError.empty()){ - // throw Biterp::Error(consumerError); - // } + if (!consumerError.empty()){ + throw Biterp::Error(consumerError); + } if (!cvDataArrived.wait_for(lock, std::chrono::milliseconds(timeout), [&] { return !messageQueue.empty(); })) { ctx.setBoolResult(false); return; diff --git a/src/addin/biterp/Logger.hpp b/src/addin/biterp/Logger.hpp index 3844965..9f260ab 100644 --- a/src/addin/biterp/Logger.hpp +++ b/src/addin/biterp/Logger.hpp @@ -50,8 +50,6 @@ namespace fs = std::filesystem; #endif -//using namespace std; - namespace Biterp { /** @@ -94,7 +92,6 @@ namespace Biterp { } inline static void log(int level, const std::string &text, const Logger& logger) { - return; instance()._log(level, text, logger); } @@ -131,11 +128,11 @@ namespace Biterp { } private: - const int CLEAN_INTERVAL = 600; - const int KEEP_TIME = 60 * 10; - const char* FILE_FMT = "%Y-%m-%d-%H-%M"; - const char* ISO_FMT = "%FT%H:%M:%S"; - const char* PREFIX = "comc1c"; + static constexpr int CLEAN_INTERVAL = 600; + static constexpr int KEEP_TIME = 60 * 10; + static constexpr char FILE_FMT[] = "%Y-%m-%d-%H-%M"; + static constexpr char ISO_FMT[] = "%FT%H:%M:%S"; + static constexpr char PREFIX[] = "comc1c"; /** * Set logs filename, open current logfile. @@ -204,37 +201,41 @@ namespace Biterp { return record.dump(); } - void cleanOld(){ + static void cleanOld(Logging* thiz){ auto now = system_clock::now(); std::tm tm = {}; - if (!fs::exists(_path)){ - return; - } - for (const auto & entry : fs::directory_iterator(_path)){ - if (!entry.is_regular_file() || entry.path().extension() != ".txt"){ - continue; - } - std::string nm = entry.path().stem().string(); - if (nm.find(PREFIX) != 0){ - continue; - } - size_t pos = nm.find("-"); - if (pos == std::string::npos){ - continue; - } - std::string dt = nm.substr(pos + 1); - if (dt.length() != 16 || dt[4]!='-' || dt[7]!='-' || dt[10]!='-' || dt[13]!='-'){ - continue; - } - std::istringstream ss(dt); - ss >> std::get_time(&tm, FILE_FMT); - if (ss.fail()){ - continue; + std::string path = thiz->_path; + try{ + if (!path.empty() && !fs::exists(path)){ + return; } - auto diff = now - system_clock::from_time_t(mktime(&tm)); - if (duration_cast(diff).count() > KEEP_TIME){ - fs::remove(entry.path()); + for (const auto & entry : fs::directory_iterator(path)){ + if (!entry.is_regular_file() || entry.path().extension() != ".txt"){ + continue; + } + std::string nm = entry.path().stem().string(); + if (nm.find(Logging::PREFIX) != 0){ + continue; + } + size_t pos = nm.find("-"); + if (pos == std::string::npos){ + continue; + } + std::string dt = nm.substr(pos + 1); + if (dt.length() != 16 || dt[4]!='-' || dt[7]!='-' || dt[10]!='-' || dt[13]!='-'){ + continue; + } + std::istringstream ss(dt); + ss >> std::get_time(&tm, Logging::FILE_FMT); + if (ss.fail()){ + continue; + } + auto diff = now - system_clock::from_time_t(mktime(&tm)); + if (duration_cast(diff).count() > KEEP_TIME){ + fs::remove(entry.path()); + } } + }catch(...){ } }