| 26 | } |
| 27 | |
| 28 | static void CleanupOldLogs(const std::filesystem::path& logDir) |
| 29 | { |
| 30 | std::vector<std::filesystem::directory_entry> logFiles; |
| 31 | |
| 32 | if (!std::filesystem::exists(logDir)) |
| 33 | return; |
| 34 | |
| 35 | for (const auto& entry : std::filesystem::directory_iterator(logDir)) |
| 36 | { |
| 37 | if (entry.is_regular_file() && entry.path().extension() == ".txt") |
| 38 | logFiles.push_back(entry); |
| 39 | } |
| 40 | |
| 41 | if (logFiles.size() <= MAX_LOG_FILES) |
| 42 | return; |
| 43 | |
| 44 | std::sort(logFiles.begin(), logFiles.end(), |
| 45 | [](const auto& a, const auto& b) |
| 46 | { |
| 47 | return std::filesystem::last_write_time(a) < std::filesystem::last_write_time(b); |
| 48 | }); |
| 49 | |
| 50 | // Delete oldest until we have MAX_LOG_FILES. |
| 51 | while (logFiles.size() > MAX_LOG_FILES) |
| 52 | { |
| 53 | try |
| 54 | { |
| 55 | std::filesystem::remove(logFiles.front()); |
| 56 | } |
| 57 | catch (...) {} |
| 58 | logFiles.erase(logFiles.begin()); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | void InitTENLog(const std::string& logDirContainingDir) |
| 63 | { |