diff --git a/lib/Core/Foundation/Feature.hpp b/lib/Core/Foundation/Feature.hpp index 3cd7c67..af56d1e 100644 --- a/lib/Core/Foundation/Feature.hpp +++ b/lib/Core/Foundation/Feature.hpp @@ -45,7 +45,7 @@ class Feature : public RegistryProxy { if (--m_instance.m_deferChain == 0) { - reinterpret_cast(m_instance).OnInitialize(); + static_cast(m_instance).OnInitialize(); } } diff --git a/lib/Support/Spdlog/SpdlogProvider.cpp b/lib/Support/Spdlog/SpdlogProvider.cpp index 9286cc8..9aecce2 100644 --- a/lib/Support/Spdlog/SpdlogProvider.cpp +++ b/lib/Support/Spdlog/SpdlogProvider.cpp @@ -7,18 +7,76 @@ void Support::SpdlogProvider::OnInitialize() { - if (m_logPath.empty()) + if (m_baseLogPath.empty()) { - m_logPath = Core::Runtime::GetModulePath().replace_extension(L".log"); + m_baseLogPath = Core::Runtime::GetModulePath().replace_extension(L".log"); } - auto sink = Core::MakeShared(m_logPath.string(), true); + auto logPath = m_baseLogPath; + + if (m_appendTimestamp) + { + const auto logExtension = m_baseLogPath.extension(); + + logPath.replace_extension(); + logPath += "-"; + + if (m_maxLogCount > 0) + { + std::error_code error; + std::set existingLogs; + + for (const auto& entry : std::filesystem::directory_iterator(m_baseLogPath.parent_path(), error)) + { + if (entry.is_regular_file() && entry.path().extension() == logExtension && + entry.path().wstring().starts_with(logPath.wstring())) + { + existingLogs.insert(entry.path()); + } + } + + auto excessiveLogCount = static_cast(existingLogs.size()) - m_maxLogCount + 1; + if (excessiveLogCount > 0) + { + for (const auto& path : existingLogs) + { + std::filesystem::remove(path, error); + + if (--excessiveLogCount == 0) + { + break; + } + } + } + } + + // Append timestamp to filename + auto now = std::chrono::system_clock::now(); + std::time_t now_c = std::chrono::system_clock::to_time_t(now); + std::tm now_tm = *std::localtime(&now_c); + + const auto logTimestamp = + fmt::format("{:04d}-{:02d}-{:02d}-{:02d}-{:02d}-{:02d}", now_tm.tm_year + 1900, now_tm.tm_mon + 1, + now_tm.tm_mday, now_tm.tm_hour, now_tm.tm_min, now_tm.tm_sec); + + logPath += logTimestamp; + logPath.replace_extension(logExtension); + } + + auto sink = Core::MakeShared(logPath.string(), true); auto logger = Core::MakeShared("", spdlog::sinks_init_list{sink}); logger->flush_on(spdlog::level::trace); spdlog::set_default_logger(logger); spdlog::set_level(spdlog::level::trace); + if (m_recentSymlink && logPath != m_baseLogPath) + { + std::error_code error; + std::filesystem::remove(m_baseLogPath, error); + std::filesystem::create_symlink(logPath.filename(), m_baseLogPath, error); + } + SetDefault(*this); } diff --git a/lib/Support/Spdlog/SpdlogProvider.hpp b/lib/Support/Spdlog/SpdlogProvider.hpp index 4a83e6a..c69386b 100644 --- a/lib/Support/Spdlog/SpdlogProvider.hpp +++ b/lib/Support/Spdlog/SpdlogProvider.hpp @@ -18,13 +18,34 @@ class SpdlogProvider auto SetLogPath(const std::filesystem::path& aPath) noexcept { - m_logPath = aPath; + m_baseLogPath = aPath; + return Defer(this); + } + + auto AppendTimestampToLogName() noexcept + { + m_appendTimestamp = true; + return Defer(this); + } + + auto CreateRecentLogSymlink() noexcept + { + m_recentSymlink = true; + return Defer(this); + } + + auto SetMaxLogFiles(int32_t aMaxFiles) noexcept + { + m_maxLogCount = aMaxFiles; return Defer(this); } protected: void OnInitialize() override; - std::filesystem::path m_logPath; + std::filesystem::path m_baseLogPath; + bool m_appendTimestamp{ false }; + bool m_recentSymlink{ false }; + int32_t m_maxLogCount{ 10 }; }; } diff --git a/src/App/Application.cpp b/src/App/Application.cpp index 9c49afd..81cd757 100644 --- a/src/App/Application.cpp +++ b/src/App/Application.cpp @@ -13,7 +13,7 @@ App::Application::Application(HMODULE aHandle, const RED4ext::Sdk* aSdk) Register(aHandle)->SetBaseImagePathDepth(2); Register(); - Register(); + Register()->AppendTimestampToLogName()->CreateRecentLogSymlink(); Register(); Register(Env::GameDir()); diff --git a/src/pch.hpp b/src/pch.hpp index f6042ab..0dc3d1f 100644 --- a/src/pch.hpp +++ b/src/pch.hpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include