From e39efd72f107211a0d416059ea040ebf41000aef Mon Sep 17 00:00:00 2001 From: Aster Seker Date: Sun, 14 Sep 2025 08:11:02 +0300 Subject: [PATCH 1/2] fix(logger): handle file rotation errors --- .../logit_cpp/logit/loggers/FileLogger.hpp | 43 ++++++++++++++----- tests/file_logger_rotation_test.cpp | 11 +++-- 2 files changed, 38 insertions(+), 16 deletions(-) diff --git a/include/logit_cpp/logit/loggers/FileLogger.hpp b/include/logit_cpp/logit/loggers/FileLogger.hpp index ba749c1..9ac9260 100644 --- a/include/logit_cpp/logit/loggers/FileLogger.hpp +++ b/include/logit_cpp/logit/loggers/FileLogger.hpp @@ -338,16 +338,39 @@ namespace logit { const std::string base = time_shield::to_iso8601_date(m_current_date_ts); const std::string dir = get_directory_path(); -# if defined(_WIN32) - const std::string cur = dir + "\\" + base + ".log"; + std::string rotated_str; +# if __cplusplus >= 201703L +# if defined(_WIN32) + fs::path cur = (fs::u8path(dir) / (base + ".log")).lexically_normal(); + fs::path rotated = fs::u8path(make_rotated_name(base, dir)).lexically_normal(); +# else + fs::path cur = (fs::path(dir) / (base + ".log")).lexically_normal(); + fs::path rotated = fs::path(make_rotated_name(base, dir)).lexically_normal(); +# endif + std::error_code ec; + fs::rename(cur, rotated, ec); + if (ec) { + throw std::runtime_error("Failed to rename log file: " + ec.message()); + } +# if defined(_WIN32) + rotated_str = rotated.u8string(); +# else + rotated_str = rotated.string(); +# endif # else +# if defined(_WIN32) + const std::string cur = dir + "\\" + base + ".log"; +# else const std::string cur = dir + "/" + base + ".log"; -# endif - std::string rotated = make_rotated_name(base, dir); -# if defined(_WIN32) - std::rename(utf8_to_ansi(cur).c_str(), utf8_to_ansi(rotated).c_str()); -# else - std::rename(cur.c_str(), rotated.c_str()); +# endif + rotated_str = make_rotated_name(base, dir); +# if defined(_WIN32) + if (std::rename(utf8_to_ansi(cur).c_str(), utf8_to_ansi(rotated_str).c_str()) != 0) { +# else + if (std::rename(cur.c_str(), rotated_str.c_str()) != 0) { +# endif + throw std::runtime_error("Failed to rename log file"); + } # endif open_log_file(m_current_date_ts); @@ -359,9 +382,9 @@ namespace logit { m_compressor.reset(new detail::CompressionWorker( m_config.compress, m_config.compress_level, m_config.external_cmd)); } - m_compressor->enqueue(rotated); + m_compressor->enqueue(rotated_str); } else { - detail::compress_file(m_config.compress, rotated, m_config.compress_level, m_config.external_cmd); + detail::compress_file(m_config.compress, rotated_str, m_config.compress_level, m_config.external_cmd); } } diff --git a/tests/file_logger_rotation_test.cpp b/tests/file_logger_rotation_test.cpp index 3d4b2e2..754eecc 100644 --- a/tests/file_logger_rotation_test.cpp +++ b/tests/file_logger_rotation_test.cpp @@ -2,6 +2,7 @@ #include #include #include +#include int main() { LOGIT_ADD_FILE_LOGGER_WITH_ROTATION(".", true, 30, "%v", 20, 10); @@ -9,10 +10,8 @@ int main() { LOGIT_INFO(msg); LOGIT_INFO(msg); LOGIT_WAIT(); - std::string current = LOGIT_GET_LAST_FILE_PATH(0); - std::string rotated = current; - size_t pos = rotated.rfind(".log"); - rotated.insert(pos, ".001"); - std::ifstream in(rotated); - return in.good() ? 0 : 1; + std::filesystem::path current = LOGIT_GET_LAST_FILE_PATH(0); + std::filesystem::path rotated = current; + rotated.replace_filename(current.stem().string() + ".001.log"); + return std::filesystem::exists(rotated) ? 0 : 1; } From df73ffa40ea0fed56adff03aa030b0aca419442f Mon Sep 17 00:00:00 2001 From: Aster Seker Date: Sun, 14 Sep 2025 16:28:19 +0300 Subject: [PATCH 2/2] test: guard rotation test for C++11 --- tests/file_logger_rotation_test.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/file_logger_rotation_test.cpp b/tests/file_logger_rotation_test.cpp index 754eecc..ad2c9e5 100644 --- a/tests/file_logger_rotation_test.cpp +++ b/tests/file_logger_rotation_test.cpp @@ -2,7 +2,9 @@ #include #include #include +#if __cplusplus >= 201703L #include +#endif int main() { LOGIT_ADD_FILE_LOGGER_WITH_ROTATION(".", true, 30, "%v", 20, 10); @@ -10,8 +12,18 @@ int main() { LOGIT_INFO(msg); LOGIT_INFO(msg); LOGIT_WAIT(); +#if __cplusplus >= 201703L std::filesystem::path current = LOGIT_GET_LAST_FILE_PATH(0); std::filesystem::path rotated = current; rotated.replace_filename(current.stem().string() + ".001.log"); return std::filesystem::exists(rotated) ? 0 : 1; +#else + std::string current = LOGIT_GET_LAST_FILE_PATH(0); + std::string rotated = current; + size_t pos = rotated.rfind(".log"); + if (pos == std::string::npos) return 1; + rotated.insert(pos, ".001"); + std::ifstream f(rotated.c_str()); + return f.good() ? 0 : 1; +#endif }