82 lines
2.4 KiB
C++
82 lines
2.4 KiB
C++
|
/***
|
||
|
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
||
|
|
||
|
File: DirLogArchive.cpp
|
||
|
Date: 2022-2-6
|
||
|
Author: Reece
|
||
|
***/
|
||
|
#include <Source/RuntimeInternal.hpp>
|
||
|
#include "DirLogArchive.hpp"
|
||
|
#include "FileSink.hpp"
|
||
|
|
||
|
namespace Aurora::Logging::Sinks
|
||
|
{
|
||
|
static void EraseFilesByTimestamp(AuUInt32 maxLogs, const AuString &path, /*const its not worth reallocation*/ AuList<AuString> &files)
|
||
|
{
|
||
|
if (files.size() <= maxLogs)
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
// our filenames are usually prefixed by an ISO 8601 timestamp where the most significant bits are last (YYYY-MM-DD?HH-MM-SS)
|
||
|
// a quick ghetto sort should be all we need. no need to waste time parsing timestamps
|
||
|
std::sort(files.begin(), files.end());
|
||
|
|
||
|
auto amount = files.size() - maxLogs;
|
||
|
for (auto x = 0, i = 0; ((x < amount) && (i > files.size())); i++)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
if (AuIOFS::Remove(path + "/" + files[i]))
|
||
|
{
|
||
|
x++;
|
||
|
}
|
||
|
}
|
||
|
catch (...)
|
||
|
{
|
||
|
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
static void CleanupOldLogs(DirectoryLogger logger, const AuString &baseLogPath)
|
||
|
{
|
||
|
AuList<AuString> files;
|
||
|
AuBST<AuString, AuIOFS::Stat> fileMeta;
|
||
|
AuUInt32 size {};
|
||
|
|
||
|
AuIOFS::FilesInDirectory(baseLogPath, files);
|
||
|
|
||
|
for (const auto &file : files)
|
||
|
{
|
||
|
AuIOFS::Stat stat;
|
||
|
AuIOFS::StatFile(baseLogPath + "/" + file, stat);
|
||
|
fileMeta[file] = stat;
|
||
|
size += stat.size;
|
||
|
}
|
||
|
|
||
|
EraseFilesByTimestamp(logger.maxLogsOrZero, baseLogPath, files);
|
||
|
// TODO: erase when size >= maxFileSizeOrZero * 1024
|
||
|
// Didn't auRuntime v1.0 have this?
|
||
|
}
|
||
|
|
||
|
IBasicSink *NewDirectoryLoggerNew(const AuString &baseDirectory, DirectoryLogger meta, bool binary)
|
||
|
{
|
||
|
AuString path;
|
||
|
|
||
|
auto tm = Time::ToCivilTime(Time::CurrentClockMS());
|
||
|
path = fmt::format("{}/{:04}-{:02}-{:02}T{:02}-{:02}-{:02}Z.txt",
|
||
|
baseDirectory,
|
||
|
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
|
||
|
tm.tm_hour, tm.tm_min, tm.tm_sec);
|
||
|
|
||
|
CleanupOldLogs(meta, baseDirectory);
|
||
|
|
||
|
return Sinks::NewFileSinkNew(path, binary);
|
||
|
}
|
||
|
|
||
|
void NewDirectoryLoggerRelease(IBasicSink *logger)
|
||
|
{
|
||
|
Sinks::NewFileSinkRelease(logger);
|
||
|
}
|
||
|
}
|