-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
LogRotate.mpp
44 lines (38 loc) · 1.05 KB
/
LogRotate.mpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
export module CppUtils.LogRotate;
import std;
import CppUtils.FileSystem;
import CppUtils.Memory;
using namespace CppUtils::Memory::Literals;
export namespace CppUtils
{
struct LogRotateOptions final
{
std::filesystem::path filePath;
std::size_t maxFiles = 10;
std::size_t maxSize = 10_Mo;
};
inline auto logRotate(const LogRotateOptions& options) -> void
{
const auto& [filePath, maxFiles, maxSize] = options;
if (not std::filesystem::exists(filePath))
return;
if (std::filesystem::file_size(filePath) >= maxSize)
{
auto pathWithoutExtension = filePath.parent_path() / filePath.stem();
auto extension = filePath.extension();
auto newPath = pathWithoutExtension;
newPath += std::to_string(maxFiles - 1);
newPath += extension;
for (auto i = maxFiles - 1; i > 0;)
{
auto oldPath = pathWithoutExtension;
if (--i != 0)
oldPath += std::to_string(i);
oldPath += extension;
if (std::filesystem::exists(oldPath))
std::filesystem::rename(oldPath, newPath);
newPath = std::move(oldPath);
}
}
}
}