Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions include/Logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <chrono>
#include <cstdio>
#include <ctime>
#include <filesystem>
#include <format>
#include <fstream>
Expand All @@ -20,22 +21,30 @@ class Logger {
private:
/**
* @brief Retourne heure formatée
* @return Horodatage "HH:MM:SS.mmm"
* @return Horodatage "HH:MM:SS"
*/
static std::string time() {
return std::format(
"{:%T}", std::chrono::zoned_time{std::chrono::current_zone(),
std::chrono::system_clock::now()});
auto now = std::chrono::system_clock::now();
std::time_t t = std::chrono::system_clock::to_time_t(now);
std::tm local_tm{};
localtime_r(&t, &local_tm);
char buf[9];
std::strftime(buf, sizeof(buf), "%T", &local_tm);
return std::string(buf);
}

/**
* @brief Retourne date formatée
* @return Horodatage "YYYY-MM-DD"
*/
static std::string date() {
return std::format(
"{:%F}", std::chrono::zoned_time{std::chrono::current_zone(),
std::chrono::system_clock::now()});
auto now = std::chrono::system_clock::now();
std::time_t t = std::chrono::system_clock::to_time_t(now);
std::tm local_tm{};
localtime_r(&t, &local_tm);
char buf[11];
std::strftime(buf, sizeof(buf), "%F", &local_tm);
return std::string(buf);
}

/**
Expand Down
10 changes: 7 additions & 3 deletions test/LoggerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,13 @@ TEST_CASE("Logger functionality") {
// Vérifier si fichier a été archivé avec date
// La logique de rotation: rename(filePath, date() + filePath)
// Il faut obtenir la chaîne de date
auto now = std::chrono::zoned_time{std::chrono::current_zone(),
std::chrono::system_clock::now()};
std::string dateStr = std::format("{:%F}", now);
auto now = std::chrono::system_clock::now();
std::time_t t = std::chrono::system_clock::to_time_t(now);
std::tm local_tm{};
localtime_r(&t, &local_tm);
char dateBuf[11];
std::strftime(dateBuf, sizeof(dateBuf), "%F", &local_tm);
std::string dateStr = dateBuf;
std::string rotatedFile = dateStr + basicLog;

CHECK(std::filesystem::exists(rotatedFile));
Expand Down
Loading