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
+53 -10
View File
@@ -1,4 +1,4 @@
// Generated by the DN single header generator 2026-07-16 14:16:33
// Generated by the DN single header generator 2026-07-16 23:31:53
#if !defined(DN_H)
#define DN_H
@@ -4216,7 +4216,18 @@ struct DN_OSTimer /// Record time between two time-points using the OS's perform
DN_U64 end;
};
// NOTE: DN_OSFile
typedef DN_U32 DN_OSFileRotateFlags;
enum DN_OSFileRotateFlags_
{
DN_OSFileRotateFlags_Nil = 0,
// NOTE: All files are rotated as usual but when it's time to rotate the base file to index 1,
// this flags causes the base file to be copied to index 1 instead of being moved. This may be
// useful if you are looking to make rotating backups rather than rotating files.
DN_OSFileRotateFlags_KeepBaseFile = 1 << 0,
};
enum DN_OSPathInfoType
{
DN_OSPathInfoType_Unknown,
@@ -6458,19 +6469,13 @@ DN_API void DN_OS_SetLogPrintFuncToOS
// `DN_OSLoggerFlags_FileError` from the logger before attempting to log again. Setting a new
// file path via `DN_OS_LoggerSetFilePath` will also clear this flag.
//
// The rotating logger outputs files to the base `file_path` and then rotates it to
// `<file_path>.1` after the desired size is rotated for example:
//
// <file_path>
// <file_path>.1
// ..
// <file_path>.N-1
// See `DN_OS_FileRotate` for more information on the rotating scheme. The files are rotated with
// a suffix of ".".
//
// The target byte count for rotation is a hint. A large log message on a file teetering against
// the maximum allowed byte size will cause the log file to exceed the target size before being
// rotated on the next log invocation.
DN_API DN_Str8 DN_OS_LoggerGetRotateFilePath (DN_Str8 base_file_path, DN_Arena *arena, DN_USize rotate_index);
DN_API void DN_OS_LoggerSetFilePath (DN_OSLogger *logger, DN_Pool *pool, DN_Str8 file_path);
DN_API void DN_OS_LoggerFV (DN_OSLogger *logger, DN_LogTypeParam type, DN_CallSite call_site, DN_LogFlags flags, DN_FMT_ATTRIB char const *fmt, va_list args);
DN_API void DN_OS_LoggerF (DN_OSLogger *logger, DN_LogTypeParam type, DN_CallSite call_site, DN_LogFlags flags, DN_FMT_ATTRIB char const *fmt, ...);
@@ -6548,8 +6553,46 @@ DN_API DN_F64 DN_OS_TimerUs (D
DN_API DN_F64 DN_OS_TimerNs (DN_OSTimer timer);
DN_API DN_U64 DN_OS_EstimateTSCPerSecond (uint64_t duration_ms_to_gauge_tsc_frequency);
// NOTE: OS File
// Overview
// File system manipulation functionality. Implemented using Win32 API and POSIX API on linux
// machines.
// API
// DN_OS_FileRotate
// Rotate the file at `base_file_path` by moving it to `<base_file_path><suffix><rotate_index>`
// and then moving the file at `rotate_index+1` to `rotate_index+2` up until `rotate_count`.
//
// For example:
//
// base_file_path: "log.txt"
// rotate_count: 4
// suffix: ".bak."
//
// Will execute the rotate operation, in this order:
//
// log.txt.bak.2 moves to => log.txt.bak.3
// log.txt.bak.1 moves to => log.txt.bak.2
// log.txt moves to => log.txt.bak.1
//
// Set the flag `DN_OSFileRotateFlags_KeepBaseFile` to preserve the `base_file_path` by copying
// it instead of moving it, e.g the example changes to:
//
// log.txt.bak.2 moves to => log.txt.bak.3
// log.txt.bak.1 moves to => log.txt.bak.2
// log.txt copied to => log.txt.bak.1
//
// This function is a no-op if `rotate_count <= 0`. If `rotate_count == 1` we interpret that as
// we want to cycle the singular file specified at `base_file_path` which translates to just
// deleting the file specified at `base_file_path` unless `DN_OSFileRotateFlags_KeepBaseFile`
// is set. In that case this function also becomes a no-op.
//
// If any of the files to rotate are directories this function will stop and return false. If
// the files to rotate does not exist, the function returns true.
DN_API bool DN_OS_FileCopy (DN_Str8 src, DN_Str8 dest, bool overwrite, DN_ErrSink *err);
DN_API bool DN_OS_FileMove (DN_Str8 src, DN_Str8 dest, bool overwrite, DN_ErrSink *err);
DN_API bool DN_OS_FileRotate (DN_Str8 base_file_path, DN_USize rotate_count, DN_Str8 suffix, DN_OSFileRotateFlags flags);
DN_API DN_OSFile DN_OS_FileOpen (DN_Str8 path, DN_OSFileOpen open_mode, DN_OSFileAccess access, DN_ErrSink *err);
DN_API DN_OSFileRead DN_OS_FileRead (DN_OSFile *file, void *buffer, DN_USize count, DN_ErrSink *err);