Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions mods/src/log_history.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once

#include <filesystem>
#include <string>

// Keep the extension intact: community_patch.log.1 is the newest previous session.
inline void RotateLogHistory(const std::filesystem::path& log)
{
namespace fs = std::filesystem;
const auto backup = [&](int index) { return fs::path(log.string() + "." + std::to_string(index)); };
fs::remove(backup(5));
for (int index = 4; index >= 1; --index) {
if (fs::exists(backup(index))) {
fs::rename(backup(index), backup(index + 1));
}
}
if (fs::exists(log)) {
fs::rename(log, backup(1));
}
}
14 changes: 13 additions & 1 deletion mods/src/patches/patches.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "patches.h"
#include "file.h"
#include "log_history.h"
#include "version.h"

#include <il2cpp/il2cpp-functions.h>
Expand Down Expand Up @@ -70,7 +71,15 @@ __int64 il2cpp_init_hook(auto original, const char* domain_name)

File::Init();

auto file_logger = spdlog::basic_logger_mt("default", File::Log(), true);
std::string rotation_error;
try {
RotateLogHistory(File::Log());
} catch (const std::filesystem::filesystem_error& error) {
rotation_error = error.what();
}

// Append if rotation failed so an existing log is never truncated.
auto file_logger = spdlog::basic_logger_mt("default", File::Log(), false);
auto sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
file_logger->sinks().push_back(sink);
spdlog::set_default_logger(file_logger);
Expand All @@ -80,6 +89,9 @@ __int64 il2cpp_init_hook(auto original, const char* domain_name)

spdlog::set_level(log_level);
spdlog::flush_on(log_level);
if (!rotation_error.empty()) {
spdlog::warn("Failed to rotate log history; preserving existing log: {}", rotation_error);
}

#if VERSION_PATCH
if constexpr (sizeof(VERSION_COMMIT_HASH) > 1) {
Expand Down
Loading