Skip to content

Commit

Permalink
DEVOPS-10175 return logging
Browse files Browse the repository at this point in the history
  • Loading branch information
ipalenov committed Jun 13, 2024
1 parent 9ad7583 commit 5e538de
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 40 deletions.
10 changes: 5 additions & 5 deletions src/RabbitMQClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,8 @@ void RabbitMQClient::basicConsumeImpl(Biterp::CallContext& ctx) {
})
.onCancelled([this](const std::string &consumer){
LOGI("Consumer cancelled " + consumer);
// std::lock_guard<std::mutex> lock(_mutex);
// consumers.erase(std::remove_if(consumers.begin(), consumers.end(), [&consumer](std::string& s){return s == consumer;}));
std::lock_guard<std::mutex> lock(_mutex);
consumers.erase(std::remove_if(consumers.begin(), consumers.end(), [&consumer](std::string& s){return s == consumer;}));
})
.onError([this, &result](const char* message)
{
Expand Down Expand Up @@ -344,9 +344,9 @@ void RabbitMQClient::basicConsumeMessageImpl(Biterp::CallContext& ctx) {
{
std::unique_lock<std::mutex> 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;
Expand Down
71 changes: 36 additions & 35 deletions src/addin/biterp/Logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ namespace fs = std::filesystem;
#endif


//using namespace std;

namespace Biterp {

/**
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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<seconds>(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<seconds>(diff).count() > KEEP_TIME){
fs::remove(entry.path());
}
}
}catch(...){
}
}

Expand Down

0 comments on commit 5e538de

Please sign in to comment.