address log accumulation issue and non-ascii files load on program init

This commit is contained in:
shweet committed 2026-08-13 21:40:28 -04:00
1 parent 46690d6975
commit 5e27111b21
12 files changed
+811 -153

No files matched your search

+140 -2
View File
@@ -1,11 +1,15 @@
#include "log.hpp"
#include <algorithm>
#include <array>
#include <exception>
#include <filesystem>
#include <format>
#include <print>
#include <vector>
#include "file.hpp"
#include "path.hpp"
#include "sdl.hpp"
#include "strings.hpp"
#include "time.hpp"
@@ -13,6 +17,7 @@
#if _WIN32
#include <fcntl.h>
#include <io.h>
#include <process.h>
#else
#include <unistd.h>
#endif
@@ -21,6 +26,108 @@ using namespace anm2ed::util;
namespace anm2ed
{
namespace log
{
constexpr auto SESSION_LIMIT = 10;
constexpr std::uintmax_t DIRECTORY_SIZE_LIMIT = 100ull * 1024ull * 1024ull;
constexpr std::uintmax_t SESSION_SIZE_LIMIT = 25ull * 1024ull * 1024ull;
struct SessionFile
{
std::filesystem::path path{};
std::filesystem::file_time_type time{};
std::uintmax_t size{};
};
std::uint32_t process_id_get()
{
#if _WIN32
return (std::uint32_t)_getpid();
#else
return (std::uint32_t)getpid();
#endif
}
std::filesystem::path directory_path_get() { return sdl::preferences_directory_get() / "logs"; }
std::filesystem::path session_path_get()
{
auto name = std::format("anm2ed_{}_{}.log", time::get("%Y-%m-%d_%H-%M-%S"), process_id_get());
return directory_path_get() / path::from_utf8(name);
}
std::vector<SessionFile> session_files_get(const std::filesystem::path& directory,
const std::filesystem::path& currentPath)
{
std::vector<SessionFile> files{};
std::error_code ec{};
if (!std::filesystem::is_directory(directory, ec)) return files;
for (std::filesystem::directory_iterator it(directory, ec), end{}; !ec && it != end; it.increment(ec))
{
auto path = it->path();
if (path == currentPath || path.extension() != ".log") continue;
std::error_code fileEc{};
if (!std::filesystem::is_regular_file(path, fileEc)) continue;
auto size = std::filesystem::file_size(path, fileEc);
if (fileEc) continue;
auto time = std::filesystem::last_write_time(path, fileEc);
if (fileEc)
{
time = std::filesystem::file_time_type::min();
}
files.push_back({path, time, size});
}
return files;
}
void session_file_remove(const SessionFile& file, std::uintmax_t& totalSize)
{
std::error_code ec{};
std::filesystem::remove(file.path, ec);
if (!ec && totalSize >= file.size) totalSize -= file.size;
}
void sessions_prune(const std::filesystem::path& directory, const std::filesystem::path& currentPath)
{
auto files = session_files_get(directory, currentPath);
std::sort(files.begin(), files.end(), [](const SessionFile& left, const SessionFile& right)
{ return left.time > right.time; });
std::uintmax_t totalSize{};
for (const auto& file : files)
totalSize += file.size;
while (files.size() >= SESSION_LIMIT)
{
session_file_remove(files.back(), totalSize);
files.pop_back();
}
while (totalSize > DIRECTORY_SIZE_LIMIT && !files.empty())
{
session_file_remove(files.back(), totalSize);
files.pop_back();
}
}
void legacy_log_remove(const std::filesystem::path& currentPath)
{
auto directory = sdl::preferences_directory_get();
if (directory.empty()) return;
auto legacyPath = directory / "log.txt";
if (legacyPath == currentPath) return;
std::error_code ec{};
std::filesystem::remove(legacyPath, ec);
}
}
void Logger::write_raw(const std::string& message)
{
std::lock_guard lock(mutex);
@@ -35,7 +142,21 @@ namespace anm2ed
{
try
{
file::write_string(logPath, message + '\n', "ab");
auto lineSize = (std::uintmax_t)message.size() + 1;
auto isLineWithinLimit = logSize <= log::SESSION_SIZE_LIMIT && lineSize <= log::SESSION_SIZE_LIMIT - logSize;
if (isLineWithinLimit)
{
auto line = message + '\n';
if (file::write_string(logPath, line, "ab")) logSize += line.size();
}
else if (!isLogLimitReached)
{
std::string line =
"[WARNING] Session log size limit reached; suppressing further file logs for this session.\n";
if (logSize + line.size() <= log::SESSION_SIZE_LIMIT)
if (file::write_string(logPath, line, "ab")) logSize += line.size();
isLogLimitReached = true;
}
}
catch (...)
{
@@ -151,10 +272,27 @@ namespace anm2ed
void Logger::open(const std::filesystem::path& path)
{
logPath = path;
logSize = 0;
isLogLimitReached = false;
if (!logPath.empty())
{
std::error_code ec{};
auto directory = logPath.parent_path();
if (!directory.empty()) std::filesystem::create_directories(directory, ec);
log::legacy_log_remove(logPath);
log::sessions_prune(directory, logPath);
logSize = std::filesystem::file_size(logPath, ec);
if (ec) logSize = 0;
isLogLimitReached = logSize >= log::SESSION_SIZE_LIMIT;
}
stderr_redirect_start();
}
std::filesystem::path Logger::path() { return sdl::preferences_directory_get() / "log.txt"; }
std::filesystem::path Logger::path()
{
static auto path = log::session_path_get();
return path;
}
Logger::Logger()
{