| 19 | } |
| 20 | |
| 21 | void Logger::EnsureOutputStream() { |
| 22 | if (ofs) |
| 23 | return; |
| 24 | |
| 25 | const auto logs_path = CONFIG_INSTANCE.GetLogsPath(); |
| 26 | |
| 27 | // List log files |
| 28 | std::vector<std::filesystem::directory_entry> logs; |
| 29 | for (const auto& entry : std::filesystem::directory_iterator(logs_path)) { |
| 30 | if (entry.is_regular_file() && entry.path().extension() == ".log") |
| 31 | logs.push_back(entry); |
| 32 | } |
| 33 | |
| 34 | // Delete oldest logs if needed |
| 35 | if (logs.size() >= MAX_LOG_FILES) { |
| 36 | std::sort(logs.begin(), logs.end(), [](const auto& a, const auto& b) { |
| 37 | return std::filesystem::last_write_time(a) < |
| 38 | std::filesystem::last_write_time(b); |
| 39 | }); |
| 40 | |
| 41 | const usize to_delete = logs.size() - (MAX_LOG_FILES - 1); |
| 42 | for (usize i = 0; i < to_delete; ++i) |
| 43 | std::filesystem::remove(logs[i]); |
| 44 | } |
| 45 | |
| 46 | // Create a new log file |
| 47 | // TODO: version |
| 48 | const auto path = fmt::format("{}/" APP_NAME "_{:%Y-%m-%d_%H-%M-%S}.log", |
| 49 | logs_path, std::chrono::system_clock::now()); |
| 50 | ofs = new std::ofstream(path); |
| 51 | |
| 52 | // Get start time |
| 53 | start_time = clock_t::now(); |
| 54 | } |
| 55 | |
| 56 | } // namespace hydra |
nothing calls this directly
no test coverage detected