Skip to content

Commit

Permalink
Update shared lib
Browse files Browse the repository at this point in the history
  • Loading branch information
psiberx committed Jan 18, 2024
1 parent b7c2dd4 commit 9e4a271
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/Core/Foundation/Feature.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class Feature : public RegistryProxy<Feature>
{
if (--m_instance.m_deferChain == 0)
{
reinterpret_cast<Feature&>(m_instance).OnInitialize();
static_cast<Feature&>(m_instance).OnInitialize();
}
}

Expand Down
64 changes: 61 additions & 3 deletions lib/Support/Spdlog/SpdlogProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<spdlog::sinks::basic_file_sink_mt>(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<std::filesystem::path> 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<int32_t>(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<spdlog::sinks::basic_file_sink_mt>(logPath.string(), true);
auto logger = Core::MakeShared<spdlog::logger>("", 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);
}

Expand Down
25 changes: 23 additions & 2 deletions lib/Support/Spdlog/SpdlogProvider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
};
}
2 changes: 1 addition & 1 deletion src/App/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ App::Application::Application(HMODULE aHandle, const RED4ext::Sdk* aSdk)
Register<Core::RuntimeProvider>(aHandle)->SetBaseImagePathDepth(2);

Register<Support::MinHookProvider>();
Register<Support::SpdlogProvider>();
Register<Support::SpdlogProvider>()->AppendTimestampToLogName()->CreateRecentLogSymlink();
Register<Support::RedLibProvider>();

Register<App::ArchiveService>(Env::GameDir());
Expand Down
1 change: 1 addition & 0 deletions src/pch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <map>
#include <memory>
#include <ranges>
#include <set>
#include <source_location>
#include <string>
#include <string_view>
Expand Down

0 comments on commit 9e4a271

Please sign in to comment.