Generalise the file rotating logic

This commit is contained in:
2026-07-16 23:32:17 +10:00
parent 2b6856b692
commit d011043771
4 changed files with 200 additions and 66 deletions
+47 -23
View File
@@ -11590,16 +11590,6 @@ DN_API void DN_OS_LogPrintF(DN_LogTypeParam type, void *user_data, DN_CallSite c
va_end(args);
}
DN_API DN_Str8 DN_OS_LoggerGetRotateFilePath(DN_Str8 base_file_path, DN_Arena *arena, DN_USize rotate_index)
{
DN_Str8 result = {};
if (rotate_index == 0)
result = DN_OS_PathFmtArena(arena, "%.*s", DN_Str8PrintFmt(base_file_path));
else
result = DN_OS_PathFmtArena(arena, "%.*s.%zu", DN_Str8PrintFmt(base_file_path), rotate_index);
return result;
}
static void DN_OS_LoggerSetFilePathNoMutex_(DN_OSLogger *logger, DN_Pool *pool, DN_Str8 file_path)
{
if (logger->file_path.data) {
@@ -11647,15 +11637,7 @@ static void DN_OS_DoLogFileSetupAndRotation_(DN_OSLogger *logger)
bool needs_rotate = file_info.size >= logger->rotate_every_n_bytes;
if (needs_rotate) {
DN_OS_FileClose(&logger->file);
// NOTE: Loop through all the rotated log files from [rotate_count-1..1]. The rotated log
// file at `rotate_count-1` gets deleted and `rotate_count-2` gets moved into
// `rotate_count-1` and so forth
for (DN_USize offset = 0; offset < (logger->rotate_count - 1); offset++) {
DN_USize const file_index = logger->rotate_count - (offset + 1);
DN_Str8 file_path = DN_OS_LoggerGetRotateFilePath(logger->file_path, &scratch.arena, file_index);
DN_Str8 prev_file_path = DN_OS_LoggerGetRotateFilePath(logger->file_path, &scratch.arena, file_index - 1);
DN_OS_FileMove(prev_file_path, file_path, true, nullptr);
}
DN_OS_FileRotate(logger->file_path, logger->rotate_count, DN_Str8Lit("."), DN_OSFileRotateFlags_Nil);
}
// NOTE: After rotating, check the file size of the log file we will write to again. If the
@@ -11965,11 +11947,45 @@ DN_API uint64_t DN_OS_EstimateTSCPerSecond(uint64_t duration_ms_to_gauge_tsc_fre
return result;
}
DN_API bool DN_OS_PathIsOlderThan(DN_Str8 path, DN_Str8 check_against)
DN_API bool DN_OS_FileRotate(DN_Str8 base_file_path, DN_USize rotate_count, DN_Str8 suffix, DN_OSFileRotateFlags flags)
{
DN_OSPathInfo file_info = DN_OS_PathInfo(path);
DN_OSPathInfo check_against_info = DN_OS_PathInfo(check_against);
bool result = !file_info.exists || file_info.last_write_time_in_s < check_against_info.last_write_time_in_s;
// NOTE: Loop through all the rotated files from [rotate_count-1..0]. The rotated file at
// `rotate_count-1` (if it exists) gets deleted and `rotate_count-2` gets moved into
// `rotate_count-1` and so forth.
bool result = true;
if (rotate_count) {
if (rotate_count == 1) {
// NOTE: Rotate count of 1 just means that the base file should be deleted essentially unless
// the keep base flag is set.
if (DN_BitIsNotSet(flags, DN_OSFileRotateFlags_KeepBaseFile))
DN_OS_PathDelete(base_file_path);
} else {
DN_TCScratch scratch = DN_TCScratchBeginArena(nullptr, 0);
for (DN_USize offset = 0; offset < (rotate_count - 1); offset++) {
DN_USize const file_index = rotate_count - (offset + 1);
bool last_file_index = file_index - 1 == 0;
DN_AssertF(file_index != 0, "This index should never hits zero, we iterate in reverse and stop 1 before the last one");
DN_Str8 file_path = DN_Str8FromFmtArena(&scratch.arena, "%.*s%.*s%zu", DN_Str8PrintFmt(base_file_path), DN_Str8PrintFmt(suffix), file_index);
DN_Str8 prev_file_path = last_file_index ? base_file_path : DN_Str8FromFmtArena(&scratch.arena, "%.*s%.*s%zu", DN_Str8PrintFmt(base_file_path), DN_Str8PrintFmt(suffix), file_index - 1);
DN_OSPathInfo file_path_info = DN_OS_PathInfo(file_path);
DN_OSPathInfo prev_file_path_info = DN_OS_PathInfo(prev_file_path);
if (file_path_info.type == DN_OSPathInfoType_Directory || prev_file_path_info.type == DN_OSPathInfoType_Directory) {
result = false;
break;
}
if (prev_file_path_info.exists) {
if (last_file_index && (flags & DN_OSFileRotateFlags_KeepBaseFile))
result &= DN_OS_FileCopy(prev_file_path, file_path, /*overwrite=*/true, nullptr);
else
result &= DN_OS_FileMove(prev_file_path, file_path, /*overwrite=*/true, nullptr);
}
}
DN_TCScratchEnd(&scratch);
}
}
return result;
}
@@ -12160,6 +12176,14 @@ DN_API DN_Str8 DN_OS_Str8FromPathInfoType(DN_OSPathInfoType type)
return result;
}
DN_API bool DN_OS_PathIsOlderThan(DN_Str8 path, DN_Str8 check_against)
{
DN_OSPathInfo file_info = DN_OS_PathInfo(path);
DN_OSPathInfo check_against_info = DN_OS_PathInfo(check_against);
bool result = !file_info.exists || file_info.last_write_time_in_s < check_against_info.last_write_time_in_s;
return result;
}
DN_API bool DN_OS_PathAddRef(DN_Arena *arena, DN_OSPath *fs_path, DN_Str8 path)
{
if (!arena || !fs_path || path.count == 0)