Логи поправил
This commit is contained in:
@ -58,41 +58,19 @@ void Logger::processQueue() {
|
|||||||
while (true) {
|
while (true) {
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> lock(queueMutex);
|
std::unique_lock<std::mutex> lock(queueMutex);
|
||||||
// Ждем до 1 минуты или пока условие не выполнится
|
queueCondition.wait(lock, [this] { return !logQueue.empty() || stop; });
|
||||||
queueCondition.wait_for(lock, std::chrono::minutes(1), [this] { return !logQueue.empty() || stop; });
|
|
||||||
|
|
||||||
if (stop && logQueue.empty()){
|
if (stop && logQueue.empty()){
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!logQueue.empty()){
|
//Пытаемся переименовать файл если он существует
|
||||||
LogRec rec = std::move(logQueue.front());
|
|
||||||
logQueue.pop();
|
|
||||||
writeLog(rec.thread,rec.level,rec.data,rec.cout);
|
|
||||||
}
|
|
||||||
|
|
||||||
//Всё что ниже для переименовывания старого файла
|
|
||||||
if(!std::filesystem::exists(fileName)){
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
// Получаем время создания файла (st_ctime)
|
|
||||||
struct stat fileInfo;
|
|
||||||
if (stat(fileName.c_str(), &fileInfo) != 0) {
|
|
||||||
std::cerr << "Error getting file info: " << fileName << std::endl;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
std::time_t creationTime = fileInfo.st_ctime;
|
|
||||||
std::tm* creation_tm = std::localtime(&creationTime);
|
|
||||||
|
|
||||||
// Получаем текущее время
|
|
||||||
auto now = std::chrono::system_clock::now();
|
auto now = std::chrono::system_clock::now();
|
||||||
std::time_t now_c = std::chrono::system_clock::to_time_t(now);
|
std::time_t now_c = std::chrono::system_clock::to_time_t(now);
|
||||||
std::tm* now_tm = std::localtime(&now_c);
|
std::tm* now_tm = std::localtime(&now_c);
|
||||||
|
// Проверяем, нужно ли переименовать файл
|
||||||
//Если даты не совпадают то переименовываю файл в с датой создания и переоткрываю новый файл
|
if (lastWriteTime != 0 && (now_c - lastWriteTime) >= 86400) {
|
||||||
if( (now_tm->tm_year != creation_tm->tm_year) ||
|
if(std::filesystem::exists(fileName)){
|
||||||
(now_tm->tm_mon != creation_tm->tm_mon) ||
|
|
||||||
(now_tm->tm_mday != creation_tm->tm_mday)){
|
|
||||||
|
|
||||||
if(file.is_open()) {
|
if(file.is_open()) {
|
||||||
file.close();
|
file.close();
|
||||||
@ -100,12 +78,15 @@ void Logger::processQueue() {
|
|||||||
|
|
||||||
char buffer[11]; // Длина строки "YYYY-MM-DD" + '\0'
|
char buffer[11]; // Длина строки "YYYY-MM-DD" + '\0'
|
||||||
std::memset(buffer, 0, sizeof(buffer));
|
std::memset(buffer, 0, sizeof(buffer));
|
||||||
std::strftime(buffer, sizeof(buffer), "%Y-%m-%d", creation_tm);
|
std::strftime(buffer, sizeof(buffer), "%Y-%m-%d", now_tm);
|
||||||
std::ostringstream newFileName;
|
std::ostringstream newFileName;
|
||||||
newFileName << Utility::BeforeLast(fileName,'.') << "_" << buffer << "." << Utility::AfterLast(fileName,'.');
|
newFileName << Utility::BeforeLast(fileName,'.') << "_" << buffer << "." << Utility::AfterLast(fileName,'.');
|
||||||
|
|
||||||
std::string newName = newFileName.str();
|
std::string newName = newFileName.str();
|
||||||
|
if(std::filesystem::exists(newName)){ //Не знаю по какой причине если файл существует...
|
||||||
|
std::ostringstream newFileName;
|
||||||
|
newFileName << Utility::BeforeLast(fileName,'.') << "_" << buffer << "_" << now_c << "." << Utility::AfterLast(fileName,'.');
|
||||||
|
newName = newFileName.str();
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
std::filesystem::rename(fileName, newName);
|
std::filesystem::rename(fileName, newName);
|
||||||
} catch (const std::filesystem::filesystem_error& e) {
|
} catch (const std::filesystem::filesystem_error& e) {
|
||||||
@ -117,6 +98,15 @@ void Logger::processQueue() {
|
|||||||
Utility::deleteOldFiles(folder,10);
|
Utility::deleteOldFiles(folder,10);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Записываю лог в файл
|
||||||
|
if(!logQueue.empty()){
|
||||||
|
LogRec rec = std::move(logQueue.front());
|
||||||
|
logQueue.pop();
|
||||||
|
writeLog(rec.thread,rec.level,rec.data,rec.cout);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
@ -147,6 +137,9 @@ void Logger::writeLog(std::string thread, std::string level, std::string data, b
|
|||||||
|
|
||||||
file.write(str.str().c_str(), str.str().length());
|
file.write(str.str().c_str(), str.str().length());
|
||||||
file.flush();
|
file.flush();
|
||||||
|
|
||||||
|
// Обновляем время последней записи
|
||||||
|
lastWriteTime = std::time(nullptr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
|
|||||||
@ -25,6 +25,7 @@ struct LogRec{
|
|||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
class Logger {
|
class Logger {
|
||||||
private:
|
private:
|
||||||
|
std::time_t lastWriteTime = 0;
|
||||||
|
|
||||||
std::ofstream file;
|
std::ofstream file;
|
||||||
std::string fileName;
|
std::string fileName;
|
||||||
|
|||||||
Reference in New Issue
Block a user