Skip to content

Commit f32b437

Browse files
authored
fix(logger): handle file rotation errors
2 parents a2723d9 + df73ffa commit f32b437

2 files changed

Lines changed: 46 additions & 12 deletions

File tree

include/logit_cpp/logit/loggers/FileLogger.hpp

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -338,16 +338,39 @@ namespace logit {
338338

339339
const std::string base = time_shield::to_iso8601_date(m_current_date_ts);
340340
const std::string dir = get_directory_path();
341-
# if defined(_WIN32)
342-
const std::string cur = dir + "\\" + base + ".log";
341+
std::string rotated_str;
342+
# if __cplusplus >= 201703L
343+
# if defined(_WIN32)
344+
fs::path cur = (fs::u8path(dir) / (base + ".log")).lexically_normal();
345+
fs::path rotated = fs::u8path(make_rotated_name(base, dir)).lexically_normal();
346+
# else
347+
fs::path cur = (fs::path(dir) / (base + ".log")).lexically_normal();
348+
fs::path rotated = fs::path(make_rotated_name(base, dir)).lexically_normal();
349+
# endif
350+
std::error_code ec;
351+
fs::rename(cur, rotated, ec);
352+
if (ec) {
353+
throw std::runtime_error("Failed to rename log file: " + ec.message());
354+
}
355+
# if defined(_WIN32)
356+
rotated_str = rotated.u8string();
357+
# else
358+
rotated_str = rotated.string();
359+
# endif
343360
# else
361+
# if defined(_WIN32)
362+
const std::string cur = dir + "\\" + base + ".log";
363+
# else
344364
const std::string cur = dir + "/" + base + ".log";
345-
# endif
346-
std::string rotated = make_rotated_name(base, dir);
347-
# if defined(_WIN32)
348-
std::rename(utf8_to_ansi(cur).c_str(), utf8_to_ansi(rotated).c_str());
349-
# else
350-
std::rename(cur.c_str(), rotated.c_str());
365+
# endif
366+
rotated_str = make_rotated_name(base, dir);
367+
# if defined(_WIN32)
368+
if (std::rename(utf8_to_ansi(cur).c_str(), utf8_to_ansi(rotated_str).c_str()) != 0) {
369+
# else
370+
if (std::rename(cur.c_str(), rotated_str.c_str()) != 0) {
371+
# endif
372+
throw std::runtime_error("Failed to rename log file");
373+
}
351374
# endif
352375

353376
open_log_file(m_current_date_ts);
@@ -359,9 +382,9 @@ namespace logit {
359382
m_compressor.reset(new detail::CompressionWorker(
360383
m_config.compress, m_config.compress_level, m_config.external_cmd));
361384
}
362-
m_compressor->enqueue(rotated);
385+
m_compressor->enqueue(rotated_str);
363386
} else {
364-
detail::compress_file(m_config.compress, rotated, m_config.compress_level, m_config.external_cmd);
387+
detail::compress_file(m_config.compress, rotated_str, m_config.compress_level, m_config.external_cmd);
365388
}
366389
}
367390

tests/file_logger_rotation_test.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,28 @@
22
#include <LogIt.hpp>
33
#include <fstream>
44
#include <string>
5+
#if __cplusplus >= 201703L
6+
#include <filesystem>
7+
#endif
58

69
int main() {
710
LOGIT_ADD_FILE_LOGGER_WITH_ROTATION(".", true, 30, "%v", 20, 10);
811
const std::string msg = "0123456789"; // 10 bytes
912
LOGIT_INFO(msg);
1013
LOGIT_INFO(msg);
1114
LOGIT_WAIT();
15+
#if __cplusplus >= 201703L
16+
std::filesystem::path current = LOGIT_GET_LAST_FILE_PATH(0);
17+
std::filesystem::path rotated = current;
18+
rotated.replace_filename(current.stem().string() + ".001.log");
19+
return std::filesystem::exists(rotated) ? 0 : 1;
20+
#else
1221
std::string current = LOGIT_GET_LAST_FILE_PATH(0);
1322
std::string rotated = current;
1423
size_t pos = rotated.rfind(".log");
24+
if (pos == std::string::npos) return 1;
1525
rotated.insert(pos, ".001");
16-
std::ifstream in(rotated);
17-
return in.good() ? 0 : 1;
26+
std::ifstream f(rotated.c_str());
27+
return f.good() ? 0 : 1;
28+
#endif
1829
}

0 commit comments

Comments
 (0)